Suggested Pages

Tuesday, May 1, 2012

Example of JSP Custom Taglib

In this post we see how to create a custom taglib using Maven 2.0 to build and manage the project. Most of people working in the IT sector know the usefullness of custom taglib over scriptlet code, but because of projects that require frequent releases, create a custom taglib seems usually a waste of time.
However, many developers can agree with me in saying that a waste of time is analyze code in order to find bugs inside JSP pages made with scriptlet code.
Use a custom taglib, forces the developers to do a deep analysis on the real needs of applications; they have to understand what kind of utilities or macros are needed. In fact, a custom taglib is a sort of macro that may be used in different part of the project. In this post you can see a taglib that performs a trim of a String. Not exactly what you need in a large web application, but suppose you need something more difficult, you can complicate and improve this taglib. The result will be a good feedback in term of clearness and reusability.

Step 1: Add dependencies in your pom.xml

A custom taglib, mainly, consists of : a jar to put inside WEB-INF/lib directory and a file with tld extension to put inside WEB-INF directory. The jar contains the java class that implements your specific logic and the file with tld extension the description of attributes of new tag.
We choose to build it with Maven 2.0 so we have to write in our pom.xml the required dependencies to develop taglib class. As you can see these dependencies have a provided scope, because jsp-api and servlet-api are provided by the container (Tomcat or JBoss for example).


Step 2: Create a Java Class that extends TagSupport

This Java Class performs only a simple trim of a String after checking it's not null and not empty.

package com.simonefolinojavablog;


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

public class TrimTagLib extends TagSupport {

 private String var;

 private int scope;

 public int doStartTag() throws JspException {

  String attribute = (String) pageContext.getAttribute(var, scope);

  if (attribute != null && !attribute.equals("")) {
   String attributeTrimed = attribute.trim();
   JspWriter out = pageContext.getOut();
   try {
   } catch (IOException e) {

  return (SKIP_BODY);

 public String getVar() {
  return var;

 public void setVar(String var) {
  this.var = var;

 public int getScope() {
  return scope;

 public void setScope(int scope) {
  this.scope = scope;


Step 3: Create a file trim.tld

Create a file trim.tld and put it under WEB-INF/ directory of your web application. As you can see there are two attributes: var e scope. The former describes the variable you want to trim, the latter describes the scope to search this variable . Both the variables are mandatories.

<taglib version="2.0" xmlns:xsi="" xmlns="" xsi:schemalocation="">

 <description>JSTL 1.1 core library</description>
 <display-name>Custom Taglib</display-name>

        Catches any Throwable that occurs in its body and optionally
        exposes it.

Step 4: Write the path to your file web.xml

Within web.xml, write the path to your file trim.tld and the URI that identifies the taglib.

<web-app version="2.4" xmlns:xsi="" xmlns="" xsi:schemalocation="">


Step 5: Use taglib inside a JSP

Inside JSP pages we have to put the declaration of our custom taglib with the prefix that you're going to use in your jsp code. Notice that scope='3' it's a form of hard-coding of PageContext.SESSION_SCOPE whose value is 3. Obviously you can do better, providing a mapping inside TrimTagLib class but i prefered not to get it complex at this time. This JSP is telling to search an attribute with the name "Description" in Session scope.

 <%@taglib prefix="c" uri="" %>
<%@taglib prefix="trim" uri="http://com.simonefolinojavablog/trim" %>
   <trim:trim var='Description'  scope='3'/>

No comments :

Post a Comment

Suggested Pages