Using Custom Tags in JSP - JEE
Home » JEE, Jsp, Tech Notes

Using Custom Tags in JSP

16 April 2009 One Comment

If we look at the open source frameworks for presentation tier, you will find most of them providing custom tag libraries for the Jsp development. This should be enough to tell us the importance of custom tags in jsp technology. The biggest advantage of custom tag is improved reusability. There are many other advantages like minimization of Java code in Jsp, ease in usage of custom tag, reduction in the size of jsp code, etc. Considering the importance of custom tag, it is always good to identify the external and internal tag libraries in early design phases. Application specific custom tags will also help in reducing the effort required in development of a jsp. Let us understand the details of custom tag and finally write our own custom tag.

What is a custom tag?

Following points are the features of the custom tag technology.

  • Custom tags are user defined custom components developed in Jsp language
  • Custom tags encapsulate recurring code
  • Custom tags are deployed as a tag library
  • During translation of Jsp, tags result in method calls to Tag Handler object
  • Custom tags can be nested
  • Custom tags can modify the response generated by the calling page
  • All implicit objects in Jsp are available in custom tag also.

How to implement custom tag?

During early design phase, we can identify the reusable components across Jsps. Further refinement of these components can tell us the custom tags for our application. Each custom tag requires a set code elements to be implemented.

With this the custom tag is ready to use in jsp. Now let us take a simple example, to give a name as input to custom tag and print that name in jsp.

In this example the tag class extends from javax.servlet.jsp.tagext.TagSupport. We have one attribute – name, to be used to give name input.

com.myorg.jsptutorial. CustomExampleTag.java

package com.myorg.jsptutorial;
import java.io.IOException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class CustomExampleTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String name = null;
	public String getName() {
		return name;
	}
	public void setName(String aName) {
		this.name = aName;
	}
    public int doStartTag() throws JspTagException {
    	try{
   			JspWriter out = pageContext.getOut();
   			if(name != null && !("".equals(name))){
    			out.print("Hello " + name + "!!!");
    		}else{
    			out.print("Please identify yourself!!!");
    		}
    	}catch(IOException ioe){
    		ioe.printStackTrace();
    	}
        return SKIP_BODY;
    }

}

Next is the xml tag library descriptor.

/WEB-INF/ custom-tag-example.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">

  <description>Custom Tag Example</description>
  <display-name>CustomTagExample</display-name>
  <tlib-version>1.0</tlib-version>
  <short-name>cte</short-name>
  <uri>http://myorg.jsptutorial.com/customtagexample</uri>
<tag>
	<description>
		Custom tag example
	</description>
	<name>customTagExample</name>
	<tag-class>com.myorg.jsptutorial.CustomExampleTag</tag-class>
	 <body-content>JSP</body-content>
	<attribute>
      <name>name</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
</tag>
</taglib>

Last is the Jsp using custom tag. Here we are doing two things.

  • Declare the tag library on top of jsp using taglib directive. The prefix will be used in each tag call we place. We are giving input through the attribute name.

CustomTagExample.jsp

<%@ taglib uri="/WEB-INF/custom-tag-example.tld" prefix="cte" %>

<html>

<head>

<title>Custom Tag Example</title>

</head>

<body>

  <h1><cte:customTagExample name="Jsp Tutorial Author"/></h1>

</body>

</html>

When we call this jsp, the output is displayed like this.

Using Custom Tag in JSP

 

<<Previous   Home   Next>>

 

More Related Posts in JEE, Jsp, Tech Notes

One Comment »

  • shiva said:

    hi,
    while executing the CustomExampleTag.java program i am getting some errors…..how to resaolve these errors…

    package javax.servlet.jsp does not exist
    import javax.servlet.jsp.JspWriter;
    ^
    java:5: package javax.servlet.jsp.tagext does no
    import javax.servlet.jsp.tagext.TagSupport;
    ^
    .java:7: cannot find symbol
    symbol: class TagSupport
    public class addTH extends TagSupport{
    ^
    java:19: cannot find symbol
    symbol : class JspWriter
    location: class addTH
    JspWriter out = pageContext.getOut();
    ^
    java:19: cannot find symbol
    : variable pageContext
    location: class addTH
    JspWriter out = pageContext.getOut();
    ^

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.