Download (direct link):
65 return salary;
Listing 24.6 (continued)
To deploy this tag library, you need to define it using a tag library descriptor (see Listing 24.7).
01: <?xml version = '1.0' encoding = ’windows-1252'?>
02: <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
07: <uri>javapitfalls </uri>
08: <info>Shows how to encapsulate the salary business logic in a 2 taglib.</info>
Listing 24.7 Salary.tld
JSP Design Errors 219
However, neither of these shows the tag library in action. All of the complexity is gone, so your developer has an easy task. Listing 24.8 is an example of a JSP using our tag library.
01 <%@ page contentType="text/html;charset=windows-1252"%>
02 <%@ page import="java.util.*"%>
03 <%@ taglib uri="javapitfalls" prefix="jp" %>
07 Salary Servlet Jsp
09 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset 2 - s w 0 ndo 1 w II
13 Salary Jsp
20 <!-- Down to this simple line -->
22 <jp:salary salary="<%= request.getParameter("salary") %>" />
Listing 24.8 SalaryTag.jsp
All of this modularization has not addressed the fundamental problem we found in our workflow system. While we successfully separated logic from presentation, we had not separated out the flow control of the application. This is critical in most Web systems, but particularly in our workflow system.
The solution to this problem is the Model 2 Architecture.
220 Item 25
Java Server Page Business Logic
Figure 24.2 Model 2 Architecture.
JSP Development in the Model 2 Architecture
Figure 24.2 shows the Model 2 Architecture. This architecture is a JSP/servlet implementation of the popular and powerful Model-View-Controller pattern. Included is the controller, which is a servlet that responds to a request and dispatches it to the appropriate business logic component. The JavaBean wraps the business logic that needs to be executed to handle the request. From there, the bean hands off to a JavaServer Page, which controls the presentation returned in the response.
It seems like a great idea, but is it realistic to expect Web applications to build this entire infrastructure, especially considering the history of ad hoc Web development? The good thing is that you don't have to build this infrastructure. The Apache Software Foundation's Jakarta project has a rather sophisticated implementation of the Model 2 Architecture, called the Struts framework (http://jakarta.apache.org/struts).
Item 25: When Servlet HttpSessions Collide
Picture this scenario. A user is on the Internet, shopping at Stockman's Online Hardware Store, where there is an electronic commerce user interface with "shopping cart" functionality. As he browses the Web site, he decides to put a belt sander, a drill press, and an air compressor in his shopping cart. Instead of checking out what he has put in his shopping cart, he decides that he'd better buy something for his wife to ease the pain when she discovers his hardware purchases. He quickly goes to "Lace Lingerie Online," where they also have an e-commerce interface. There, he builds a shopping cart consisting of "Sensual Bubble Bath," the "Romantic Lunch Box Package for Two," and the "Lace Gift Certificate." He checks out of the lingerie store, enters his credit card information, and leaves the Web site. Now that his conscience is cleared, the
When Servlet HttpSessions Collide 221
user goes back to the hardware store Web site. He clicks on the "Check out" button, but, to his surprise, his shopping cart at the Stockman Hardware store is filled with "Sensual Bubble Bath," the "Romantic Lunch Box Package for Two," and the "Lace Gift Certificate." The user is confused. Frankly, the folks processing orders at the hardware store are baffled and are calling the programmers who designed the Web site. What could have happened?
Believe it or not, that could be a feasible user scenario when this pitfall relating to the HttpSession class is introduced into Java servlets. HttpSession is a wonderful class for persisting state on the server. It is an interface that is implemented by services to provide an association (session) between a browser and the Web server's servlet engine over a period of time. Using the HttpSession class, you can store a great deal of information without the use of browser client-side cookies and hidden fields. In fact, you can store very complex data structures of information with the API of the HttpSession, which is shown in Table 25.1.