J2EE (Java 2 Platform, Enterprise Edition) is a platform-independent, Java-centric environment for developing, building, and deploying web-based enterprise applications. It includes APIs for developing and deploying multitier, scalable, reliable, and secure network applications.
The key components include:
JSP (JavaServer Pages): For building dynamic web content.
Servlets: For handling HTTP requests and responses.
EJB (Enterprise JavaBeans): For building business logic.
JMS (Java Message Service): For messaging between components.
JDBC (Java Database Connectivity): For database connectivity.
A web container, such as Apache Tomcat, manages the lifecycle of servlets and JSP pages. It handles HTTP requests, manages sessions, and provides security for web applications.
JSP is a technology that simplifies the creation of dynamic web pages by embedding Java code into HTML. Servlets, on the other hand, are Java classes that handle requests and generate dynamic responses, typically in HTML or JSON format. JSPs are easier to use for designing the user interface, while servlets are better suited for controlling logic and processing requests.
MVC (Model-View-Controller) is a design pattern used in J2EE to separate concerns:
Model: Represents the application's data and business logic.
View: Responsible for displaying the data, usually JSP pages.
Controller: Manages the user input, processes requests, and coordinates between the model and the view (typically implemented as servlets).
EJB (Enterprise JavaBeans) is a server-side component that encapsulates business logic of an application. There are three types of EJB:
Session Beans: Used to handle business logic, can be stateless or stateful.
Entity Beans: Represents persistent data stored in a database (though mostly replaced by JPA in modern applications).
Message-Driven Beans: Used to handle asynchronous processing via JMS.
Stateless Session Bean: Does not maintain any state across method calls or transactions. It’s used for operations where the context does not depend on previous interactions.
Stateful Session Bean: Maintains conversational state across multiple method calls or transactions, ideal for processes like online shopping carts.
JNDI (Java Naming and Directory Interface) is used for looking up resources such as EJBs, data sources, JMS queues, etc., in a directory service. It allows applications to find and retrieve these objects by name.
Interceptors are used in EJB to implement cross-cutting concerns like logging, security, and transaction management. They are applied before or after the execution of a method in a session bean or a message-driven bean.
A WAR (Web Application Archive) file is used to package a web application for deployment. It contains JSP files, servlets, Java classes, HTML files, and other resources like libraries. It is deployed in a web container such as Apache Tomcat.
Dependency injection (DI) is a design pattern used to inject dependencies into a class at runtime rather than during object creation. In J2EE, DI is used to manage resources like EJBs, JDBC DataSources, and JMS queues without explicitly looking them up in code. This improves testability and reduces coupling.
JPA (Java Persistence API) is a specification for object-relational mapping in Java, and Hibernate is an implementation of JPA. JPA defines the standards for mapping Java objects to relational database tables, while Hibernate provides additional features like caching, lazy loading, and specific annotations.
A transaction in J2EE is a unit of work that is treated as a single atomic operation. J2EE provides declarative transaction management using annotations like @Transactional in EJB or using Java EE’s javax.transaction.UserTransaction API to programmatically manage transactions.
The different types of EJB transactions are:
Bean-Managed Transactions (BMT): The developer manages transactions using code.
Container-Managed Transactions (CMT): The EJB container manages the transactions automatically. Common transaction attributes are Required, RequiresNew, and Mandatory.
A message-driven bean (MDB) is a type of EJB used for asynchronous message processing. It listens for messages from JMS queues or topics and processes them when received. It is commonly used for handling tasks like order processing, email notification, or logging.
Clustering in J2EE refers to the use of multiple servers (nodes) working together to provide high availability, load balancing, and fault tolerance. J2EE applications can be deployed across a cluster to ensure that if one server fails, others continue to provide the service without interruption.
The different scopes are:
Request Scope: Data is available only for the duration of the HTTP request.
Session Scope: Data is available across multiple requests for the same user session.
Application Scope: Data is available across all sessions and users in the application.
Page Scope: Data is available only for the current JSP page.
Optimistic Locking: Assumes that conflicts are rare, so it checks for conflicts only when committing the transaction.
Pessimistic Locking: Locks the data immediately when reading it, preventing other transactions from accessing it until the lock is released.
J2EE provides a range of security mechanisms, including:
Declarative Security: Configuring security constraints in web.xml without writing code.
Programmatic Security: Using APIs like HttpServletRequest for role-based access control.
JAAS (Java Authentication and Authorization Service): A standard security framework for user authentication and authorization in Java applications.
JTA (Java Transaction API) allows for distributed transactions across multiple databases and systems. It is used for managing global transactions, while JDBC transactions are limited to a single database. JTA is typically used with EJBs, while JDBC transactions are used with direct database access in servlets or plain Java classes.