Files Prepare Run Troubleshooting Related Topics
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.
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. |
Before working with this example:
No special configuration is required for this 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.
To run the example...
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...
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.