Using Custom Tags in JSP

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.

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.

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>>

  • Share/Bookmark
JEE, Jsp, Tech Notes

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

(required)

(required)