Get Time

An Enterprise Java Overview
Wednesday, January 14, 2004

By dplass
TopCoder Member

In this feature, I will give an overview of designing systems using Java 2 Enterprise Edition (J2EE) and touch upon some of the technologies used to implement these systems. J2EE actually encompasses over a dozen separate technologies; I will only describe a handful of them here. Likewise, this article mostly covers web applications ("web apps") since that has been my area of expertise over the last 4 years, even though Enterprise Java can be used for client-server, or other types of applications.

Design Overview
J2EE web app design nearly always uses the "Model-View-Controller", or MVC design pattern. It allows software architects to design in layers, which naturally leads to an implementation with those corresponding layers:

The Model layer represents the actual "business objects" that are manipulated, such as a User, an Address, or an Order. This layer is often combined with a persistence layer, such as Entity Enterprise Java Beans (EJBs) or Java Database Connectivity (JDBC.)

The View presents the model to the user. Typically this is provided by Java Server Pages (JSPs), which render HTML to the user. The Controller accepts input from the user, modifies the Model, and can change which View the user is seeing. They are usually implemented using Java Servlets and/or EJBs.

The Model Layer
The Model is usually implemented using "PODS" - Plain Old Data Structures. These are "simple" Java classes that have "getters" and "setters" for their properties. The properties themselves are private but the getters and setters are public. For example:

public class Address
      private String city;

      public String getCity() { return city; }
      public void setCity(String c) { = c;}

The model holds data that is input from the user or stored in the database. The relationships between these business entities are modeled using references between a parent and its children. For example:

public class Person
      private Address address;
      public Address getAddress() { return address; }
      public void setAddress(Address a) { this.address = c; }

A one-to-many relationship is usually built as a Vector or List of multiple instances of the object.

Persistence Models
The persistence layer provides storage of data to the database, and retrieval from databases, usually relational SQL databases.

The commonly used persistence models include:

  1. JDBC - Java Database Connectivity. This is a standard J2EE technology used for accessing SQL databases. Most major database vendors provide JDBC drivers, which are specific to each database itself. Happily, since JDBC standardizes the access to SQL, developers do not need to know the specifics of each database in order to use JDBC.
  2. The drawback of JDBC is that there is no standard way to map between objects and relational databases (the so-called "O-R mapping"). Many third-party Java products exist, such as Hibernate and JDO, to aid in this mapping and can make persistence nearly invisible to the rest of the code.
  3. Entity EJB. This technology can automatically map a Java class to a table in a SQL database. A major benefit of using EJBs is that the server, not the developer, manages transactions. Entity EJBs can be cumbersome to code. Historically, the mapping between the model and the database has been vendor-specific and it has been difficult to model complex table relationships (although both are mitigated by the latest EJB 2.0 specification.)

Enterprise Java Beans (EJB)
EJBs are components that provide distributed execution, transaction control, declarative security, multi-threading, and scalability. The mechanism to access an EJB is similar to a remote procedure call, except that the EJB compiler rather than the developer implements the actual call.

There are various flavors of EJBs, which include:

  1. Entity EJBs. Each instance of the bean represents a row in a table of a database. Entity EJBs are cached in the application server so that multiple clients do not have to re-read the same records from the database. The two variations on Entity EJBs are:
    1. Container Managed Persistence (CMP)- The mapping between the columns of a table is defined by the developer in a configuration file (called a deployment descriptor). Until recently, this was a vendor-specific mapping where the specific application server's EJB compiler implements all the insert, update, delete and retrieve code.
    2. Bean Managed Persistence (BMP) - All database interactions are written by the developer. The developer can model complex relationships between tables, and thus objects. The drawback to BMP is that it is error-prone and potentially slower to develop than CMP beans.
  2. Session EJBs. These EJBs are used as controllers.
    1. Stateful Session EJBs (SFSB). These beans keep their state between calls; they can be used as "shopping carts" for a particular user.
    2. Stateless Session EJBs (SLSB). The application server maintains a "pool" of objects that do not retain their state between calls. SLSB's are useful to control a transaction from start to finish, spanning multiple Entity EJB calls, multiple databases, and JDBC calls.

