The taglib directive

The taglib directive is used to contain custom tag within current JSP page.
Syntax of JSP page directive
<%@taglib uri="tagLibURI" prefix="tagPrefix" %>
Attributes of JSP include directive
  1. uri : A URI (Uniform Resource Identifier ) that indicates the tag library descriptor.
  2. prefix: This used to define the custom tag in the JSP page. Example prefix:tagname
Example of taglib directive
index.jsp
<%@ taglib uri="welcome.tld" prefix="mytag" %> <html> <head> <title>Simple custom tag example </title> </head> <body> Welcome to first simple custom tag example <h1> <mytag:example/> </h1> <br> <h2> <mytag:example/> </h2> </body> </html>
welcome.tld
<?xml version="1.0" encoding="UTF-8"?> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>hello</short-name> <uri>welcome.tld</uri> <tag> <name>example</name> <tag-class>ns.WelcomeTag </tag-class> <body-content>EMPTY</body-content> </tag> </taglib>
WelcomeTag.java
package ns; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class WelcomeTag extends TagSupport { public int doStartTag() throws JspTagException { return EVAL_BODY_INCLUDE; } public int doEndTag() throws JspTagException { try { JspWriter out=pageContext.getOut(); out.print("Custom Tag Example"); } catch(Exception e) { } return EVAL_PAGE; } }
Output
taglib directives
Download this example
1.) Deployed using Tomcat 6.0
2.) Deployed using Eclipse
 



 

Latest Tutorial


Most Visited Tutorial