WebLogic Server 7.0 Code Examples, BEA Systems, Inc.

Package examples.jsp.tagext.counter

This example implements two simple empty-body tags to increment and display a counter in a JSP page.

See:
          Description

Class Summary
Count This class synchronizes access to the count hits file.
Display This class displays a graphical page-hits count in a JSP page.
Increment This class displays the page-hits count in a JSP page.
 

Package examples.jsp.tagext.counter Description

This example implements two simple empty-body tags to increment and display a counter in a JSP page.

The example demonstrates the following tasks:

          Instructions for Building and Running the Example

Additional Resources for examples.jsp.tagext.counter
pagehits.jsp This is the JSP page that runs this example.
counter.tld The tag library descriptor (tld) for this example.
images\numbers\*.gif Image files used to display the counter in the JSP page.
 

The pagehits.jsp file

pagehits.jsp uses the following directive to include the counter tag library:

  <%@ taglib uri="counter" prefix="counter" %>
This reference maps the tag library descriptor to an entry in the deployment descriptor for the examplesWebApp, which points to the actual location of counter.tld (in the WEB-INF directory).

The counter.tld file is the tag library descriptor for the tags used in the pagehits.jsp page. This directive associates the JSP keyword counter with this tag library. We could have chosen any other non-reserved word.

In the rest of the JSP page, you can refernce the new tags from the counter tag library as shown here:

<counter:increment/> invokes the increment tag that is described in the counter.tld file.

<counter:display/> invokes the display tag that is described in the counter.tld File.

The counter.tld file

The counter.tld file defines the tag library and the tags it contains. The following example defines the display tag:

  <tag>
    <name>display</name>
    <tagclass>examples.jsp.tagext.counter.Display</tagclass>
    <bodycontent>empty</bodycontent>
  </tag>

The <tagclass> element defines a Java class that implements the functionality of the display tag -- also referred to as the tag handler. This class is defined by the Display.java source file. The classes are compiled into the WEB-INF\classes directory.

The Tag Handler Classes

This example uses two custom tags that are implemented by the tag handler classes Display and Increment. Since both of these tags are simple empty-body tags, their tag handlers must implement the javax.servlet.jsp.tagext.Tag interface, or the convenience class javax.servlet.jsp.tagext.TagSupport.

Both tag handlers only override the doStartTag() method, which is invoked when the tag is reached in the JSP page. The Increment class increments the current page hits count, via the Count class. The Display class looks up the current count, and inserts a sequence of numeral images into the JSP page that represent the current count. These images are located in the "images\numbers" directory of the Web Application (located in SAMPLES_HOME\server\stage\examples\examplesWebApp).

The page-hits count is maintained in a file named "count.tmp" on the file system of the server. This file is located in a temporary working directory that is determined by the ServletContext attribute javax.servlet.context.tempdir. The tag handler classes look up this attribute value using the following code:

    File tempDir = (java.io.File) pageContext.getServletContext().
       getAttribute("javax.servlet.context.tempdir");

Instructions for Package examples.jsp.tagext.counter

Build the Example

  1. Open a new command shell.

  2. Set up this development shell as described in Setting up Your Environment for Building and Running the Examples.

  3. In this development shell, switch to the SAMPLES_HOME\server\src\examples\jsp\tagext\counter directory of your WebLogic Server distribution.

  4. Build the example using the following command line: (this will compile the classes and copy the jsps to the correct location)
      prompt> ant

  5. Start WebLogic Server with the examples configuration.

Configure the Server

Make sure that the examplesWebApp is deployed on your server.

Run the Example

Use a web browser to load the following URL:
http://localhost:7001/examplesWebApp/pagehits.jsp

There's More...

For more information on WebLogic JSP, see Programming WebLogic JSP.

For more information on WebLogic JSP Tag Libraries, see Programming WebLogic JSP Tag Libraries.

Copyright © 2000-2001 BEA Systems, Inc. All rights reserved.



Documentation is available at
http://e-docs.bea.com/wls/docs70

Copyright © 2002 BEA Systems, Inc. All Rights Reserved.