J2EE Application Components
Having seen how different J2EE components and services fit in, let us take a closer look at each of them to understand the roles they play in the complex game of enterprise computing.
Web Container Components
Servlets are server-side, presentation logic components that reside in the Web container. As applets extend the basic functionality of a Web browser, servlets extend the functionality of a Web serverproviding programming capabilities and the capability to generate dynamic contents, apart from serving static HTML.
Servlets are capable of receiving the HTTP requests emanating from a client's Web browser, process the input parameters received, and deliver the output as an HTTP response that can then be displayed on the client's browser.
For example, servlets can be used to develop a simple Web-based authentication system, receive the username and password from a client browser, process the request, and send back an authorization success or failure message.
Apart from accepting requests from client browsers, servlets can also respond to calls from other servlets.
Although servlets can generate HTTP response stream to clients by themselves, a better methodology to display the processed results is to use Java server pages.
Java Server Pages
Java server pages (JSPs) are also presentation logic components that reside, along with servlets, in the Web container. But they are slightly different from the latter in their scope and capacity.
JSPs are a cross between HTML and Java; that is, JSPs can contain HTML codes as well as Java codes. Although the HTML codes are directly sent to the client's browser, Java codes are stripped off and interpreted in the server itself. Thus, JSPs are most useful for incorporating intelligence inside HTML tags.
A structured and reusable way of incorporating Java intelligence in JSPs is to use JSP tag libraries. These libraries are easy to develop, and result in cleaner segregation of Java codes from HTML codes.
JSPs complement servlets in the Web container, and are often used to display the results processed by a servlet. It is also typical to find application designs, which use JSPsin powerful association with Javabeanswithout any servlets involved.
JSPs, in combination with Javabeans and/or servlets, can result in compact and yet extensible application designs.
Javabeans are basic data model components that were introduced to the Java community long before the advent of J2EE. In J2EE architecture, Javabeans usually reside in a client tier or presentation logic tier, and usually complement applets or Java server pages.
Javabeans define a certain basic grammar for application codes to be qualified as bean components. For example, a simple Java class with a few variables, and get_attribute() and set_attribute() methods for those variables, can be qualified as a standard Javabean component.
Setting aside the naming similarity, Javabeans are no way related to Enterprise Javabeans (EJBs), which are completely different breeds of enterprise Java application components.
EJB Container Components
Enterprise Javabeans (or EJBs) are distributed, scalable business logic components of J2EE middleware. They encapsulate core business logic and data model elements in all enterprise Java projects.
EJBs reside in specialized environments called EJB containers, which are provided by the application server vendor. The J2EE specifications define the relationship (contracts) between the application EJBs coded by developers and the container environments.
Developers adhere to certain interfaces as they develop their EJBs. Although certain methods defined in the interface are to be implemented by the developers themselves, the rest are left to the EJB container provider (or the application server vendor). Under the hood, we can understand this grammar as a methodology to segregate business logic implementations (developer-coded methods) from infrastructure provisions (container-implemented methods).
An EJB can talk to any other EJBeither within the same container or in a different container that is hosted in a remote server. They can make use of all enterprise services, as well as custom component libraries and service access libraries.
EJB specifications define several breeds of EJBs that serve the needs of different application scenarios:
- Stateless session beans
- Stateful session beans
- Entity beans
- Message driven beans
In combination with servlets and Java server pages, Enterprise Javabeans result in highly flexible and reusable MVC-based application architecture.