Section 19.4. Using JSP with BudgetPro


19.4. Using JSP with BudgetPro

We could have taken the BudgetPro example from the previous chapter and simply translated it all into JSP files. The reason we didn't is that it's not how you are likely to find JSP used "in the wild." Since JSP files become servlets, it is not uncommon to find JSP and servlets mixed togethernot arbitrarily, but in a sensible way. Remember the Model/View/Controller (MVC) pattern from your readings on object-oriented programming and design patterns?[2] Well, JSP makes for a reasonable View, and a plain servlet can act as the Controller. The Model is typically the database behind all this. That's what we've done with the BudgetPro example.

[2] If not, then a) you should do some more reading, and b) the MVC pattern is a "classic" way to divide the work of a GUI into three distinct parts: Modelthe data behind what you are doing or displaying; Viewa particular way to display that data; and Controlleran object that acts as the "traffic cop" to various inputs and events, sending messages to either the View, or Model, or both.

We've taken the two main chunks of output codethat for the main account display and the form used for creating subaccountsand turned those into JSP files. The main servlet class (BudgetProServlet.java) is thus "gutted" of its output, and the new version (BudgetProController.java) acts as the controller. Requests come to it via HTTP requests, but for output, it redirects the browser making that request over to the appropriate JSP.

This introduces a new bit of servlet syntaxredirecting a request to another URL. The action is taken by means of a method call on the HTTP response object:

 response.sendRedirect("BPAcct"); 

Whereas in the previous, servlet version of BudgetPro, we would create an object that was the next page of output:

 nextPage = new AccountView(current); 

In this version, we instead redirect the response to a JSP that produces the output for that page.

So how does the JSP know for which account it should display information? That is shared between the JSP and the controller servlet via the session information. As with the previous, servlet-base BudgetPro, the session is used to store the current account. It can be retrieved from the session information, as seen in line 11 of BPAcct.jsp:

 11: <% Account acct = (Account) session.getAttribute("current"); 

That variable (acct) is then used throughout the JSP to get the appropriate data for display, as in:

 21: Account: <%= acct.getName() %> 

We could also have used a session JavaBean. Such a mechanism requires more setup on both sides, the controller and the JSP, but has the advantage of removing more literal Java code from the JSP. ("We leave this as an exercise for the reader!")



    Java Application Development with Linux
    Java Application Development on Linux
    ISBN: 013143697X
    EAN: 2147483647
    Year: 2004
    Pages: 292

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net