WebLogic Server 6.0 Code Examples, BEA Systems, Inc.

Package examples.jsp.tagext.session

The JSP page lists the contents of the HTTP session and allows you to add new name/value pairs or remove existing ones, reflecting the fundamental behavior of a shopping cart.

See:
          Description

Class Summary
ListTag This class defines a custom JSP tag that enumerates through the contents of the current session.
ListTagExtraInfo Defines scripting variables.
 

Package examples.jsp.tagext.session Description

The JSP page lists the contents of the HTTP session and allows you to add new name/value pairs or remove existing ones, reflecting the fundamental behavior of a shopping cart.

This example implements two simple empty-body Tags. One calls some server side code to increment a session. The other displays the session in a JSP page.

This example introduces the following tag extension library concepts:

          Instructions for Building and Running the Example

Additional Resources for examples.jsp.tagext.session
SessionList.jsp This is the JSP page that runs this example.
session.tld The tag library descriptor (tld) for this example.
images/trash.gif Image file used as a "delete" icon in the jsp page.
 

The tag handler is implemented by the Java class examples.jsp.tagext.session.ListTag.

Iteration over body evaluation

This example uses the return value of the doAfterBody() method call to control whether it should re-evaluate the body of the custom tag. For each evaluation of the body, the tag extracts a name and value pair from the HTTP session and makes them available through scripting variables. The scripting variables contain a different value for each evaluation of the body.

Like the previous example, the ListTag tag handler extends the BodyTagSupport class, allowing it to process its body content.

The doStartTag() method obtains the HTTP session from the inherited pageContext instance variable. This provides information about the JSP page that uses the tag. If the session contains values, the first two values are extracted, stored as attributes in the page context, and EVAL_BODY_TAG is returned to indicate that the body should be evaluated. Note that the Enumeration from session.getAttributeNames() is stored in the tag handler instance variable names. This variable is accessed by other methods later in the tag handler's life cycle. The same tag handler class instance is used throughout the tag's life cycle and is not shared by other tag instances.

If the session does not contain any name/value pairs, the control value SKIP_BODY is returned to indicate that the body should not be evaluated. The tag will not produce any output in this case.

If the body is evaluated, it may reference the two scripting variables which contain the session name and value. In this example, these are compiled into an HTML table. Once the body is evaluated, the doAfterBody() method is invoked. This method checks for the next elements in the enumerated name list and if present, it updates the values of the scripting variables and returns EVAL_BODY_TAG to indicate that the tag body should be re-evaluated again. The doAfterBody() method will be invoked again once the body has been re-evaluated.

When there are no more elements to process in the names enumeration, the contents of BodyContent are written to the surrounding output stream scope using:

  getBodyContent().writeOut(getPreviousOut());
Note that the value of BodyContent is automatically accumulated upon each evaluation of the body and is only written out to the surrounding scope when the tag has finished iterating over the body. The method then returns SKIP_BODY indicating not to evaluate the body again.

Defining New Scripting Variables

As the tag iterates over the session name/value pairs, it makes their values available to the tag body through scripting variables. The changing values of the scripting variables cause the body to have different results upon each evaluation.

A tag declares its scripting variables through a separate class that extends TagExtraInfo class. We declare the TagExtraInfo class in the Tag Library Descriptor file "session.tld" with the line:

  <teiclass>examples.jsp.tagext.session.ListTagExtraInfo</teiclass>
The JSP engine invokes the getVariableInfo() method on the class to discover what new scripting variables are available. This example defines two scripting variables in the ListTagExtraInfo class by returning an array of type VariableInfo as shown here:
  return new VariableInfo[] {
    new VariableInfo("name",
                     "String",
                     true,
                     VariableInfo.NESTED),
    new VariableInfo("value",
                     "String",
                     true,
                     VariableInfo.NESTED)           
  };
This declares two variables of type String named "name" and "value." Their scope is declared as nested, meaning that they are only valid within the body of the tag.

The tag handler class initializes these scripting variables in the doStartTag() and doAfterBody() methods with the following code:

  pageContext.setAttribute("name", name);
  pageContext.setAttribute("value", value);

The JSP page references the new scripting variables within the body of the tag using the standard JSP expression syntax, inserting their values into HTML table data elements. The use of the scripting variables is highlighted in red in the listing below:

  <session:list>
    <tr>
      <td><%= name %></td>     
      <td><%= value %></td>
      <td><a href="<%=request.getRequestURI()%>?action=delete&delName=<%=name%>">
          <img border=0 src="<%= request.getContextPath() %>/images/trash.gif"> delete
          </a>
      </td>
    </tr>
  </session:list>

In this example, the scripting variables are always called "name" and "value". The SQL example illustrates how to declare a dynamically-named scripting variable using the "id" tag attribute.

Instructions for Package examples.jsp.tagext.session

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/session 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/session/SessionList.jsp
    to
    config/examples/applications/examplesWebApp/SessionList.jsp

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

  7. Create the following subdirectory in your WebLogic Server installation (if it does not already exist): config/examples/applications/examplesWebApp/images

  8. In your WebLogic Server installation, copy
    samples/examples/jsp/tagext/session/images/trash.gif
    to
    config/examples/applications/examplesWebApp/images/trash.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/SessionList.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.


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

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