Introduction to the Java 2 Platform, Enterprise Edition (J2EE)

Simon Brown, October 2002

Introduction

This article, the first in a series, will introduce J2EE and present an overview of what it is and what it can do. In addition to this, we'll also take a look at how to get started with J2EE by presenting the steps necessary to download, install and start developing J2EE applications.

Future articles will subsequently take a look at some of the core J2EE technologies such as Java Servlets, JavaServer Pages (JSP), Enterprise JavaBeans (EJB) and the Java Message Service (JMS). Following on from this, we'll move on to take a look at J2EE from an architecture and design perspective, covering topics like best practices and design patterns.

What is J2EE?

Using the Java 2 Platform, Standard Edition (J2SE) as a basis, Java 2 Platform, Enterprise Edition (J2EE) builds on top of this to provide the types of services that are necessary to build large scale, distributed, component based, multi-tier applications. Essentially, J2EE is a collection of APIs that can be used to build such systems, although this is only half of the picture. J2EE is also a standard for building and deploying enterprise applications, held together by the specifications of the APIs that it defines and the services that J2EE provides. In other words, this means that the "write once, run anywhere" promises of Java apply for enterprise applications too:

What does J2EE comprise?

J2EE is comprised of many APIs that can be used to build enterprise applications. Although the total list of APIs initially seems overwhelming, it is worth bearing in mind that some are primarily used by the J2EE environment in which your application executes, while some provide services that your specific application may not require. Therefore, it is worth remembering that you don't have to use all of them in order to build J2EE applications. For completeness, however, the full list of technologies that make up J2EE is as follows:

From a developer perspective, the main technologies are EJB, JSP, Java Servlets, JDBC and JMS, although JNDI is used for locating EJBs and other enterprise resources. For the moment, let's take a quick look at some of these technologies before moving on to see how to get started with J2EE.

What are Java Servlets?

At a high level, Java Servlets are the Java equivalent of CGI scripts that can be used to perform processing and the servicing of client requests on a web server. From an implementation perspective, servlets are simply Java classes that implement a predefined interface. One use for servlets is that they can be used to dynamically generate content for presentation to the user, and this is achieved by embedding markup language (e.g. HTML) inside the Java code. As Servlets are written in Java, they have access to the rich library of features provided by Java, including access to databases and other enterprise resources such as EJB.

What are JavaServer Pages (JSP)?

JSP is another technology for presenting information to the user over the web and uses a paradigm where Java code is embedded into the HTML - the opposite of servlets, and much like Microsoft ASP. Pages are written as HTML files with embedded Java source code known as scriptlets.

One of the pitfalls in using JSP is that it is very easy to build large pages containing lots of embedded Java code and business logic. For this reason, JSPs provide easy integration with JavaBeans and another feature called JSP tag extensions. These custom tags (also known as custom actions) allow re-usable functionality to be encapsulated into XML-like tags that can be easily used on the pages by both page developers and designers.

What are Enterprise JavaBeans?

EJB is a major part of the J2EE specification and defines a model for building server-side, reusable components. There are three types of enterprise beans currently supported by J2EE - session beans, entity beans and message-driven beans.

Session beans can be seen as extensions to the client application and are typically used to model business processes. There are two types of session bean - stateful and stateless. Stateful session beans are typically used to record conversational state for a single client between requests, whereas stateless session beans are shared between any number of clients at any one time.

Entity beans are typically used to model persistent business entities and, in particular, data in a database. A common mapping is to model an entity bean on a table, there being one instance of that bean for every row in the table. There are two ways that persistence can be achieved - container managed and bean managed persistence. In container managed persistence, a mapping is defined at deployment time between the persistent properties in the bean and the columns in the table. With bean managed persistence, developers write the JDBC code that performs the create, read, update and delete operations.

Finally, message-driven beans allow functionality to be executed on an asynchronous basis, typically triggered by JMS messages from message-oriented middleware.

What is the Java Message Service (JMS)?

JMS is Java API that presents an interface into message-oriented middleware such as IBM MQSeries, SonicMQ and so on. Like JDBC, JMS provides Java applications a mechanism to integrate with such systems by presenting a common programming interface irrespective of the underlying messaging system. Functionally, JMS allows messages to be sent and received using a point-to-point or publish/subscribe paradigm.

