[ Team LiB ] Previous Section Next Section

Sample JSP Page

Let's look at an example using many of the JSP features we discussed. Display.jsp, shown in Listing 15.4, displays a JSP that may be used within the GAMS application to display data for an item up for bid. In line 2, we import our supporting classes as before. Note the use of the include action in line 3. This facility enables us to insert code contained within the header.jsp file (see Listing 15.4) directly into the current JSP. Lines 12–33 implement logic using JSP scriptlets. We'll look at encapsulating program logic into tag libraries in Chapters 17 and 18. Line 7 demonstrates use of the request object. This structure allows a page to be implemented by a request originating from a servlet or another JSP page (the item data being passed within the request)—again coding for use within an n-tiered architecture.

Listing 15.4 DisplayItem.jsp
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <%@ page import="com.gams.ejbs.item.*, java.text.*, java.sql.Date" %>
3 <jsp:include page="header.jsp" flush="true"/>
4
5 <html>
6 <head>
7   <title>Global Auctions - Display Item</title>
8 </head>
9
10 <body>
11
12 <%
13   SimpleDateFormat df = new SimpleDateFormat("MMM d, yyyy h:mm a");
14   NumberFormat nf = NumberFormat.getCurrencyInstance();
15
16   //ItemValue iv = (ItemValue)request.getAttribute("itemdata");
17   ItemValue iv = new ItemValue(new Integer(101), "Ming Chair", new Float(550.50), new
graphics/ccc.gif Date(102, 06, 30));
18
19   float currentBid = iv.getCurrentBid().floatValue();
20   long minimumBid = iv.getMinimumBid();
21
22   float minimalBidAllowed = 0;
23
24   // compute minimal acceptable bid
25   if (currentBid >= minimumBid)
26       minimalBidAllowed = currentBid + .5f;
27   else
28       minimalBidAllowed = minimumBid;
29
30   String strCurrentBid = nf.format(currentBid);
31   String strMinimalBid = nf.format(minimalBidAllowed);
32   String endbiddate = df.format(iv.getEndDateTime());
33 %>
34
35 <table>
36 <tr><td align="left"><br><br><b>Display Item: </b><%=iv.getItemId()%></td><td></td></tr>
37 <tr><td width="200">Title:</td><td><%=iv.getTitle()%></td></tr>
38 <tr><td>Current Bid:</td><td><%=strCurrentBid%></td></tr>
39 <tr><td>Auction Ends:</td><td><%=endbiddate%></td></tr>
40
41 <% if (session.getAttribute("IsBuyer") == null) { %>
42   <tr><td colspan="2" align="left"><b>To bid on this item, please login as a buyer</b><
graphics/ccc.gif/td></tr>
43
44 <% } else { %>
45   <form action="PlaceBidServlet" method="post" name="bidform" id="bidform"
graphics/ccc.gif onSubmit="return checkform()">
46   <input type="hidden" name="itemid" value="<%=iv.getItemId()%>">
47   <input type="hidden" name="itemtitle" value="<%=iv.getTitle()%>">
48
49   <tr><td>Bid Amount:</td><td><input type="text" name="bidamount" size="8">&nbsp;
graphics/ccc.gif(Minimum <%=strMinimalBid%>)</td></tr>
50
51   <% if (iv.getQuantityAvailable().intValue() == 1) { %>
52   <input type="hidden" name="bidquantity" value="1">
53   <% } else { %>
54  <tr><td>Bid Quantity:</td><td><input type="text" name="bidquantity" size="5">&nbsp;
graphics/ccc.gif(Maximum <%=iv.getQuantityAvailable()%>)</td></tr>
55   <% } %>
56
57  <tr><td colspan="2" align="center"><input type="submit" value="Place Bid"></td></tr>
58   </form>
59 <% }%>
60
61 </table>
62
63 </body>
65 </html>

NOTE

The code presented is intended to show examples of JSP syntactic elements. Proper implementation of MVC architecture would implement such logic shown within the Control layer (using an EJB, taglib, JavaBean, or other constructs). Efficient JSP pages should contain little if any implementation logic and no hard-coded data.


Finally at lines 35–59, the example in Listing 15.4 uses JSP expressions to populate tags within an HTML table.

Let's look at the included file header.jsp, shown in Listing 15.5. Note lines 7–9. This is another example of using implicit objects; in this case, the session object. The session object is used to get values that have been put into the session at some point prior to viewing this page—a convenient way to make data available across JSP pages within an application. Note the use of scriptlets to condition references to a logout servlet and a login JSP in lines 9–15.

Listing 15.5 header.jsp
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
3 <table>
4 <tr><td>
5 <h1>Welcome To Global Auctions<br></h1>
6
7 <% if (session.getAttribute("CustomerName") != null) { %>
8   Hello <%=session.getAttribute("CustomerName")%>
9   <% if (session.getAttribute("IsSeller") != null) { %>
10   &nbsp;&nbsp;&nbsp;&nbsp;<a href="/seller/displaySellerFees.jsp" target="main">Display
graphics/ccc.gif Fees</a>&nbsp;&nbsp;&nbsp;&nbsp;
11   <% } %>
12   <a href="LogoutServlet" target="main">Logout</a>
13 <% } else { %>
14
15 <a href="login.jsp" target="main">Login</a> to Place Your Bid!
16
17 <% } %>
18 </td></tr>
19 </table>

Let's look at login.jsp as presented at Listing 15.6. Note the use of the request object in lines 10 and 11 to retrieve a request parameter. Also, in line 14 the processing of the actual login logic is delegated to a servlet, keeping with an n-tiered approach of implementing the GAMS application.

Listing 15.6 login.jsp
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
3 <html>
4 <head>
5     <title>Login</title>
6 </head>
7
8 <body>
9
10 <% if (request.getAttribute("ErrorMessage") != null) { %>
11    <b><%=request.getAttribute("ErrorMessage")%></b>
12 <% } %>
13
14 <form action="LoginServlet" method=post>
15 <table>
16 <tr><td colspan="2" align="center"><h1>Customer Login</h1></td></tr>
17 <tr><td width="200"><b>Global Auction Name:</b><br> (ex. AuctionGuy):</td> <td><input
graphics/ccc.gif type="text" name="gamsAlias" size="10"></td></tr>
18 <tr><td><b>Password:</b></td><td><input type="password" name="password" size="20"></td></tr>
19 <tr><td colspan="2" align="center"><input type="submit" value="Login" onClick="top
graphics/ccc.gif.header.location='header.jsp'"></td></tr>
20 </table>
21 </form>
22
23 </body>
24 </html>

To run the examples presented, place the JSPs in your domain's applications\DefaultWebApp directory. Deposit supporting classes (non-JDK provided imports) in the WEB-INF\classes directory (subdirectory of applications\DefaultWebApp). Start your server and access the JSPs through your browser as shown in Figures 15.4 and 15.5. JSP configuration is detailed within the next section.

Figure 15.4. Browser view of DisplayItem.jsp (Header.jsp included).

graphics/15fig04.gif

Figure 15.5. Browser view of Login.jsp.

graphics/15fig05.gif

    [ Team LiB ] Previous Section Next Section