SE 452 Lecture Notes
Week 7: Custom Tag Libraries
Administrative [1/11]
JSP Tag Extensions [2/11]
Tag Libs vs. Java Beans [3/11]
Creating a custom tag [4/11]
The tag handler [5/11]
Tag Handler Life Cycle [6/11]
Tag Library Descriptor (TLD) [7/11]
JSP that uses our new tag [8/11]
Where to deploy [9/11]
Using the tag contents [10/11]
Exam next week [11/11]
Administrative [1/11]
- Any question on the demo option for homework grading?
- Additional extra credit option
- We discussed the initial extra credit option last week
- Or, you can give a 15 minute presentation on a J2EE pattern during week 10 (the optional week where there will be no quiz)
- You need to explain what it is
- Why you would do it in a certain situation
- And how your code illustrates it
- A presentation will be worth 120 points, but you will not be allowed to do the in person extra credits
- No duplicate J2EE patterns will be presented
- First come first serve
- Send me an e-mail with your request
- Your best bet is to send an e-mail with a few you are willing to cover
- Once you commit to do a pattern you are required to do so
- If you commit and change your mind there will be a score deduction (more of an empty threat, but just don't do it because you mess up other people)
- In addittion to the Sun list; you can also demostrate the use of a GoF (Gang of Four) design pattern in a JSP/Servlet context
- If you choose the GoF route you need to show how you combine multiple GoF patterns to be effective
- Overlapping GoF patterns will be allowed as long as each presentation has one unique pattern
- E-mail me with your requests
- DL students who would like to choose this option should contact me about taking the final at a different time
- What's an example where JavaBeans would have an advantage over scriplets?
- What's the point of JavaBeans?
- Let's go over the homework 4/5
- Any questions before the quiz?
JSP Tag Extensions [2/11]
- Allows introduction of new custom actions into
a JSP that we define
- A Tag Library can be used to define a
specialized sub language of JSP that allows the users of that library to use it naturally with JSP
- Allows us to create a Tag Library
- A set of Tags
- Portable
- Usable in any JSP container
- Simple and easy to use
- Hides the implementation
- Allows non programmers to develop more
complex pages without using complex scripting elements
Tag Libs vs. Java Beans [3/11]
- Accomplish many of the same goals
- Encapsulate complex behaviors
- Allows for code reuse
- Object Oriented
- Some improvements with custom tags
- Tag can manipulate JSP content
- Support for complex operations such as iterations
- does take a little more work to set up
Creating a custom tag [4/11]
- Three parts
- Tag Handler
- A Java class that implements
javax.servlet.jsp.taxext.Tag
interface
- Usually indirectly by extending
TagSupport
or BodyTagSupport
- Tag Library Descriptor(TLD)
- An XML configuration file
- Loaded by the servlet engine at startup
- A JSP file
- Use a
taglib
directive
- Use your custom JSPs
The tag handler [5/11]
- The
Tag
interface defines the basic methods for all tag handlers
doStartTag()
- Processes the start tag of this action (before the body)
pageContext
is available
- return constant
EVAL_BODY_INCLUDE
to evaluate the tag body
- return constant
SKIP_BODY
to not evaluate the body
- Required if the TLD has a body content of
EMPTY
doEndTag()
- Processes the end tag of this action (after the body)
- If returns
EVAL_PAGE
, the rest of the page continues to be evaluated
- If returns
SKIP_PAGE
, the rest of the page is skipped
TagSupport implements Tag
- Extend this class for empty tags
- The
BodyTag
interface extends Tag
interface provides two additional methods for
handling tag bodies
doInitBody()
- Evaluated prior to doAfterBody
- Oncly called once
- Can view BodyContent
doAfterBody()
- Used for tags that repeat data
- Called to determine if tag should be evaluated again
- return
EVAL_BODY_AGAIN
or SKIP_BODY
BodyTagSupport implements BodyTag
- Enxtend this one for non empty tags
- More on this one in a bit
- Tags also contain writeable attributes
- provide setters for the properties
- setters are called by the JSP container prior to any other methods
package edu.depaul.se452;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class HelloWorldTag extends TagSupport {
protected String myString;
public void setMyString(String myString)
{
this.myString = myString;
}
public int doStartTag() {
try {
JspWriter out = pageContext.getOut();
out.print("Hello, " + myString + "!");
} catch(IOException ioe) {}
return(SKIP_BODY);
}
}
HelloWorldTag.java
Tag Handler Life Cycle [6/11]
You can overwrite as many of the below methods as you need to. So you should know what happens when.
default properties initialized ->
all properties initialized from tag ->
pageContext initialized ->
doStartTag() ->
setBodyContent() ->
doInitBody() ->
doAfterBody() ->
doEndTag()
Tag Library Descriptor (TLD) [7/11]
- XML file that informs the server about the handler
name
: unique name used to access the tag
tagclass
: the fully qualified name of the handler
bodycontent
: indicates if our tag has a body to execute
attribute
: required if our handler has attributes to set
name
: attribute's name
required
: indicates if the attribute must always be supplied by the jsp
rtexprvalue
: can the attribute value be a JSP expression
1.0
1.1
se452
helloTag
edu.depaul.se452.HelloWorldTag
EMPTY
myString
true
true
se452-taglib.tld
JSP that uses our new tag [8/11]
- Use taglib directive
- <%@ taglib uri="se452-taglib.tld" prefix="se452" %>
uri
: absolute or relative url
- Use tag in the JSP
-
- Body (any JSP/ html/ Text)
MyFirst Tag
<%@page import="java.util.*" %>
<%@taglib uri="WEB-INF/se452-taglib.tld" prefix="se452" %>
Let's try our first tag
" />
myFirstTag.jsp
Where to deploy [9/11]
- C:\jakarta-tomcat-4.0.6\webapps\jjungman\WEB-INF\classes\edu\depaul\se452\HelloWorldTag.class
- C:\jakarta-tomcat-4.0.6\webapps\jjungman\WEB-INF\se452-taglib.tld
- C:\jakarta-tomcat-4.0.6\webapps\jjungman\myFirstTag.jsp
Using the tag contents [10/11]
- To include the tag body have your
doStartTag
method return EVAL_BODY_INCLUDE
- Any closing action can be placed in
doEndTag
(executed after the body)
- extending
BodyTagSupport
gives us two new important methods
doAfterBody
- return
SKIP_BODY
once you no longer need to interpret the body
- return
EVAL_BODY_TAG
(deprecated for JSP1.1) or EVAL_BODY_AGAIN
(same int) to view the body again
getBodyContent
returns a BodyContent object encapsulating the contents of the body and provides three important methods
getEnclosingWriter
the writer used in doStartTag
and doEndTag
getReader
a reader that can read the tag's body
getString
the body content as a String
package edu.depaul.se452;
import java.util.Arrays;
import java.util.List;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class CounterTag extends BodyTagSupport
{
private List theList = ListClass.getList();
private int size = theList.size();
public int doAfterBody()
{
BodyContent bodyContent = getBodyContent();
if (size > 0)
{
try
{
JspWriter out = bodyContent.getEnclosingWriter();
out.println(bodyContent.getString() + theList.get(--size));
bodyContent.clearBody();
}
catch (Exception e)
{}
return EVAL_BODY_AGAIN;
}
return SKIP_BODY;
}
}
class ListClass
{
private static String[] ar = new String[] { "jen", "joe", "sue" };
public static List getList()
{
return Arrays.asList(ar);
}
}
CounterTag.java
1.0
1.1
se452
helloTag
edu.depaul.se452.HelloWorldTag
EMPTY
myString
true
true
counter
edu.depaul.se452.CounterTag
JSP
se452-taglib.tld
My Second Tag
<%@page import="java.util.*" %>
<%@taglib uri="WEB-INF/se452-taglib.tld" prefix="se452" %>
Let's try our second tag
mySecondTag.jsp
Exam next week [11/11]
Exam next week
Homework due before class