Files   Prepare   Run   Troubleshooting   Related Topics 

About the Example

Simple tags are a new JSP 2.0 feature. They present a much simpler invocation protocol than do classic tag handlers. A simple tag handler is represented in Java by a class which implements the SimpleTag interface. In addition to being simpler to work with, simple tag extensions do not directly rely on any servlet APIs, which allows for potential future integration with other technologies. This is facilitated by the JspContext class, which PageContext extends. JspContext provides generic services such as storing the JspWriter and keeping track of scoped attributes, whereas PageContext has functionality specific to serving JSPs in the context of servlets. Whereas the Tag interface relies on PageContext, Simple tags only rely on JspContext. The body of a Simple Tag, if present, is translated into a JSP fragment and passed to the setJspBody method. The tag can then execute the fragment as many times as needed.

Unlike classic tag handlers, the SimpleTag interface does not extend Tag. Instead of supporting doStartTag() and doEndTag(), the SimpleTag interface provides a simple doTag() method, which is called once and only once for any given tag invocation. All tag logic, iteration, body evaluations, and so on are performed in this single method. Thus, simple tag handlers have the equivalent power of a BodyTag, but with a much simpler lifecycle and interface. To support body content, the setJspBody() method is provided. The container invokes the setJspBody() method with a JspFragment object encapsulating the body of the tag. The tag handler implementation can call invoke() on that fragment to evaluate the body. The SimpleTagSupport convenience class provides getJspBody() and other useful methods to make this even easier.

The example demonstrates the Simple Tag API. It uses TagSupport to iterate through a collection. It also uses BodyTagSupport and TryCatchFinally to access data in a database.

The IteratorTag.java file demonstrates how to extend the JSP 1.2 Simple Tag API to iterate thru a collection, while the ExecuteSql.java file demonstrates how to extend the JSP 1.2 Simple Tag API to manipulate data located in a database.


Files Used in the Example

Directory Location:

BEA_HOME/wlserver_10.3/samples/server/examples/webapp/jsp/tags/simple/

(where BEA_HOME is the directory containing your WebLogic Server installation)

File

Click source files to view code.

Description

application.xml The Java EE standard enterprise application deployment descriptor.
build.xml Ant build file that contains targets for building and running the example.
ExamplesFooter.jsp Java Server Page containing the Example Footer.
ExamplesHeader.jsp Java Server Page containing the Example Header.
ExecuteSql.java This class demonstrates extend the JSP 1.2 Simple Tag API to manipulate data located in a database.
IteratorTag.java This class demonstrates extend the JSP 1.2 Simple Tag API to iterate thru a collection.
SimpleTag.jsp Java Server Page containing the Simple Tag.
simpleTag.tld The Simple Tag Library Descriptor.
web.xml The Java EE standard Web application deployment descriptor.
weblogic.xml The WebLogic Server-specific Web application deployment descriptor.
weblogic-application.xml The WebLogic Server-specific enterprise application deployment descriptor.

Prepare the Example

Prerequisites

Before working with this example:

  1. Install WebLogic Server, including the examples.
  2. Start the Examples server.
  3. Set up your environment.

Configure WebLogic Server

  No special configuration is required for this example

Build and Deploy the Example

To build the example, follow these steps:

  1. Change to the SAMPLES_HOME\server\examples\src\examples\webapp\jsp\tags\simple directory, where SAMPLES_HOME refers to the main WebLogic Server examples directory, such as d:\bea\wlserver_10.3\samples.

  2. Execute the following command (from the shell where you set your environment):

    ant build

    This command compiles and stages the example.
  3. Execute the following command (from the shell where you set your environment):

    ant deploy

    This command deploys the example on the wl_server domain of your WebLogic Server installation.

    The Ant command uses the build.xml file, located in the SAMPLES_HOME\server\examples\src\examples\webapp\jsp\tags\simple directory, to build the example.

    Running the build script builds the EJB in split development directory format where compiled and generated classes are placed in SAMPLES_HOME\server\examples\build\jspSimpleTagEar and non-compiled files remain within the source directory of the example. For more information on split development directory, see Developing WebLogic Server Applications.


Run the Example

To run the example, follow these steps:

  1. Complete the steps in the "Prepare the Example" section.
  2. In your development shell, run the JSP Simple Tag example by using the following command from the example directory (SAMPLES_HOME\server\examples\src\examples\webapp\jsp\tags\simple):

    prompt> ant run

    This command executes the build.xml run target, which opens your default browser to the following URL:

    http://localhost:port/jsp_simpleTag/SimpleTag.jsp

    where

  1. If your example runs successfully, you will see a list of elements as follows:

    Iterating over a collection using custom iterate tag...
        Element is Apples
        Element is Oranges
        Element is Grapes
        Element is Kiwis
        Element is Strawberries

    You will also see a custom SQL tag Query, with results displayed in a table.


Troubleshooting


Related Topics

(Internet connection required.)


Copyright © 1996, 2008, Oracle and/or its affiliates. All Rights Reserved.