//In two ways custom tag syntax can be written
<prefix:tagname attr1=value1….attrn=valuen >
body code
</prefix:tagname>
Field Name | Description |
---|---|
public static int SKIP_PAGE | After custom tag it avoids information of JSP page . |
public static int SKIP_BODY | To vanish the tag body information. |
public static int EVAL_PAGE | It access JSP page information. |
public static int EVAL_BODY_INCLUDE | It access information of the body. |
Methods | Purpose |
---|---|
public void setparent | Tag handler parent will be instantiated. |
public void setpagecontext(pagecontext pagecontext) | Set the pagecontext object. |
public int dostartTag() throws jspException | JSP programmer should override this method and write the business logics to be performed at the start of the tag. |
public void release() | To discharge the state open void discharge() is summoned by the JSP execution object. |
public int doEndTg() throws jspException | JSP developer ought to abrogate this method and compose the business logic to be performed toward the end of the tag.. |
public Tag getTag() | Gets the parent tag handler object. |
Fields of IterationTag interface
Method of Iteration Tag interface
Following are the steps to be performed while working with custom tags.
custom.jsp
<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %> Current Date and Time is: <m:today/>
Here tagname will implement tag from taglib and the prefix which is given in the taglib library.
mytags.tld
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>simple</short-name> <uri>http://tomcat.apache.org/example-taglib</uri> <tag> <name>today</name> <tag-class>MyTagHandler.MyTagHandler</tag-class> </tag> </taglib>
web.xml
?xml version="1.0" encoding="UTF-8"? <web-app> <welcome-file-list> <welcome-file>custom.jsp</welcome-file> </welcome-file-list> </web-app>
Here page will be compiled from the custom.jsp .
MyTagHandler.java
import java.util.Calendar; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class MyTagHandler extends TagSupport{ public int doStartTag() throws JspException { JspWriter out=pageContext.getOut();//returns the instance of JspWriter try{ out.print(Calendar.getInstance().getTime());//printing date and time using JspWriter }catch(Exception e){System.out.println(e);} return SKIP_BODY;//will not evaluate the body content of the tag } }
SKIP_BODY is an optional returned value but this value must be returned by doStartTag() when you want to skip the body evaluation that is it must be returned when the TagLibraryDescriptor file contains the element empty, the value “empty” shows that there will always be an empty action. The java.util.Calendar.getInstance() method gets a calendar using the specified time zone and specified locale.
Output