Back Contents Next

Design Considerations

Design considerations

JSP syntax is compact and uses the familiar format of tags and attributes, so learning JSP on the level of syntactic correctness is easy. The challenge is to find good ways of using JSPs in the overall system: the technology is very young and good practices have not yet been established. Paradoxically, the versatility of JSPs may hinder, rather than promote, good system design. There is a natural temptation to use a JSP page simultaneously as a servlet, a backend processor, and a template file for output, simply because you can. This can easily result in monolithic JSP pages that are difficult to understand, debug and maintain.

 

One evidently correct practice is to unload the backend processing part to Java beans, minimizing the amount of code fragments (scriptlets) in the page. Going a step further, it is also possible to separate the servlet functionality from the template file functionality, using the JSP page only as an output template. This idea has been very clearly articulated by Craig McClanahan, a frequent contributor to the JSP-INTEREST list; some of his contributions end up as entries in the JSP FAQ at http://www.esperanto.org.nz/jsp/. We advise you to have a look at it.

 

Conversations with the Client

We are, however, interested in the opportunities offered by the JSP page that functions both as a servlet and an output template. In particular, this combination allows for a very compact and elegant conversation between the JSP page and the client. The idea is that a JSP page functioning as a servlet uses include directives to include output template pages, while each output template page contains a form whose ACTION attribute is the JSP page functioning as a servlet.

 

Here is how it works in the Birthday application. The main page of the application, Birthday.jsp, uses a bean, BirthdayBean, whose properties include bbcmd and jspcmd:

 

  String bbcmd=null; //BirthdayBeanCommand from JSP specifies action

         // login; dodb; send; logout;

  String jspcmd=null; // JSPCommand from BirthdayBean specifies display

        // birthdaylist; list; msgsent; change; error; logout

 

Birthday.jsp, at some point, calls the doCommand() method of the bean, whose operation depends on the value of bbcmd. The doCommand() method sets the jspcmd property. That property determines which of several supporting JSP files gets included in the response page. If the jspcmd is not logout and does not result in an error, then the response includes a form, which includes a select element whose name is bbccd. That value of that select element becomes the value of the bbcmd property of the bean. We've come full circle and are ready to call doCommand() again:

 

 

At this point, you might be interested in what the Birthday application does with all of this. Given the preceding content of the book, it shouldn't be surprising that it accesses a database and makes the results available to human users. The new element (apart from JSP pages and beans) is that the results are made available via electronic mail.

 

Back Contents Next