JSP Custom Tags Example Tutorial

Today we will look into JSP custom tags. Earlier we learned about JSP Action Elements, JSP EL and JSTL to avoid scripting elements in JSP pages. But sometimes even these are not enough and we might get tempted to write java code to perform some operations in JSP page. Fortunately JSP is extendable and we can create our own custom tags to perform certain operations.

Custom Tags in JSP

Let’s say we want to show a number with formatting with commas and spaces. This can be very useful for user when the number is really long. So we want some custom tags like below:

<mytags:formatNumber number="100050.574" format="#,###.00"/>

Based on the number and format passed, it should write the formatted number in JSP page, for above example it should print 100,050.57

We know that JSTL doesn’t provide any inbuilt tags to achieve this, so we will create our own custom tag implementation and use it in the JSP page. Below will be the final structure of our example project.

JSP-Custom-Tag-Project

JSP Custom Tag Handler

This is the first step in creating custom tags in JSP. All we need to do is extend javax.servlet.jsp.tagext.SimpleTagSupport class and override doTag() method.

The important point to note is that we should have setter methods for the attributes we need for the tag. So we will define two setter methods – setFormat(String format) and setNumber(String number).

SimpleTagSupport provide methods through which we can get JspWriter object and write data to the response. We will use DecimalFormat class to generate the formatted string and then write it to response. The final implementation is given below.

NumberFormatterTag.java

Notice that I am catching exception thrown by format() method and then throwing SkipPageException to prevent further loading of the JSP page.

Creating JSP Custom Tag Library Descriptor (TLD) File

Once the tag handler class is ready, we need to define a TLD file inside the WEB-INF directory so that container will load it when application is deployed.

numberformatter.tld

Notice the URI element, we will have to define it in our deployment descriptor file. Also notice the attributes format and number that are required. body-content is empty means that the tag will not have any body.

JSP Custom Tag Deployment Descriptor Configuration

We will include the jsp custom tag library in web application using jsp-config and taglib elements like below.

web.xml

taglib-uri value should be same as defined in the TLD file. All the configuration is done now and we can use it in the JSP page.

Page using JSP Custom Tag

Here is a sample JSP page where I am using our number formatter jsp custom tag.

index.jsp

Now when we run our web application, we get JSP page like below image.

JSP-Custom-Tag-Example-450x195

The JSP response page is showing the formatted number, similarly we can create more jsp custom tag handler classes. We can have multiple tags defined in the tag library.

If you have a lot of custom tag handler classes or you want it to provide as a JAR file for others to use, you need to include TLD files in the META-INF directory of the JAR file and include it in the web application lib directory. In that case, we don’t need to configure them in web.xml also because JSP container takes care of that.

That’s why when we use JSP Standard Tags, we don’t need to configure them in web.xml and we can declare them in JSP and use them. If you are not convinced, unzip the JSTL Tomcat standard.jar and you will find a lot of TLD files in the META-INF directory. 🙂

That’s all for custom tags in JSP, I hope you liked it.

By admin

Leave a Reply

%d bloggers like this: