WebLogic Server 6.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 /config/examples/applications/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/examples/jsp/tagext/counter directory of your WebLogic Server distribution.

  4. Compile the Java classes used in this example using the following command line:
      $ javac -d %EX_WEBAPP_CLASSES% *.java

  5. In your WebLogic Server installation, copy
    samples/examples/jsp/tagext/counter/pagehits.jsp
    to
    config/examples/applications/examplesWebApp/pagehits.jsp

  6. In your WebLogic Server installation, copy
    samples/examples/jsp/tagext/counter/counter.tld
    to
    config/examples/applications/examplesWebApp/WEB-INF/counter.tld

  7. Create the following subdirectories in your WebLogic Server installation (if they do not already exist): config/examples/applications/examplesWebApp/images config/examples/applications/examplesWebApp/images/numbers

  8. In your WebLogic Server installation, copy
    samples/examples/jsp/tagext/counter/images/numbers/*.gif
    to
    config/examples/applications/examplesWebApp/images/numbers/*.gif

  9. 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 BEA Systems, Inc. All rights reserved.



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

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