Custom Tag Libraries and Tag Files in JSP 2.0

With previous versions of JSP developing custom tag libraries was possible only by writing Java classes. As a result knowledge of Java was a must. JSP 2.0 introduces a new way of developing custom tag library using plain JSP. This enables JSP developers, who dont know Java, to develop custom tag libraries as Tag files. JSP 2.0 also provided added features of passing parameters to tag files. I will demonstrate this in the following examples.

also read:

1) File Name : firstPage.jsp

<%@ page contentType="text/html" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/myCustomTags" %>
<html>
	<body bgcolor="white">
		Random Number : <my:generateRandomNum />
	</body>
</html>

2) File Name : generateRandomNum.tag

<%@ tag body-content="empty" %>
<%
	out.println(java.lang.Math.random());
%>

3) Output

Random Number : 0.029871659130147776

Explantion:

In my first example I use a tag file to generate random numbers. I will first explain the code in generateRandomNum.tag file. This is just a simple file with a .tag extension and a tag directive. In this file I write the logic to generate random numbers using the java.lang.Math class. Once this file is ready place the file under any location below WEB-INF directory. I have placed it inside /WEB-INF/tags/myCustomTags directory.

Now in the firstPage.jsp I use a taglib directive. In the taglib directive I set the value for two attributes : prefix and tagdir. The prefix sets a prefix to invoke the custom tag and the tagdir attribute
tell the container that the tag file is placed at the location specified. Next I invoke the tag file simply by using
the prefix and the name of the tag file, without the .tag extension

4) File Name : secondPage.jsp

<%@ page contentType="text/html" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags/myCustomTags" %>
<html>
	<body bgcolor="white">
		Sum of two numbers : <my:addNumbers firstNum="10" secondNum="20" />
	</body>
</html>

5) File Name : addNumbers.tag

<%@ tag body-content="empty" %>
<%@ attribute name="firstNum" required="true"%>
<%@ attribute name="secondNum" required="true"%>

${firstNum + secondNum}

6) Output

Sum of two numbers : 30

Explantion:

In this second example I explain the usage of passing attributes to tag files. In the file addNumbers.tag I have used a directive called attribute. This is basically used to tell the web container that this tag file expects an attribute and whether the attribute is mandatory or optional.

We should use the attribute directive for each attribute passed from the invoking jsp file. I declare two attributes and use them to perform an add operation using Expressions Langauge (EL).

In the file secondPage.jsp I invoke the tag and pass attributes with some values. The values will be received by the attributes declared in the tag file and will be used to perform the addition.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better