For more information please read

We know JSP tags simplify JSP page development and maintenance works in our day-to-day life. JSP technology also provides a mechanism for encapsulating other types of dynamic functionality in custom tags, which are extensions to the JSP language. Some examples of tasks that can be performed by custom tags include operating on implicit objects, processing forms, accessing databases and other enterprise services such as email and directories, and implementing flow control. Custom tags increase productivity because they can be reused in more than one application.

Custom tags are distributed in a tag library, which defines a set of related custom tags and contains the objects that implement the tags. The object that implements a custom tag is called a tag handler. JSP technology defines two types of tag handlers: simple and classic. Simple tag handlers can be used only for tags that do not use scripting elements in attribute values or the tag body. Classic tag handlers must be used if scripting elements are required.

A tag file is a source file containing a reusable fragment of JSP code that is translated into a simple tag handler by the web container. Tag files can be used to develop custom tags that are presentation-centric or that can take advantage of existing tag libraries, or by page authors who do not know Java. When the flexibility of the Java programming language is needed to define the tag, JSP technology provides a simple API for developing a tag handler in the Java programming language.

A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on a tag handler. The web container then invokes those operations when the JSP page’s servlet is executed.

Custom tags have a rich set of features. They can

Be customized by means of attributes passed from the calling page.

Pass variables back to the calling page.

Access all the objects available to JSP pages.

Communicate with each other. You can create and initialize a JavaBeans component, create a public EL variable that refers to that bean in one tag, and then use the bean in another tag.

Be nested within one another and communicate by means of private variables.

For more information please read

Now we will see how to create JSP custom tag

For this tutorial we will create maven based web project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.


The following configurations are required in order to run the application

JDK 1.7
Tomcat 7
Have maven installed and configured
jsp api dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse.

Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : jsp

Step 2. Modify the pom.xml file as shown below.

<project xmlns="" xmlns:xsi=""

    <name>jsp Maven Webapp</name>


        <!-- servlet & jsp -->

            <!-- maven compiler plugin definition -->

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create tag handler class

package com.roytuts.jsp.taglib;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class WishTagHandler extends TagSupport {

  private static final long serialVersionUID = 1L;

  private String name;

  public int doStartTag() throws JspException {
    JspWriter jspWriter = pageContext.getOut();
    try {
      jspWriter.println("Hello " + name);
    } catch (IOException e) {
    return SKIP_BODY;

  public String getName() {
    return name;

  public void setName(String name) { = name;


The attribute ‘name’ will be used as an input field to the taglib.

Step 5. Create tag handler tld file under WEB-INF/tld/ directory

<?xml version="1.0" encoding="UTF-8"?>
    <!-- taglib version -->
    <!-- jsp version -->
    <!-- meaninful short name -->
    <!-- taglib description -->
    <info>taglib for displaying name</info>
    <!-- optional -->
        <!-- taglib name which will be used as a taglib name -->
        <!-- taglib class -->
        <!-- taglib function description -->
        <info>display function</info>
        <!-- taglib attribute -->

Step 6. Create a index.jsp file

<%@taglib prefix="test" uri="/WEB-INF/tld/wishTagHandler.tld"%>
<title>JSP Custom Taglib example</title>
    <font color="blue"> <test:wish name="Soumitra" /> </font>

In index.jsp page we have included taglib with prefix ‘test’ and the actual file in uri section.

Step 7. Now deploy the web application into Tomcat 7 server, you will see page appears with below output in blue color text

Hello Soumitra

There is no change in default generated web.xml file.

Thanks for reading.


I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *