The "taglib" directive in JSP


 

The "taglib" directive in JSP

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

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.

SYNTAX:

<%@ 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. 

EXAMPLE :

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

http://www.java2s.com/Code/Jar/STUVWXYZ/Downloadjstl12jar.htm

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="http://java.sun.com/jsp/jstl/core" prefix="c" %>

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

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

Output

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>
        <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
        <taglib-location>/WEB-INF/fmt.tld</taglib-location>
      </taglib>
      <taglib>
        <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
        <taglib-location>/WEB-INF/c.tld</taglib-location>
      </taglib>
      <taglib>
        <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
        <taglib-location>/WEB-INF/sql.tld</taglib-location>
      </taglib>
      <taglib>
        <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
        <taglib-location>/WEB-INF/x.tld</taglib-location>
      </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: http://java.sun.com/j2ee/dtds/web-app_2_2.dtd.

  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.

Ads