Share on Google+Share on Google+

The "taglib" directive in JSP

In this section, we will discuss about JSP "taglib" directive with a small example.

The "taglib" directive in JSP

In this section, we will discuss about JSP "taglib" directive with a small example. The JSP "taglib" directive is use to define tag library, which is the collection of tags and it also defines the prefix for the tags. To use custom tags in your JSP , you have to use it's syntax to import the classes which contain the definition of these tags.


<%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %> 

The following table describe the attribute of the taglib directive :

Attribute Name         Description
uri          Locates the TLD file of a custom tag
prefix          Defines a prefix string to be used for
        distinguishing a custom tag instance

 A Tag library is of two kinds :-

  • Standard(pre-defined)
  • Custom(user-defined)

The "prefix" attribute is a must for custom as well as standard tags in JSP. Example of standard tag library is JSTL or JSP Standard tag library. 


In this example , we are using "Standard" library(JSTL) library. First, you have to download ' jstl-1.2.jar ' from the below link:

Second , paste it in Tomcat's "lib" folder. for example : 'C:\Program Files\Tomcat 6.0\lib '.

Final Step , run the JSP code given below in your web browser :-

<%@ taglib uri="" prefix="c" %>

<p>Simple Example for C:out</p>

Multiply 85 and 2::<c:out value="${85*2}" />


Download Source Code

Another Way of using JSTL

Download JSTL support from the Jakarta Website then follow these steps:

  1. Download the JSTL archive (binaries not source) from the Jakarta Website. Unzip/untar the file.
  2. Copy the jar files you've extracted to common/lib in your Tomcat installation (although you won't need all the jar files for our project). This makes the JSTL jar files available to any of your Web applications.
  3. For any Web application for which you want to use JSTL, copy the .tld files to the WEB-INF directory in your Web application.
  4. For your JSTL Web application, edit your web.xml file and add the following entries:
  5. <taglib>
  6.  These entries let your Web application use the expression language (EL) versions of the JSTL tag libraries. Position of these entries matters! If you're not sure where to put them, the definitive guide to web.xml options and ordering is defined in the document type definition (DTD) at:

  7. When you create a JSP page that uses JSTL, put it in your Web application's main directory, just like other JSP and HTML pages. You can name this page whatever you want, but it should have a ".jsp" extension.



Posted on: June 18, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null