The View Layer
Java Server Pages (JSPs) are the technology of choice when implementing the View of a web application. JSPs are text files with sections that can include Java code or XML (called "tags"). For example, this is a simple JSP that generates HTML to show the current date:

<%@ page import="java.util.Date"%>
<html> <head><title>JSP Date</title></head>
The current date is <%=new Date()%>

The output of this JSP is:

Even though the above example renders HTML and JSPs are nearly always used to generate HTML, JSPs can be used to generate any kind of text. The usefulness of JSPs is that they can generate dynamic content but still use a familiar format.

There are actually two different ways to produce dynamic content in JSPs. Java scriptlets are bits and pieces of Java code interspersed in the text of the page. In the example above, the scriptlet is underlined. The disadvantage of this is that the code is not re-usable. If you wanted the same code snippet on multiple pages, (even though there is an "include" mechanism in JSP) the code itself is not inherently reusable. Another disadvantage of using scriptlets is that JSP development is frequently relegated to non-Java developers who specialize in HTML and JSPs. Forcing them to use a language that they do not know is counter-productive and can lead to sub-optimal JSPs.

Instead, JSP custom tag libraries are used. The developer can either write her own tag libraries (in Java), or use pre-made libraries, such as the JSTL library. With a JSP tag library, tags replace the scriptlets. Instead of:

The current date is <%=new Date()%>

A developer could write

The current date is 
<dt:format pattern="MM/dd/yyyy hh:mm">

As one can see, the tag syntax is XML and therefore it would be easy to transition HTML developers into "JSP-with-tags" developers.

When designing a website that uses JSP technology, I strongly advise that you consider designing and developing with templates. Web sites need a consistent look and feel and therefore will contain common visual elements, such as a header, navigation area, and footer. By designing your pages to use a common template, you will save yourself headaches later when you need to change the header on EVERY page. There are many frameworks for JSP that can help with this; Tiles is one of these frameworks and works with the Struts framework; both part of The Apache Software Foundation's Jakarta project.

The Controller Layer
The Controller is responsible for receiving input from the user (usually in the form of an HTTP request), taking some action, and updating the model. The controller then sends the user to a view (usually a different view) to display the results of the action.

Servlets are the J2EE objects that take input from users. They can be multi-threaded (to increase performance) with little effort on the part of the coder, and can take part in container-managed security.

Servlets and JSPs co-operate in two ways:

  1. a. Servlets store their output - the updated Model - in common places for JSPs to then read and display.
  2. JSPs send their data to servlets based on the target of an HTML form; servlets redirect the web browser back to a particular JSP to change the "view".

Often, servlets are used in conjunction with Stateless Session EJBs. The benefit of this mechanism is that we can avoid writing our own transaction control by using the EJB's Container-Managed Transaction management.

Finally, I'd like to mention Struts. Struts is a widely-used, widely-supported framework that makes designing and building the View, Controller, and Model layers easier. The benefits of using Struts includes the following:

  1. Data mapping from a servlet to a Model
  2. A JSP tag library
  3. A way to "internationalize" messages in JSPs
  4. A mechanism to redirect among different JSPs after the controller processes user requests
  5. Other useful View and Controller functionality

A thorough discussion of Struts is way beyond the scope of this article. There are a plethora of written and on-line references for more information.

Conclusion and references
As with every other framework, designing systems using Enterprise Java provides much flexibility, requiring the software designer to make many choices along the way. This article shows the typical layers of J2EE applications and identifies some of the decision points that arise when building web applications. Each topic is worthy of an article unto itself; this article was just the tip of the iceberg! - Sun Microsystems publishes their "Blueprints" to design and develop Enterprise applications and provides much on-line documentation. This covers EJBs, JSP, Java Servlets, as well as the design patterns that Sun recommends when designing web applications. - The Jakarta project has too many Java libraries and frameworks to mention; Struts, Tiles, Taglibs, and others are used in many web applications. Instead of re-inventing the wheel, you can use these open-source projects to reduce development time. - "The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence." - "Hibernate is a powerful, ultra-high performance object/relational persistence and query service for Java"

Would you like to write a feature?