Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Process JSPs effectively with JavaBeans

Transport JSP processing logic into a JavaBean with the Template Method design pattern

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
JavaServer Pages (JSP) technology offers many features for easy and quick development of Web applications. However, if you use those features without some planning and structure, your JSP code can quickly become a mix of various HTML tags, JSP tags, and Java code that is difficult to follow, debug, and maintain.

The objective here is to have JSP code that resembles HTML to the greatest possible degree, by moving all processing code into JavaBeans. The benefit of that approach is that the HTML programmers and graphic designers can do the presentation development (using one of many HTML editors) while you, the Java programmer, can do the programming logic. In addition, the approach makes it easy for you to provide different look and feels for the same Web application.

The framework I'll present here uses the Template Method pattern to enforce common design across the entire application and implement common behavior in each JSP. The common behavior includes, but is not limited to, page state management, common page processing, common error processing, and a mechanism for sharing information between pages. Those are all defined only once, leaving you to deal only with the page-specific details.

I'll present a simple "voting" application as an example of how you can use the framework. You should have basic JSP and Java knowledge, and some UML knowledge is desirable but not mandatory.

Static structure overview

This section gives an overview of the framework core participants, as well the example voting application. Figure 1 shows a UML diagram of the framework's structure:

Figure 1. UML Class Diagram
Click on thumbnail
to view full-size image.



The framework's central piece consists of two common JSP include files and two classes, described below. Their role is to carry out the common behavior.

  • includeheader.jsp: Include JSP file that must be statically included in the beginning of each JSP.
  • includefooter.jsp: Include JSP file that must be statically included at the end of each JSP.
  • AbstractJSPBean: Abstract class that you should use as a super type for all JSP JavaBean classes. This is the framework's core class.
  • SharedSessionBean: Used to provide associations between all JSP JavaBean objects within one HTTP session.


The purpose of a JSP Webpage is solely presentation. Each JSP must have a corresponding JavaBean that performs the page-specific logic. Each JSP page must statically include includeheader.jsp and includefooter.jsp. Each JavaBean must extend the AbstractJSPBean, which contains the template methods that carry out the common behavior.

The voting application consists of the following JSPs and their corresponding JavaBeans:

  • login.jsp, LoginJSPBean: Authenticates and logs in the voter
  • vote.jsp, VoteJSPBean: Performs the voting
  • confirmation.jsp, ConfirmationJSPBean: Displays confirmation and voting results


I will not examine the classes that emulate database and business logic (Voter, Candidate, and VoteDB) in great detail, but they are required for the example to function properly.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources