6.2. The Top-Level Page

Listing 6.3 shows index.jsp, the top-level page for the boat store. The result is shown in Figure 6-1. There are a couple of things to note about this page.

Figure 6-1. Result of index.jsp.


First, since the style sheet app-styles.css is also in the top-level boats directory, the LINK element in the HEAD of the page can simply use the style sheet filename for the HREF attribute. See the source code archive at http://www.moreservlets.com if you want the source for app-styles.css. Similarly, the hypertext links to the yachts, tankers, and carriers JSP pages require only a simple filename within the HREF attribute. One of the goals of a Web application is that it require no modifications when it is moved from server to server or when the URL prefix is changed. Section 4.5 (Handling Relative URLs in Web Applications) gives details on handling relative URLs so that they don’t need modifications when the URL prefix changes, but the approach is trivial for URLs that refer to files in the same directory—just use the filename.

Second, since the yacht image is in the boats/images/ directory, the IMG element uses a URL of images/yacht.jpg.

Third, the taglib element uses a uri of /WEB-INF/tlds/count-taglib.tld. Although this URL begins with a slash, it refers to the /tlds subdirectory of the WEB-INF directory within the boats directory, not to the tlds directory of the server’s top-level WEB-INF directory. The reason for this behavior is that the URL is resolved by the server, not sent to the client, and the server resolves this type of URL within the Web application. See Listings 6.4 through 6.6 for the tag library definition and the tag library descriptor file.

Finally, as Listings 6.4 and 6.5 show, the count tag uses the servlet context to store the access count. As illustrated in Section 4.6 (Sharing Data Among Web Applications), each Web application has its own servlet context. Thus, servlets and JSP pages in other Web apps do not interfere with the access count, even if they use the same tag library.

Listings 6.4 and 6.5 give the source code for the custom tag used to keep the access count for this page and the other pages on the boats site. Listing 6.6 shows the tag library descriptor file that associates the CounterTag code with the base tag name of count.

Listing 6.3. boats/index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Boats</TITLE> 
<LINK REL=STYLESHEET 
      HREF="app-styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE">Boats!</TABLE> 
<P> 
Looking for a hole in the water into which to pour your money? 
You've come to the right place. We offer a wide selection of 
reasonably priced boats for everyday use. 

<IMG SRC="images/yacht.jpg" WIDTH=240 HEIGHT=367 
     ALIGN="RIGHT" ALT="Base-model yacht"> 
<H2>Yachts</H2> 
Starting at a mere 72 million, these entry-level models are 
perfect for the cost-conscious buyer. 
Click <A HREF="yachts.jsp">here</A> for details. 

<H2>Oil Tankers</H2> 
Looking for something a bit bigger and sturdier? These 
roomy models come complete with large swimming pools. 
Click <A HREF="tankers.jsp">here</A> for details. 
<H2>Aircraft Carriers</H2> 
Concerned about security? These high-tech models come 
equipped with the latest anti-theft devices. 
Click <A HREF="carriers.jsp">here</A> for details. 
<P> 

<%@ taglib uri="/WEB-INF/tlds/count-taglib.tld" prefix="boats" %> 
<boats:count /> 

</BODY> 
</HTML> 

Listing 6.4. boats/WEB-INF/classes/moreservlets/CounterTag.java[*] [*]
package moreservlets; 

import javax.servlet.*; 
import javax.servlet.jsp.*; 
import javax.servlet.jsp.tagext.*; 
import java.io.*; 
import java.text.*; 

/** Tag that outputs a Web-app-specific hit count. 
 *  For boats example. 
 *  <P> 
 *  The actual name of the tag is not defined here; 
 *  that is given by the Tag Library Descriptor (TLD) 
 *  file that is referenced by the taglib directive 
 *  in the JSP file. 
 */ 

public class CounterTag extends TagSupport {
  public int doStartTag() {
    try {
      ServletContext application =
						pageContext.getServletContext();
						Count count = (Count)application.getAttribute("count");
						if (count == null) {
						count = new Count();
						application.setAttribute("count", count);
						} 
      DateFormat formatter = 
        DateFormat.getDateInstance(DateFormat.MEDIUM); 
      JspWriter out = pageContext.getOut(); 
      out.println("<BR CLEAR="ALL"><BR><HR>"); 
      out.println("This site has received " + 
                  count.getCount() + " hits since " + 
                  formatter.format(count.getStartDate()) + 
                  "."); 
      count.incrementCount(); 
    } catch(IOException ioe) {
      System.out.println("Error in CounterTag: " + ioe); 
    } 
    return(SKIP_BODY); 
  } 
} 

[*] Technically, only the.class file needs to go in this directory. The only requirement for the source code is that it go in a directory matching the package name (moreservlets).

Listing 6.5. boats/WEB-INF/classes/moreservlets/Count.java[*] [*]
package moreservlets; 

import java.util.Date; 

/** Simple bean used by CounterTag. For boats example. */ 

public class Count {
  private int count = 1; 
  private Date startDate = new Date(); 

  public int getCount() {
    return(count); 
  } 

  public void incrementCount() {
    count++; 
  } 

  public Date getStartDate() {
    return(startDate); 
  } 
} 

[*] Technically, only the.class file needs to go in this directory.

Listing 6.6. boats/WEB-INF/tlds/count-taglib.tld
<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE taglib 
 PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" 
 "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 

<taglib> 
  <tlibversion>1.0</tlibversion> 
  <jspversion>1.1</jspversion> 
  <shortname>Counts</shortname> 
  <info> 
    A tag library for counters. From More Servlets and 
    JavaServer Pages, http://www.moreservlets.com. 
  </info> 
  <tag>
						<name>count</name>
						<tagclass>moreservlets.CounterTag</tagclass>
						<bodycontent>empty</bodycontent>
						<info>Hit count</info>
						</tag> 
</taglib> 

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

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