Files   Prepare   Run   Troubleshooting   Related Topics 

 

JSP Simple Tag

 

About the Example

Simple tags are a new JSP 2.0 feature that present a simpler invocation protocol than do classic tag handlers. A simple tag handler is represented in Java by a class which implements the SimpleTag interface.

Simple tag extensions do not directly rely on any servlet APIs, insteady they use JspContect, which extends PageContext. JspContext provides generic services such as...

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.

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:

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

File Description
application.xml 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 Class that demonstrates extend the JSP 1.2 Simple Tag API to manipulate data located in a database.
IteratorTag.java Class that 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 Simple Tag Library Descriptor.
web.xml Java EE standard Web application deployment descriptor.
weblogic.xml Oracle WebLogic Server-specific Web application deployment descriptor.
weblogic-application.xml Oracle WebLogic Server-specific enterprise application deployment descriptor.

 

Prepare the Example

 

Prerequisites

Before working with this example:

  1. Install Oracle WebLogic Server, including the examples.

  2. Start the Examples server.

  3. Set up your environment.

 

Configure Oracle WebLogic Server

  No special configuration is required for this example.

 

Build and Deploy the Example

To build the example, set the environment, and then run...

cd SAMPLES_HOME\server\examples\src\examples\webapp\jsp\tags\simple
ant build
ant deploy

The Ant command uses...

SAMPLES_HOME\server\examples\src\examples\webapp\jsp\tags\simple\build.xml

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.


 

Run the Example

To run the example...

  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...

    cd SAMPLES_HOME\server\examples\src\examples\webapp\jsp\tags\simple
    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

To execute a Web-based example, you can...

  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

  1. Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server
  2. Programming JSP Tag Extensions for Oracle WebLogic Server
  3. Section 7.1.5 of the JSP 2.0 Specification
  4. Developing Applications for Oracle WebLogic Server