WebLogic Server 6.0 Code Examples, BEA Systems, Inc.

Package examples.jsp.tagext.quote

The examples in this package illustrate how to use JSP tag attributes and how to define a body-tag to display a Java source code file as an HTML page.

See:
          Description

Class Summary
CodeTag This class defines a custom JSP tag that enumerates through the contents of the current session.
 

Package examples.jsp.tagext.quote Description

The examples in this package illustrate how to use JSP tag attributes and how to define a body-tag to display a Java source code file as an HTML page.

This example introduces the following tag extension library concepts:

          Instructions for Building and Running the Example

Additional Resources for examples.jsp.tagext.quote
showcode.jsp This is the JSP page that runs this example.
quote.tld The tag library descriptor (tld) for this example.
 

Description

The quote example defines a body-tag that processes its body content as a Java source listing. The Java source is modified for presentation in a web browser so that it uses HTML formatting.

The content of the tag body is evaluated as JSP, allowing the content to be included directly from the Java source file using a JSP include directive. The body can also simply be included in-line in the JSP page. The evaluated body content is processed so that its characters appear as expected in the browser. For example, "<" signs are interpreted into "&amp;" escape characters in the output. Java comments and quoted text are also highlighted by including the appropriate HTML <font> tags.

The custom tag extension quotes and highlights the included Java code. The Java code presented is the actual implementation of the tag handler that is being used in this example.

The showcode.jsp page references the tag extension library using the JSP directive:

  <%@ taglib uri="quote" prefix="quote" %>
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 tag handler is implemented by the Java class located under in the WEB-INF/classes directory, under the full package name examples.jsp.tagext.quote.CodeTag.

Using Tag Attributes

Tag attributes allow the JSP page to pass String values into the tag handler. These can be used to configure the behavior of the tag. This example defines three attributes:

The Tag Library Descriptor declares each attribute for the tag within the <tag> element using an <attribute> tag for each attribute as follows:

  <attribute>
    <name>commentColor</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>

The tag handler must implement setter and getter methods for each attribute, similar to those found in a JavaBean component. The first letter of the attribute is capitalized after the get/set word in the method name. For example, the attribute commentColor must have the following methods defined in the tag handler:

  public void setCommentColor(String col) { commentColor = col; }
  public String getCommentColor(String col) { return commentColor; }

The JSP container uses these methods to inform the tag handler instance of the attribute values before the doStartTag() method is invoked. The class need not have instance variables named exactly after the attributes, and may respond to the method invocations however it wishes, but it must provide these methods.

In this example, we store the given attribute values in instance variables, and refer to them from other methods in the tag handler class. This tag handler instance will only be used during the life cycle of this tag, so we don't need to worry about multithreaded access to the same instance.

Defining a Body-Tag

The CodeTag class implements the BodyTagSupport class, which is used by JSP tag extensions that operate on their body content. This is a convenience class that implements default behavior for the BodyTag interface. This example overrides the doAfterBody() method, which is invoked after the body has been evaluated. An evaluated body is the result of processing the tag body content, which can include other nested JSP tags.

In the doAfterBody() method, the example obtains the content of the evaluated body as a String using the following line of code:

  String body = getBodyContent().getString();
The string is processed, and the result is sent back to the page via the JspWriter obtained from the getPreviousOut() method. This represents an output stream to the surrounding tag context, allowing the body of a nested tag to be evaluated before it is presented to the tag it is nested within. In fact, even if you used the pageContext to obtain the JspWriter of the response, the content will not be written directly to the response, but is caught and passes to the surrounding tag if it is in a nested tag. Otherwise, the output might appear out-of-order in the response page. The scope of getPreviousOut() used in a non-nested tag refers to the content of the JSP page.

The doAfterBody() method returns the control code SKIP_BODY, indicating that the JSP engine continues evaluating the JSP page. It is possible to cause the body to be re-evaluated at this point, which is illustrated in the Session example.

Instructions for Package examples.jsp.tagext.quote

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/quote directory of your WebLogic Server distribution.

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

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

  6. In your WebLogic Server installation, copy
    samples/examples/jsp/tagext/quote/CodeTag.java
    to
    config/examples/applications/examplesWebApp/CodeTag.java
    This is the Java source code that this example will display as HTML.

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

  8. 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/showcode.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.