Web Tier Technologies

JSPs and Servlets

Servlets are the standard Java mechanism for creating HTTP applications. Each servlet has a service() method for processing request and response objects. Servlets have abstract mechanisms for maintaining sessions and managing cookies.

Servlets are hosted in a "servlet container". The servlet container calls the init() method before placing placing a servlet into use and calls the destroy() method before taking it out of use. It calls the service() method for each HTTP request submitted to the web server. The container manages the session and application context. It handles filters, tag libraries, and listeners.

JavaServer Pages (JSPs) are a text files that are dynamically compiled into servlets. The format is similar to HTML and XML, but with embedded scriptlets and custom tags. Although servlets can serve up HTML directly, it may be better to put presentation code into JSPs and control logic into servlets.

A web application where each servlet (or JSP) handles both business logic and presentation is called a Model 1 Architecture. It is simple, but constrained. An application that uses servlets to execute business logic and redirect page flow, but uses JSPs for presentation is called a Model 2 Architecture.

Model 2 is a form of the MVC design pattern. The MVC pattern splits design concerns into the persistent model behind an interface and the displayed user view. A control mechanism is defined that manipulates the model and communicates between view and model. This pattern maps well to GUI widgets in a windowing environment. It is somewhat of a stretch in web applications, since in a web browser, all control signals pass through HTTP, and responses to the view aren't dynamic.

Web Frameworks

The plain servlet/JSP mechanism is a little bare. Its model is similar to old-fashioned CGI scripts. Web frameworks place a more abstract layer over the servlet API. They may provide an MVC model, or at least simplify repetitive tasks. Some provide sophisticated widgets and AJAX tools that are hard to build from scratch.

Benefits of frameworks are that they reuse much code, so there is less for developers to write and maintain. They provide a higher-level API for constructing user interfaces. They (hopefully) provide best-practices models for developing web applications. By separating concerns, they may make it easier to change styles and workflow, or may make refactoring easier. By standardizing some of the development, they may simplify maintenance of multi-person projects.

There are drawbacks to frameworks. They are an additional learning curve, and may not be necessary for simple projects. They may mandate a programming model that is inappropriate for the given project. They must be maintained separately, and may lock development in to certain infrastructure software.

JavaServer Faces

JavaServer Faces (JSF) is a web framework standardized by Sun that provides a GUI component model. The display components provide sophisticated, dynamic controls. JSF supports WYSIWYG design tools, so pages can more easily be written by graphic designers, rather than by programmers.

It supports an event-driven model of execution, similar to GUI widgets in windowing environments. This matches better to the classic MVC design pattern.

A typical JavaServer Faces application couples a backing bean with each page in the application. The backing bean defines properties and methods that are associated with the UI components used on the page. Each backing bean property is bound to either a component instance or its value. A backing bean can also define a set of methods that perform functions for the component, such as validating the component's data, handling events that the component fires, and performing processing associated with navigation when the component is activated.

A property can be any of the basic primitive and numeric types or any Java object type for which an appropriate converter is available. You can also use a converter to convert the component's value to a type not supported by the component's data.

Annotations and JSF deployment configuration

Some JSF properties can be hard-coded into Java classes through annotations. All configurations can also be set up on the faces-config.xml file. This file can set up all managed beans, converters, and validators, as well as navigation rules between pages.

annotation target description
@ManagedBean class Designate a class as being a managed backing bean
@ManagedProperty field Configure a class variable to be managed, including names, values and scope.

JSF life cycle

When processing an HTTP request, JSF goes through these steps:

  1. Restore view
  2. Apply request values
  3. Process validations
  4. Update model values
  5. Invoke application
  6. Render response

Web-centric versus EJB-centric implementation

When designing a solution, you should consider the extent to which EJBs should be used. EJBs provide value, but sophisticated applications can be built entirely without them.

In the Web-centric design, the Web tier components are responsible for most of the application's functionality. The Web tier components communicate directly with the EIS tier using container services such as the JDBC API. In the EJB-centric design, the enterprise beans encapsulate the core application logic. Web tier components communicate with EJB tier components instead of accessing the EIS tier directly. A web-centric design may still use the Model 2 decomposition in which JSPs provide presentation while servlets execute business logic. In JEE 5, servlets may use the dependency injection functions that are available to EJBs. Web-centric applications can run on lighter application servers, such as Tomcat, Jetty, or GlassFish prelude.

An EJB-centric design is more work, although EJB 3 provides a great improvement over EJB 2. EJBs provide thread safety, as well as container managed transactions, security, and scalability. EJBs are a better tool for large, complex, and distributed applications. Also, EJBs provide better support for fat clients, since workstation clients may communicate directly to the EJBs.

Sub