• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Custom Tag Libraries and Tag Files in JSP 2.0

February 25, 2009 //  by Krishna Srinivasan//  Leave a Comment

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:

  • Java EE Tutorials
  • Servlets Interview Questions
  • New Features in Servlets 3.0
  • Asynchronous Servlets in Servlets 3

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.

Category: Java EETag: JSP, JSP 2.0

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.

Previous Post: « Error Pages in JSP 2.0
Next Post: Custom Tags in JSP 2.0 »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact