Creating custom JSP tags

Posted on 04-01-2013 12:46 by graham
This tutorial describes how to create your own custom JSP tags in Java applications. As an example, we will create a tag that displays a list of action messages.


This tutorial assumes you have a basic Web application created.

Creating custom tag implementation

We create a custom tag implementation by implementing the Tag interface.
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class ActionErrors implements Tag, Serializable
private static final long serialVersionUID = 7981162057964780076L;
private Tag parent;
private PageContext pageContext;
private List<String> errors;

public int doEndTag() throws JspException
return EVAL_PAGE;

public int doStartTag() throws JspException
if (this.errors != null && !this.errors.isEmpty())
JspWriter page = pageContext.getOut();

for (String error : this.errors)
page.write("<li>" + error + "</li>");

catch (IOException e)
throw new JspTagException("An IOException occurred.");
return SKIP_BODY;

public Tag getParent()
return parent;

public void release()
this.pageContext = null;
this.parent = null;
this.errors = null;

public void setPageContext(PageContext pc)
this.pageContext = pc;

public void setParent(Tag parent)
this.parent = parent;

public void setErrors(List<String> errors)
this.errors = errors;

public List<String> getErrors()
return errors;

Creating tag definitions in the TLD file

In your project directory structure, create folder tld under /src/main/webapp/WEB-INF. In this directory, create a file with the name that best describes a group of tags you want to create. We will call this file my-tags.tld. The contents of the file looks as follows:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"

<shortname>My Tags</shortname>
<info>Action errors</info>

<info>Action Errors</info>


NOTE: In earlier versions of the Spring Framework it was necessary to put a reference to the TLD file in your web.xml file. With Spring 3.0, this is no longer necessary.

Using your custom tag

If you want to use your custom tag, you need to tell JSP where to look for its definition. You do this by adding the following at the beginning of your JSP file:
<%@ taglib uri="/WEB-INF/tld/my-tags.tld" prefix="mytags" %>

Now, you can use the tag the following way:
<mytags:errors errors="${errorMessages}" />

That's it!


Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems