Make your own free website on Tripod.com
 
The Server-side Java™ Magazine - September 1998 Issue
Servlet Central It's the Coupling Stupid: JSP and JavaBeans
Dr. Gary L. Craig, President, Superlative Software Solutions, Inc.
craig@cat.syr.edu
Shortcut :
 

ServletExec
 

In political campaigns in the United States during the 1990s, a familiar slogan has rung true. "It’s the Economy Stupid", demonstrates what is important in keeping voters happy. Take care of the economy and the rest will follow.

Software systems have a similar critical concern, system component coupling. If you minimize coupling, maintenance, testing, and scalability follow. This is at the heart of Java-based server-side scripting technology known as JavaServer Pages (JSP). Although server-side scripting is about building code that will be executed on a web server, JSP is at the center of a robust web application programming model. 

A typical client request received at a web (application) server triggers execution to retrieve or compute part of the response content. The complete response is the composition of this "dynamic content", static content such as images, text, etc., and formatting content which defines the presentation of the result. 

Figure 1 shows the overview of this programming model. An incoming request from a browser is responded to by a servlet that controls the request processing within the Application Server. Next, this controller selects one or more JavaBeans (and/or EJBs) which provide access to the application’s business logic. The Beans then perform or orchestrate the performance of the necessary computation which results in the dynamic content to be presented back to the client. Next, the controller, will select the appropriate output "presentation" template, a JavaServer Page (JSP), and invoke it to display the dynamic content. The JSP merges the dynamic content (available via Bean properties) into an HTML page template. This result page ("view") is then delivered as a response back to the Browser. 

This programming model provides a clean separation of presentation from business logic. Further, the presentation is managed primarily through HTML and traditional web page editors/tools, while the business logic, written or access via Java are delivered as reusable components.

In this way, the presentation of dynamic content is independent of the logic and the environment which produces it. Implementation of the business logic can change independent of the page and layout style. Just as important, the skills required to provide high quality presentation, can be focused on producing a deliverable (JSP) which is devoid of details about business logic, i.e., not polluted with involved server-side scripts.
 
 

Referencing Beans from JSP

A JSP consists of HTML and bits of Java code. In this way JSP is very similar in structure to most server-side scripting files. In most of these technologies, an attempt is made to separate out most of the code, either by defining it in functions or in separate components. In JSP, the preferred technique is to provide the logic in JavaBeans. The body of the JSP must have access to these Beans. Access is provided via the <USEBEAN> tag. (The <USEBEAN> tag was introduced in the JSP Specification version 0.92. At the time this article was written, no Application Servers yet support the 0.92 Spec. In the 0.91 version of the Specification, access to JavaBeans was provided by the <BEAN> tag, which provided equivalent expressiveness.)

The form of the <USEBEAN> tag is:

<USEBEAN NAME="nameofbeaninstance" TYPE="typeofbean" LIFESPAN="page|session|application">

and </USEBEAN>

In the typical use, a Bean of type "typeofbean" will have been stored under key "nameofbeaninstance", in a one of three well known object, 

  1. The current request context (representing the current client request), 
  2. The current HttpSession object, or 
  3. The current Application scope. 
The scope in which the Bean can be found is specified via the LIFESPAN attributed of the USEBEAN tag.

For example, consider the following:

<USEBEAN NAME=customer TYPE=com.3Si.CustomerBean LIFESPAN=session>

</USEBEAN>

This results in accessing a Bean of type com.3Si.CustomerBean from the current session object using the key "customer". In executable code:

HttpSession session = request.getSession(true);

com.3Si.CustomerBean customer = 

(com.3Si.CustomerBean)session.getValue("customer");

The remainder of the JSP now has access to this variable "customer.

In between the <USEBEAN> and the </USEBEAN> tags, may appear nothing or a combination of zero or more <SETFROMREQUEST> and <SETONCREATE> tags. The <SETFROMREQUEST> enables the Bean’s Properties to be set from "Request" parameters should their respective names match. <SETONCREATE> is used to specify initialization parameters for a Bean which is "created" during the execution of the <USEBEAN> tag. A Bean is created when the named Bean is not found in the appropriate lifespan
 
 

JSP as HTML Template

A "view" (presentation) JSP is likely to be created via tools, e.g., today’s Web page editors. The vast majority of the executable Java code associated with such a page requires first access to one or more Beans, and then access (for display purposes), to one or more of the Beans properties. The latter code could be written explicitly via a JSP expression, e.g.

<%= customer.getSomeProperty() %>

Although very compact, and very readable by Java programmers, such code embedded in what otherwise is HTML – may not be obvious to those persons which create the page in an "extended" page editor.

For this reason, a pair of HTML-like tags have been added to the JSP Specification to enable page creation tools to generate more declarative syntax. One of these, the <DISPLAY> tag, has the form:

<DISPLAY PROPERTY="beanname:propertyname" 

[PLACEHOLDER="alternatevalue">

This might be used in the following way:

<IMG SRC="<DISPLAY PROPERTY=customer:background>" 

ALT="Background Image">

(Look for the "beanname":"property" syntax to change slightly for the final 1.0 version of the JSP specification.)

This architecture permits moving all business logic away from the presentation logic (which for now is described primarily in HTML). JavaBeans promote software reuse and can easily be managed within Java development and management environments.

JSP promotes this architecture by providing a mapping between simple Bean declarations and Property accessors and the corresponding execution semantics.
 
 

Scripting Interaction Controllers via JSP

JSP is also a complete Java scripting environment. This makes it possible to write fairly simple Java Servlets, easily, through a scripting tool. Although at first glance this use of JSP would appear to be less than ideal, many "script writers" can be provided with the tools (templates) to easily write these front-end servlets.
 
 

Summary

With the availability of Enterprise Java Servers, JSPs will play an increasingly important role in the development and management of web applications. The Java-based web application implementation architecture is sure to increase productivity, robustness, and maintainability of the next generation of web applications.
 
 
 
 
 
Dr. Gary L. Craig is president of Superlative Software Solutions, Inc.

ServletExec

Click here to return to the home page of Servlet Central


 
 

Shortcut :
©Copyright 1998-1999 Servlet Central Publishing