SE452: Lecture 9 (Tag Libraries/Tag Files/JSTL/Filters)

Contents [0/14]

Overview of Today's Class [1/14]
Creating Tags [2/14]
Implementing a Tag Handler [3/14]
The SimpleTag Interface [4/14]
The Tag Interface [5/14]
The IterationTag Interface [6/14]
The BodyTag Interface [7/14]
The TryCatchFinally Interface [8/14]
The Tag Library Descriptor [9/14]
Deploying Tag Libraries [10/14]
Using Tag Libraries [11/14]
JSTL [12/14]
JSTL tags [13/14]
Homework [14/14]

Overview of Today's Class [1/14]

Tag Libraries - implementing

Tag Files - implementing

JSTL - Intro

Filters

Creating Tags [2/14]

A custom tag consists of a tag handler and a tag library descriptor

Types:

To use the tag:

A custom tag must implement the javax.servlet.jsp.tagext.Tag or javax.servlet.jsp.tagext.SimpleTag interface (which now extends javax.servlet.jsp.tagext.JspTag for organizational purposes

This is usually done by extending a base utility class

The Tag interface supplies doStartTag() and doEndTag()

BodyTag supplies doInitBody(), setBodyContent()

IterationTag supplies doAfterBody()

TryCatchFinally supplies doCatch(), doFinally() and helps tags manage resources better

Implementing a Tag Handler [3/14]

To implement a tag handler:

The SimpleTag Interface [4/14]

doTag() - Called by the container to invoke this tag, and is called once and only once. The lifecycle for SimpleTag is simpler than Tag, IterationTag, or BodyTag.

Lifecycle:

  1. A new tag handler instance is created each time by the container by calling the provided zero-args constructor. Unlike classic tag handlers, simple tag handlers are never cached and reused by the JSP container. This can be a performance issue, but in modern JVMs is not nearly as much of an issue.
  2. The setJspContext() and setParent() methods are called by the container. The setParent() method is only called if the element is nested within another tag invocation.
  3. The setters for each attribute defined for this tag are called by the container. Use JavaBean conventions for this functionality.
  4. If a body exists, the setJspBody() method is called by the container to set the body of this tag, as a JspFragment. If the action element is empty in the page, this method is not called at all.
  5. The doTag() method is called by the container. All tag logic, iteration, body evaluations, etc. occur in this method.
  6. The doTag() method returns and all variables are synchronized.

If you need to create a SimpleTag, but use it as a classic tag, you can use a TagApapter to wrap the SimpleTag and then the SimpleTag can be used where a classic Tag is expected.

The Tag Interface [5/14]

doStartTag()

doEndTag()

Lifecycle:

Tag Protocol

The IterationTag Interface [6/14]

doAfterBody()

Lifecycle:

Iteration Tag Protocol

The BodyTag Interface [7/14]

Lifecycle:

Body Tag Protocol

The TryCatchFinally Interface [8/14]

An auxiliary interface for tags that want more control over resources

doCatch()

doFinally - invoked in all cases after doEndTag() for any class implementing Tag, IterationTag, or BodyTag

The Tag Library Descriptor [9/14]

In order for your tag library to be processed properly by the Servlet container, you must supply a configuration file known as a tag library descriptor. This basically servers the purpose of web.xml for a web application, it is information that the container uses to manage the libraries at runtime

http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd for JSP 2.0

http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd for JSP 1.2

http://java.sun.com/dtd/web-jsptaglibrary_1_1.dtd for JSP 1.1>

TLD structure:

Examples:

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
	http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
	version="2.0">


    <tlib-version>2.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>se452</short-name>
    <uri>/se452</uri>
    <display-name>se452-taglib</display-name>
    <small-icon></small-icon>
    <large-icon></large-icon>
    <description>
        A set of example tag libraries for SE452 at DePaul University
    </description>
    
    <tag>
        <name>date</name>
        <tag-class>se452.tags.DateTag</tag-class>
        <body-content>empty</body-content>
        <description>
        Prints the current time and date
        </description>
    </tag>
  
    <tag>
        <name>debug</name>
        <tag-class>se452.tags.DebugTag</tag-class>
        <body-content>JSP</body-content>
        <description>
        Includes the contents of the tag if debug request parameter
        is set to true
        </description>
    </tag>
    
    <tag>
        <name>select</name>
        <tag-class>se452.tags.SelectTag</tag-class>
        <body-content>empty</body-content>
        <description>
            Displays an html select with preselected options
        </description>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <name>selected</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <name>list</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
    </tag>
    
</taglib>

Deploying Tag Libraries [10/14]

  1. Put the class files in "doc root"/WEB-INF/classes
  2. Put the tag library descriptor anywhere in the webapp
  3. Edit WEB-INF/web.xml, Add the following to your web.xml after servlet-mappings and session-config, but before resource-ref:

<jsp-config>
    <taglib>
        <taglib-uri>se452-taglib.tld</taglib-uri>
        <taglib-location>/WEB-INF/tld/se452-taglib.tld</taglib-location>
    </taglib>
</jsp-config>

Using Tag Libraries [11/14]

Use taglib directive


<%@ taglib uri="se452-taglib.tld" prefix="se452" %>
    

Write the tag in the JSP

<se452:tag1>

or

<se452:tag1>
	Body (any JSP/html/Text)
</se452:tag1>

JSTL [12/14]

The JavaServer Pages Standard Tag Library (JSTL) encapsulates, as simple tags, core functionality common to many JSP applications

JSTL is hosted at Jakarta - along with many other useful tag libraries

JSTL, unlike the other libraries, is standardized through the Java Community Process (JCP).

JSTL comes in two versions:

JSTL is the origin of the Expression Language that is now standard in JSP 2.0, so you can use the EL in any JSTL 1.0 tags in a JSP 1.2 container, but the EL is not available outside of JSTL tags (like in a JSP 2.0 container).

To deploy the JSTL, just follow the same directions as for any tag library.

JSTL tags [13/14]

JSTL comes in 4 libraries:

  1. Core library - c
  2. XML processing library - x
  3. Internationalization (i18n) library - fmt
  4. Database Access (SQL) library - sql

Core is for management of variables, iteration, output, conditional logic

XML processing is for parsing, selection of XML, XSTL transforms, logic based on XML data

The i18n library supports formatting of things like currency, dates, and numbers, as well as localized output of content based on Locale

The SQL library is not meant to promote good design of database based apps, but provide for easy access to databases and presentation of the data, without a large amount of abstraction.

Homework [14/14]

Goal: To write a simple tag library from scratch.

  1. Create a tag library that solves a display problem for your application. The key is to look for scriptlets in your JSP, or complicated logic that is needed to provide part of your presentation layer. If you don't have any good candidates in your current code, add something.
  2. The tag can be any of the tag types, including SimpleTag
  3. Write the tag library descriptor.
  4. Add a reference to your tag library in web.xml
  5. Use the tag library in a JSP.

Supply the application as a deployable war file. Test deploying it before submitting it to make sure you have the format right. Place the source files for the servlet in a src directory in the war file. Please link the homework assignment to the index.html file in the document root.

Grading Breakdown:

ItemPoints
configuration (web.xml)10
proper TLD20
Tag implementation30
JSP implementation20
Coding style and JavaDoc10
Proper Submission10
Total:100


Revised: 11/9/2003