The steps to running a J2EE application - build, package and deployment

There are several steps involved with building and running a J2EE application. The first step is to build it. J2EE is now supported by many tools (both commercial and open source) and these can certainly be useful in removing some of the complexity involved during the development process.

Once you have built your application, the next step is to package it up. Here, the type of J2EE component you have used will determine how you package up your application. At a high level though, this is really just a matter of bundling up all of your components and classes into an archive (for example JAR) file. In addition to your compiled code, some of the characteristics associated with certain components need to be configured using deployment descriptors. These are simply XML files that describe the way that the component will behave when running within the J2EE environment.

The final step in the process is called deployment. Since J2EE is, in essence, a collection of APIs and specifications, it is necessary to install an application server - the software that implements these specifications and provides the services that your J2EE components rely upon to run.

Where do I get an application server from?

There are many commercial application servers available on the market, including BEA WebLogic, IBM Websphere, Oracle 9i Application Server and so on. There are also some excellent open source implementations available such as JBoss. Finally, for non-commercial uses, the J2EE development kit is available free of charge. This is Sun Microsystems' reference implementation of the J2EE specifications, meant specifically for the development, testing and non-commercial deployment of J2EE applications.

If you only plan to use Java Servlets and JSPs...
If you are only planning on writing web-based applications using Java Servlets and JSP, it's not strictly necessary that you need to run a full J2EE application server. Many application server vendors supply cut-down implementations that only support servlets and JSP and there are also many open source implementations available too, with one of the most popular being Tomcat. This is the reference implementation of the Java Servlet and JSP specifications, and is available to download from the Jakarta site.

How do I get started with J2EE?

Step 1 : Install the JDK

The first thing that you'll need to get started with J2EE is the regular Java Development Kit (JDK). Ideally, you should try to get the latest version that you can to ensure the best compatibility and absence of bugs. Currently, this is version 1.4.1 and is available to download from the J2SE downloads page.

Step 2 : Install the J2EE SDK

Next you'll need an application server so that you can run your J2EE applications. The J2EE development kit (J2EE SDK) which is currently at version 1.3.1 can be downloaded from the J2EE downloads page.

How do I run the J2EE SDK on Microsoft Windows 95/98/Me?
With respect to Microsoft Windows, although the J2EE SDK is designed to run on NT/2000/XP, it can successfully be run on other versions such as 95/98 and Me. Take a look at this thread on the Java Developer Connection for detailed information.

Step 3 : Start the J2EE server

For the purposes of illustrating how to start up the J2EE server, let's assume that you're using a Microsoft Windows platform and have installed the JDK into c:\j2sdk1.4.1, and the J2EE SDK into c:\j2sdkee1.3.1. To start the J2EE server, open up command window and type the following: After a short delay and a few messages, you should see "J2EE server startup complete".

Step 4 : Start the J2EE deployment tool

Now that the J2EE server is up and running, the next thing to do is to start the deployment tool. Once you have built a J2EE application, this is the tool that you use to deploy it into the J2EE server. To run the deployment tool, open up another command window and type the following: After a short delay you should see a splash screen followed shortly afterwards by the main application window.

Step 5 : Test the installation

A useful, final step that we can perform is to test that everything is working correctly by pointing a web browser to http://localhost:8000/index.html. If successful, you'll see the J2EE server home page indicating that everything is working okay. At this stage you're now ready to start building and running J2EE applications.

Where can I find more information about J2EE?

The J2EE home page is a good place to start, as is TheServerSide.com and, of course, if you have a specific question, don't forget to join us over at the JavaRanch J2EE and EJB forum. Also, take a look at the J2EE section of The Bunkhouse for a list of related books, reviews and recommendations.

Another good starting point for building J2EE applications is the J2EE tutorial, covering how to get started writing Java Servlets, JSPs, EJBs and other J2EE components.

Summary

The J2EE is a very comprehensive platform and at first the range of technologies and APIs can seem daunting. By building up your knowledge of J2EE piece by piece, technology by technology, you'll soon be a good position to start designing and building J2EE systems. Next month we'll start our tour of the various J2EE technologies by taking a closer look at the web tier (Java Servlets and JSP) including a discussion of how they fit in to the J2EE, how Java Servlets and JSP coexist and also the sort of things that they are both capable of.