JSP in Enterprise Applications

In Chapter 8, we discussed the use of boundary objects to capture and isolate interaction between a use case and external entities such as with other sub systems and with users. JSPs provide an effective yet simple vehicle for the interaction of the latter type, that is, with users of the system.

For instance, consider the HomeDirect use cases identified in Chapter 16. Each use case that interacts with the customer actor means that some sort of user interface is required. There is also some form of interface required for the Login/Logout use cases, which are included by the other use cases.

To understand how JSPs are used in enterprise apps, let's start with the Login use case. A login Web page, representing the login boundary object, would be appropriate for presentation to the user when the user attempts to use the banking system initially and initiates the Login use case. Additionally, there is a need for the user to enter a username and password via the Web page for validation by the system.

A controller is also required for the Login use case, as identified via the login control object during the analysis. Because we want to have the ability to change the presentation easily, we have chosen to follow the Model 2 architecture for our application and will use the JSPs primarily for presentation rather than as replacements for servlets.

In light of this decision, we will create a servlet to handle the processing of the login form. Although use case control objects often merge at design time, this use case acts as a “gatekeeper” of sorts in the sense that none of the other use cases can be executed until this one is successful. Given this prerequisite, it is appropriate to keep the login validation isolated from the rest of the application.

Figure 11-7 shows the overall structure of the Login use case.

Figure 11-7. Login use case design


Let's take a quick walkthrough to clarify what is occurring in Figure 11-7. The Login.jsp is the entry point into the use case. It builds the login client, which includes a LoginForm to allow the user to enter information. When the form is filled in and submitted by the user, the LoginServlet processes it by interacting with the appropriate entity objects to verify the information entered (we discuss the entity objects mapping to the solution domain in Chapter 13). If the login fails, the login servlet displays an error message, and then restarts at Login.jsp. If the login is successful, it is forwarded to another entity (not shown).

Note the use of Banner.jsp and Footer.jsp. Although we could have included the information directly into individual JSPs, we have chosen this approach as it permits better reuse across the entire application and also serves to keep these details isolated.

A common technique in Model 2 is to use a JavaBean as a means of passing information between a servlet and a JSP. The idea is for the servlet to obtain and set the information in a JavaBean, and then forward the request on to the JSP. The JSP in turn uses the JavaBean to obtain and publish the information to the end user of the information. We use this technique for communication between the centralized controller for the remaining use cases and the various pages for displaying the results to the end user.

Figure 11-8 shows an example of this technique in the context of the List transactions use case.

Figure 11-8. Using JavaBeans to share information


In the figure, Main.jsp provides the anchor page for invoking the various commands available to HomeDirect users. Any commands invoked by the user are submitted to the MainServlet, which then coordinates the activities with the control and entity objects (not shown but discussed in detail in Chapter 12 and Chapter 13, respectively). When MainServlet has gathered all the information required for the response, it places the information in the JavaBean and forwards the request on to the JSP. The JSP then accesses the bean using the jsp: useBean tag and publishes it to the end user.

Figure 11-9 illustrates the dynamics associated with this scenario via a sequence diagram.

Figure 11-9. Sequence diagram detailing the login scenario


The code fragment associated with setting up the TransactInfo bean for use by the ListTransacts.jsp is shown in Figure 11-10.

Figure 11-10. Using a JavaBean to share information with a JSP
// Get the user session
HttpSession session = request.getSession(true);
...
// Find collection of transacts
...
// Create bean to pass info to JSP page
TransactInfo transactInfo = new TransactInfo(transacts);
session.setAttribute("TransactInfo", transactInfo);
...
// Forward to next JSP page
RequestDispatcher dispatcher =  getServletContext().getRequestDispatcher("/ListTransacts
.jsp");
dispatcher.forward(request, response);

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

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