Srujan Taglib
Srujan Taglib
1
• JSP’s offer a unique feature of “Tag Libraries”.
Simply put, these are custom defined JSP tags.
They are basically meant for componentizing
presentation level logic. They are very similar to
beans except the fact that Beans are used to
separate Business logic.
• Every tag is mapped to a particular class file
which is executed whenever the tag is
encountered in an JSP file.
• The general syntax :
<%@ taglib uri=“ - - - - - ” prefix = “ - - - ” %>
2
There are two things to remember while using
TAGLIB’s :
3
The taglib file forms the central description for the tag
library :
• This file is an XML document that defines the tag’s
operation.
• It maps the tag name on the page to the implementing
class.
• It defines inputs to the class.
• It references the helper class that provides details of any
output variables set by the tag class.
• This Tag and Bodytag have some methods, these all
methods are callback methods.
Tag methods : doStartTag()
doEndTag()
Body Tag : doAfterBody()
4
• small example to display Hello World :
package mytags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagtext.*;
public class Helloworld implements Tag {
private PageContext pagecontext;
private Tag parent;
public int doStartTag() throws JSPException
{
return SKIP_BODY;
}
public int doEndTag () throws JSPException
{
try{
pageContext.getOut().write(“Hello World”);
}
catch(java.io.Exception ex)
{
throw new JSPException(“IO Exception”);
}
return EVAL_PAGE;
} 7
public void release (){}
public void setPageContext(pageContext p)
{
pageContext=p;
}
public void setParent(Tag t)
{
parent = t;
}
public void getParent()
{
return parent;
}
} 8
Flow of programme:
Here in first .jsp file we are writing tag.But name
and properties of this tag we are writing in .tld
file. And whatever task that tag is going to
perform is written in .class file which has been
called by .tld file.