ADS

Creating Custom Tags In Jsp - Example

The following example illustrates creating your own tags in JSP. The tags you create here will have a prefix and a tag name.

Why should I write tags?

Tags are used to reduce the size of a JSP page by trimming lines of lengthy, redundant code in JSP pages and put them in a class and execute it every time you declare the tag. For example, you want to insert a student record in JSP. For this, you need to load the driver class, create connection object, execute the query.  But for all this, you can simply write a tag, say <student:insert> which will have attributes sno,sname and age.

<student:insert sno="101" sname="smith" age="20"/>

Now each time this tag is executed, a student record is inserted. This is not a pre-defined tag but the tag that you should write, meaning, you should implement the code in a class and link it up with this tag.

Here are some key points about the tags:
  • Every tag will have a name and a corresponding class.
  • The class name need not be same as the tag name.
  • The attributes of a tag are the parameters to that particular method. The attribute names must be same as the member variables.
  • There can be required (mandatory) attributes for a tag.
  • You can also write a tag with no attributes.
Let us now start by writing a simple tag with no attributes. The aim of this tag is to print a message.

Folder structure
webapps
           |_ jsp8
                   |_ WEB-INF
                                    |_ mytags.tld
                                    |_ classes
                                                |_ tags
                                                        |_ MyTag.java
                   |_ index.jsp

index.jsp

<%@taglib prefix="t" uri="WEB-INF/mytags.tld"%>
<html>
<body>
<t:footag/>
</body>
</html>

Here the tag prefix is mandatory and the uri is where the custom tags are defined. You can have any prefix.

mytags.tld

A tld file must be written that maps tags to their corresponding classes.
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>

    <tag>
    <name>footag</name>
    <tag-class>tags.MyTag</tag-class>
    <body-content>empty</body-content>
     </tag>

</taglib>

<name> is the name of the tag that comes after the prefix (after the :)
<tag-class> is the class in which the tags functionality is provided.
<body-content> defines if the tag must be empty or contain other tags along with text or tag dependent text etc. There are three possible options:

empty: Meaning that the tag should not be given a body. For example,
<t:footag>text</t:footag> is false, whereas <t:footag></t:footag> or <t:footag/> are true.

scriptless: Meaning that the tag can contain body including but not limited to text and other html tags. This is the default.

tagdependent: Meaning that the body can contain tag-dependent content, for example you might create a tag that takes an SQL query and executes it, so you'll write the corresponding SQL query there.

MyTag.java

This class contains the functionality of the tag, here written in the doTag() method.
package tags;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class MyTag extends SimpleTagSupport
{
     public void doTag() throws JspException, IOException
    {
    JspWriter pw=getJspContext().getOut();
    pw.println("This is in the tag");
    }
}

Here we got the implicit object out so that we can write on the JSP page declaring this tag.

References

  1. http://docs.oracle.com/javaee/5/tutorial/doc/bnama.html#bnamg
  2. http://www.tutorialspoint.com/jsp/jsp_custom_tags.htm

Subscribe to receive free email updates:

ADS