CONTENTS | PREV | NEXT | INDEX J2EE BluePrints



4.3 Servlets and JSP Pages

In an environment where only servlet technology is available, servlets can handle complex logic processing, navigation paths between screens, access to enterprise data, and formatting the data into an HTML response. In an environment where both servlet and JSP technology is available, JSP pages should be used to handle almost all of these tasks.

The Java code used within JSP pages should remain relatively simple. Therefore, a developer should encapsulate complex tasks within custom tags and JavaBeans components. A sophisticated Web application can consist solely of JSP pages, custom tags, and JavaBeans components; servlets are rarely necessary.

In this section we will review the roles that Web components can play, when to use servlets, when to use JSP pages, and when either technology may be used.


4.3.1 Web Component Roles

Although a common view is that Web components are mainly used to provide an application's presentation, in the J2EE application programming model Web components can serve two roles: as presentation components and as front components. Presentation components generate the HTML/XML response that when rendered determines the user interface. A JSP page acting as a presentation component may contain reusable custom tags or presentation logic. A presentation component could also be a servlet that produces binary data, such as an image. Front components don't do any presentation, but rather manage other components and handle HTTP requests or convert the requests into a form that an application can understand. Front components are useful because they provide a single entry point to an application, thus making security, application state, and presentation uniform and easier to maintain.

Figure 4.1 illustrates the basic mechanism. The front component accepts a request, then determines the appropriate presentation component to forward it to. The presentation component then processes the request and returns the response to the front component, which forwards it to the server for presentation to the user.

Figure 4.1 Web Component Roles

4.3.1.1 Front Components

While the sample application uses a JSP page as its front component, you could also use a servlet. The JSP page simplifies the initialization of the Web-tier JavaBeans components used by the sample application. However, a servlet could also perform this initialization.

The sample application front component parses all form data posted to the page and generates the events that result from the posted data. The events generated by the front component are forwarded to a template presentation component.

Front components perform the function of a controller when used in an MVC architecture (see Section 4.6.3).

4.3.1.2 Presentation Components

Many Web applications have a shopping cart that contains the products that a user has selected for purchase. In most applications the content of the shopping cart needs to be displayed repeatedly. JSP technology can be used to iterate through the list of items maintained in a shopping cart (implemented as a JavaBeans component) and display the contents to the user.

The code that generates the shopping cart display should be maintainable by content providers. Since the shopping cart JavaBeans object and JSP page that generates the HTML representation of the shopping cart can also be used in more than one part of an application, the presentation components used to generate the HTML representation of the shopping cart should also be modular and reusable.

Modular design allows separation of roles. Content providers can specialize in how content is displayed, and component developers can focus on the logic that is used in the JavaBeans component to manipulate the shopping cart data, and on the JSP page that generates the HTML representation of the data. Note that the data that is presented to the user may be taken from multiple sources.

Other requirements that presentation components must address are creating a consistent look and feel for an application while providing mechanisms for personalizing the user interface. For example, consider a Web site that has a personalized banner, a navigation menu that displays only information that a user wants to see, and the content a user wants to see. The next section describes how to design a JSP page or set of JSP pages that allow for a consistent look and feel throughout an application.


Presentation Component Templates

Figure 4.2 illustrates an application in which all pages share a common banner, navigation menu, body, and footer. Each item in the example can be seen as a component that is used to generate the final look and feel, can contain dynamic information, and should be customizable. This is the kind of page design that can benefit from the use of JSP templates.

Figure 4.2 Presentation Components

There are two ways of constructing the page shown in Figure 4.2. Depending on the granularity that you want your application to have, you could either build the page using custom tags and JavaBeans components or you could break up each portion into separate JSP pages each containing the necessary custom tags and JavaBeans components needed to generate their portions of the content, then build the whole page from a JSP page that incorporates the others using runtime includes.

Code Example 4.1 contains the template used to provide the screen components depicted in Figure 4.2. The template is constructed of an included JSP page (ScreenDefinitions.jsp), and the custom tag j2ee:insert. The content and pages are described in the ScreenDefinitions.jsp file. The template uses the insert custom tag to do runtime includes of the components needed to build it. See Section 10.3.2.1 for more discussion of the sample application's template mechanism.


<%@ taglib uri="Web-INF/tlds/taglib.tld" prefix="j2ee" %>
<%@ include file="ScreenDefinitions.jsp" %>
<html>
  <head>
    <title>
      <j2ee:insert template="template" parameter="HtmlTitle" />
    </title>
  </head>
  <body bgcolor="white">
    <j2ee:insert template="template" parameter="HtmlBanner" />
    <j2ee:insert template="template" parameter="HtmlBody" />
  </body>
</html>
Code Example 4.1 JSP Page Templating Mechanism

This example illustrates a clean separation between presentation logic, data, and content. There is no Java code in this page, so it could be managed by a content provider not familiar with the Java programming language.

We recommend using JavaBeans components or custom tags to do data rendering. These can be created by a developer familiar with the Java programming language. If JavaBeans components and custom tags are designed in a general manner, they should be reusable in other portions of the application or in other applications.


4.3.2 Servlets

Although JSP pages can be used for most purposes, there are some circumstances where servlets are more appropriate. The following sections describe common uses of servlets.

4.3.2.1 Generating Binary Data

Servlets are well suited for dynamically generating binary data such as images or a new content type. Requests for content of that type would be mapped to servlets that know how to generate the content, but from the Web client's point of view, it is merely requesting delivery of an ordinary image. The only assumption that need be made about the client is that it supports the image format being generated.

One example of this would be a servlet that generates a graph summarizing stock performance from data retrieved from a database or other source. This image can be kept in memory and updated every minute or so as needed. Using a servlet to generate the data, then keeping the data in memory for ready display, can save time and improve performance in both execution cycles and file access time.

4.3.2.2 Extending a Web Server's Functionality

Servlets are a portable mechanism for extending the functionality of a Web server. For example, if a new data format must be supported, a servlet can be mapped to the file type for the format.

A good example of a servlet that extends a Web server is the servlet that is mapped to JSP files. This servlet parses all files that end with a jsp file extension and compiles the JSP pages into servlets. The resulting servlets are then executed by the Web container and the resulting response is sent back to the client.


4.3.3 JSP Pages Versus Servlets

Depending on the composition of your development team, time constraints, and application architecture, your use of JSP pages and servlets will differ. Both technologies have merits and should be used accordingly. In some cases there is not a single correct choice of whether to use a servlet or JSP page.

Servlets are a programmatic tool and are best suited for low-level application functions that don't require frequent modification.

JSP pages are a presentation-centric, declarative means of binding dynamic content and logic. JSP pages should be used to handle the HTML representation that is generated by a page. They are coded in HTML-like pages with structure and content familiar to Web content providers. However, JSP pages provide far more power than ordinary HTML pages. JSP pages can handle application logic through the use of JavaBeans components and custom tags. JSP pages themselves can also be used as modular, reusable presentation components that can be bound together using a templating mechanism.



CONTENTS | PREV | NEXT | INDEX
Copyright © 2001 Sun Microsystems, Inc. All Rights Reserved.