Web Application Architecture

As you have seen JSPs have several advantages over servlets. They

  • Are quicker to write and develop

  • Focus on the page layout and delegate Java logic to supporting JavaBeans and custom tags (as will be discussed on Day 14)

  • Differentiate the Web page presentation (HTML) from the underlying logic (Java)

  • Can be written by non-Java–aware developers

  • Support a standard error reporting mechanism using the error page directive

However, JSPs do have some downsides:

  • Error identification and correction is complicated by the translate and compile lifecycle.

  • The syntax for manipulating JavaBean objects is clumsy.

  • With JSP 1.2 large volumes of embedded Java scriptlets reduce the maintainability of the page. This problem has been alleviated by the introduction of EL and JSTL.

The speed of development and the quick turnaround on look and feel or simple functional changes that is possible with JSPs is a major advantage in modern Web-based applications. Consider using servlets only when the Java code is complex or needs to be “hand crafted” for efficiency.

The current thinking on using servlets and JSPs is to combine the two technologies in what is known as a Model 2 Architecture shown in Figure 13.7. The simpler approach of just using a servlets or a JSP is known as a Model 1 architecture (see Day 2).

Figure 13.7. Model 2 Web Application.


In a Model 2 architecture the incoming HTTP request is handled by a servlet which validates any HTTP request data and updates the database if necessary. Typically, Session beans will be used to support the servlet functionality. The Session beans in turn may use Entity beans to encapsulate access to a database. In a simple application not making use of EJB technology simple JavaBean helper classes will be used in place of the EJBs.

Once the servlet has verified the request and obtained the necessary data to formulate the response, the request is forwarded to a JSP for generating the Web page to return to the client. Typically the JSP will only read data from the Session bean so that the different roles of the servlet as a controller and the JSP as a presentation (or view) component are clearly encapsulated. Experienced developers will recognize this architecture as the Model View Controller (MVC) architecture; the EJBs take the role of the Modelcomponent.

The Model 2 architecture is so popular that products such as Apache Struts (http://jakarta.apache.org/struts) and Sun Microsystems' JavaServer Faces (JSF from http://java.sun.com/j2ee/javaserverfaces) provide frameworks for building Model 2-based Web applications.

The Agency case study example uses a model 1 architecture as it is a learning tool. In reality using a model 2 architecture for the Agency case study would show significant benefits in ease of development and maintenance.

An additional advantage of this servlet/JSP approach is that the servlet can use one of several JSPs to present the data according to the type of client making the request. You can define one JSP for HTML clients (PCs) and another for WML clients (mobile phones). In the future, you can add additional clients for, say, XML clients or a presentation language that has not yet been defined. All of these different presentation requirements can be supported by a single servlet. Any future changes to the underlying business rules are only made once in the logic of the servlet, with any necessary changes in the presentation being made in the appropriate JSPs.

Such a variant of the Model 2 architecture is known as a Model View Controller (MVC) architecture. In a J2EE world the Model is implemented using EJBs and JavaBeans, the View components are JSPs and the Controller is a servlet (or multiple servlets).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.79.45