in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 75 76 77 78 79 80 < 81 > 82 83 84 85 86 87 .. 166 >> Next

64 {
65 return salary;
66 }
68 }
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" "">
03: <taglib>
04: <tlibversion>1.0</tlibversion>
05: <jspversion>1.1</jspversion>
06: <shortname>salary</shortname>
07: <uri>javapitfalls </uri>
08: <info>Shows how to encapsulate the salary business logic in a 2 taglib.</info>
09: <tag>
10: <name>salarytag</name>
11: <tagclass>mypackage.SalaryTag</tagclass>
12: <bodycontent>empty</bodycontent>
13: <attribute>
14: <name>salary</name>
15: <required>true</required>
16: <rtexprvalue>true</rtexprvalue>
17: </attribute>
18: </tag>
19: </taglib>
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" %>
04 <HTML>
05 <HEAD>
06 <TITLE>
07 Salary Servlet Jsp
08 </TITLE>
09 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset 2 - s w 0 ndo 1 w II
10 </HEAD>
11 <BODY>
12 <H1>
13 Salary Jsp
14 </H1>
15 <table>
16 <tr>
17 <td><b>Employee</b></td>
18 </tr>
20 <!-- Down to this simple line -->
22 <jp:salary salary="<%= request.getParameter("salary") %>" />
24 </table>
26 </BODY>
27 </HTML>
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 (
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.
Previous << 1 .. 75 76 77 78 79 80 < 81 > 82 83 84 85 86 87 .. 166 >> Next