|
WebLogic Server 6.0 Code Examples, BEA Systems, Inc. |
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. |
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. |
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 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.
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");
$ javac -d %EX_WEBAPP_CLASSES% *.java
http://localhost:7001/examplesWebApp/pagehits.jsp
Copyright © 2000 BEA Systems, Inc. All rights reserved.
|
Documentation is available at http://e-docs.bea.com/wls/docs60 |