Tag handler : A Tag handler as opposed to a tag file, is simply Java class that does the work of the tag.
These are come in two ways.
i)Classic (pre. versions of JSP)
ii)Simple (JSP 2.0)
Making a Simple Tag handler :
These are come in two ways.
i)Classic (pre. versions of JSP)
ii)Simple (JSP 2.0)
Making a Simple Tag handler :
i)Write a class that extends SimpleTagSupport
ii)Override the doTag() method
iii)Create a TLD for the tag
iv)Deploy the tag handler and TLD
v)Write a JSP that uses the tag
ii)Override the doTag() method
iii)Create a TLD for the tag
iv)Deploy the tag handler and TLD
v)Write a JSP that uses the tag
package foo; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import javax.servlet.jsp.tagext.Tag; public class SimpleTagEx extends SimpleTagSupport { public void doTag() throws JspException, IOException { getJspContext().getOut().print("Hello How are you...??"); //body-content : empty } }
TLD file (simple.tld)
<?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>1.2</tlib-version> <short-name>m</short-name> <uri>simpleTags</uri> <tag> <description>Simple Tag</description> <name>simple</name> <tag-class>foo.SimpleTagEx</tag-class> <body-content>empty</body-content> <!--<body-content>scriptless</body-content> --> </tag> </taglib>
JSP page
Jsp page (simpletag.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="myTags" uri="simpleTags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Simple Tag App</title> </head> <body> <myTags:simple /> </body> </html>
Simple tags are never reused by the Container, so each time a tag is invoked, the tag handler is instantiated, and its setJspContext() method is invoked.
If the tag is called from within another tag, the setParent() method is called.
SkipPageException stops processing the page.
Classic Tag :
package foo; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class ClassicTagEx extends TagSupport { public int doStartTag() throws JspException { JspWriter out = pageContext.getOut(); try { out.println("This is the calssic tag"); } catch(IOException e) { throw new JspException("IOException- "+e.toString()); } } }
complex.tld
<?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>1.2</tlib-version> <short-name>c</short-name> <uri>complexTags</uri> <tag> <description>Classic Tag</description> <name>classic</name> <tag-class>foo.ClassicTagEx</tag-class> <body-content>empty</body-content> </tag> </taglib>
Jsp page (classictag.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="myTags" uri="complexTags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Classic Tag App</title> </head> <body> <myTags:classic/> </body> </html>
No comments:
Post a Comment