Custom Tag
My First Tag
Now, let's look at a sample tag that when invoked prints a message to the client.
There are a few steps involved in developing a custom tag. These steps can be summarized as follows:
1. Develop the tag handler
2. Create a tag library descriptor
3. Test the tag
HelloTag.java
package tags;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag implements Tag {
private PageContext pageContext;
private Tag parent;
public HelloTag() {
super();
}
public int doStartTag() throws JspException {
try {
pageContext.getOut().print("This is my first tag!");
} catch (IOException ioe) {
throw new JspException("Error:IOException while writing to client"
+ ioe.getMessage());
}
return SKIP_BODY;
}
public int doEndTag() throws JspException {
return SKIP_PAGE;
}
public void release() {
}
public void setPageContext(PageContext pageContext) {
this.pageContext = pageContext;
}
public void setParent(Tag parent) {
this.parent = parent;
}
public Tag getParent() {
return parent;
}
}
The two important methods to note in HelloTag are doStartTag and doEndTag. The doStartTag method is invoked when the start tag is encountered. In this example, this method returns SKIP_BODY because a simple tag has no body. The doEndTag method is invoked when the end tag is encountered. In this example, this method returns SKIP_PAGE because we do not want to evaluate the rest of the page; otherwise it should return EVAL_PAGE.
mytaglib.tld
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>first</shortname>
<uri></uri>
<info>A simple tab library for the
examples</info>
<tag>
<name>hello</name>
<tagclass>tags.HelloTag</tagclass>
<bodycontent>empty</bodycontent>
<info>Say Hi</info>
</tag>
</taglib>
Hello.jsp
<%@ taglib uri="/WEB-INF/jsp/mytaglib.tld"
prefix="first" %>
<HTML>
<HEAD>
<TITLE>Hello Tag</TITLE>
</HEAD>
<BODY bgcolor="#ffffcc">
<B>My first tag prints</B>:
<first:hello/>
</BODY>
</HTML
Now, let's look at a sample tag that when invoked prints a message to the client.
There are a few steps involved in developing a custom tag. These steps can be summarized as follows:
1. Develop the tag handler
2. Create a tag library descriptor
3. Test the tag
HelloTag.java
package tags;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag implements Tag {
private PageContext pageContext;
private Tag parent;
public HelloTag() {
super();
}
public int doStartTag() throws JspException {
try {
pageContext.getOut().print("This is my first tag!");
} catch (IOException ioe) {
throw new JspException("Error:IOException while writing to client"
+ ioe.getMessage());
}
return SKIP_BODY;
}
public int doEndTag() throws JspException {
return SKIP_PAGE;
}
public void release() {
}
public void setPageContext(PageContext pageContext) {
this.pageContext = pageContext;
}
public void setParent(Tag parent) {
this.parent = parent;
}
public Tag getParent() {
return parent;
}
}
The two important methods to note in HelloTag are doStartTag and doEndTag. The doStartTag method is invoked when the start tag is encountered. In this example, this method returns SKIP_BODY because a simple tag has no body. The doEndTag method is invoked when the end tag is encountered. In this example, this method returns SKIP_PAGE because we do not want to evaluate the rest of the page; otherwise it should return EVAL_PAGE.
mytaglib.tld
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>first</shortname>
<uri></uri>
<info>A simple tab library for the
examples</info>
<tag>
<name>hello</name>
<tagclass>tags.HelloTag</tagclass>
<bodycontent>empty</bodycontent>
<info>Say Hi</info>
</tag>
</taglib>
Hello.jsp
<%@ taglib uri="/WEB-INF/jsp/mytaglib.tld"
prefix="first" %>
<HTML>
<HEAD>
<TITLE>Hello Tag</TITLE>
</HEAD>
<BODY bgcolor="#ffffcc">
<B>My first tag prints</B>:
<first:hello/>
</BODY>
</HTML