Java EE application tiers, containers and components

At first, the aim of this post was to talk just about the Java EE containers, but then I thought it would be a good idea to include a few words about the components hosted by these containers, and finally, I ended up wrapping all this stuff with the typical tiers of a Java EE application. Thus, the post title.

This paragraph summarizes what is being explained on the whole article. Java EE platform uses a distributed multi-tiered application model for enterprise applications, and these tiers have containers that work as a runtime environment for the different components that made up the Java EE application. What? Let's see it in detail...

Java EE Tiers

In a multi-tiered application, the functionality of the application is separated into isolated functional areas called tiers. This is not something odd or complex, it is rather an approach to building software in such a way that it is more flexible an easy to maintain and update. It should allow to replace or change parts of the application without affecting the other parts.

A multi-tiered Java EE application that consists of three physical tiers and four logical tiers.

Java EE multi-tiered applications are generally composed by four logical tiers: client tier, web tier, business tier and EIS tier (Enterprise Information System tier). Another typically used model is the three-tiered architecture, where the business tier does not exist and the web tier works directly with the EIS tier. Whether they are four-tiered or three-tiered, the Java EE applications are considered to be three-tiered applications (three physical tiers), because they are distributed over three locations: the client machine, the Java EE server machine, and the database or legacy system.

Java EE Containers

A container is a runtime environment that holds and manages components such as Java Servlets, JavaServer Pages and Enterprise Java beans. It provides an environment that works as an interface between the server and the Java EE APIs used by these components. Java EE defines two types of containers to support Web applications: the Web container and the EJB container.

Web Container

Java Servlets, JavaServer Faces (JSFs) and JavaServer Pages (JSPs) run within a Web container of a Web server. A Web server, that provides a Web container, must implement the Servlet and the JSP specifications from Sun Microsystems. These servers are generally called Servlet containers, but sometimes they are also referred to as Web containers or Web engines.

When the Web server application gets a request asking for a Servlet, the server handles the request and delegates the management of the Servlet to the container in which it is deployed. It is the container that loads, runs and manages the Servlet when it is requested. Besides this, the container gives us these other benefits:

  • Life cycle management: as stated before, the container is responsible to load, instantiate and initialize the Servlet class, and invoke the corresponding method on the instance. Once the Servlet has finished its task, the container is responsible to make the instance of the Servlet eligible for the garbage collector.
  • Communication support: you will talk to the container trough the API, and the container is responsible to communicate to the Web server, so you do not need to worry about the Web server.
  • Multi-threading support: the container automatically creates a new Java thread for every Servlet request it receives.
  • Declarative security: you can use an XML deployment descriptor to configure the security without having to hard-code it into your Servlet source code.
  • JSP support: it is responsible of translating the JSP code into Java code.

And the list go on. Due to the services offered by the container, we can concentrate on solving the business problem.

Containers running on the client, web and business tiers.

EJB container

This container is a runtime environment for managing Enterprise JavaBeans. The container holds and manages an Enterprise JavaBean in much the same manner that a Servlet container does it with a Java Servlet. Moreover, it performs other tasks like registering the object, providing a remote interface for the object, managing the active state for the object, coordinating distributed transactions and optionally, the container can also manage all persistent data within the object.

Enterprise beans typically contain the business logic for a J2EE application, there are three major types of beans supported by the container:

  • Session Bean: it can be either "Stateful" or "Stateless". Represents transient objects and processes and typically are used by a single client.
  • Entity Bean: it can be either "CMP" (Container Managed Persistence) or "BMP" (Bean Managed Persistence). Represent persistent data, typically maintained in a database.
  • Message Driven Bean: (also known as MDBs or Message Beans) are used to pass messages asynchronously to application modules and services.

The detailed functionality of each type of bean is out of the goal of this post.

Other containers

There are two more containers in the previous figure, inside the client tier. The application client container which manages the execution of application client components and the applet container which manages the execution of applets. Both of them run on the client side and it is enough to know that they exist.

Java EE Components

A Java EE component implements a well defined functionality, it is managed by the container, allowing to communicate with the other components. While the platform vendors supply implementations of the Java EE platform, fulfilling the Java EE specifications, in the form of containers you, as application developer, should develop your applications in the form of components and deploy them over these containers.

Java componenents that can appear on a multi-tiered Java application.

As illustrated in the figure above the Java EE specification defines the following Java EE components:

  • On the client tier, the stand-alone client applications and applets are components that run inside the Application client container and the applet container respectively.
  • Java Servlet, JavaServer Faces, and JavaServer Pages (JSP) technology components are web components that run inside the Web container on the web server.
  • Enterprise JavaBeans (EJB) components are business components that run inside the EJB container on the application server.

Typically, when building an enterprise application, most of the development time is spend on writing web components, Servlets and JSPs, and business components, Enterprise JavaBeans.

Conclusions

Before writing this post, I was mistaking "Web container" for "Web server" and "EJB container" for "Application server". It is not such a big mistake, we are right whether we say that JBoss is an Application Server or an EJB Container. Probably you will find some other names out there and they will be as good as mine (or better). What I am trying to tell you is, that this post should help you to clarify few concepts about this topics, but do not memorize the names used as if they were the only ones accepted. I am pretty sure that you will read some other names out there like logic tier, middle tier, jsp container, web engine and so on. By the way, how do you call the tiers, servers or containers? Any other name I missed?

References

0 comments

Be the first to write a comment!

Post a Comment

Top