The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.servlet.jsp.jstl.core  [6 examples]

e1064. Using the Java Standard Tag Library (JSTL) in a JSP Page

There are four tag libraries that make up the JSTL; see the javax.servlet.jsp.jstl.* packages for examples that use these tag libraries. JSTL is not yet a standard part of a JSP container. Hence, in order to use the JSTL in a web application, you must include a copy the JSTL library code in the WEB-INF directory of your web application. These files are available in the Web Services Developer Pack, which can be download from http://java.sun.com/webservices/webservicespack.html.

A tag library has two parts: a Tag Library Descriptor (TLD) file and a JAR file. The following ant elements copy the TLD files from the Web Services Developer Pack into WEB-INF/tld and the JAR files into WEB-INF/lib. These elements should be added to the build target in your build.xml file:

    <%-- Copy TLD files from WSDP installation --%>
    <copy todir="${build}/WEB-INF/tld">
      <fileset dir="${jwsdp.home}/tools/jstl/tld">
        <include name="*.tld" />
      </fileset>
    </copy>
    
    <%-- Copy JSTL jar files from WSDP installation --%>
    <copy todir="${build}/WEB-INF/lib">
      <fileset dir="${jwsdp.home}/tools/jstl">
        <include name="jstl.jar" />
      </fileset>
      <fileset dir="${jwsdp.home}/tools/jstl/standard/lib">
        <include name="standard.jar" />
      </fileset>
    </copy>
In order for a JSP page to use the JSTL, it must declare it's use on the page using the taglib directive. Here's an example that declares the use of all four JSTL tag libraries:
    <%-- Core --%>
    <%@ taglib uri="/WEB-INF/tld/c-rt.tld" prefix="c_rt" %>
    
    <%-- I18N Formatting --%>
    <%@ taglib uri="/WEB-INF/tld/fmt-rt.tld" prefix="fmt_rt" %>
    
    <%-- SQL --%>
    <%@ taglib uri="/WEB-INF/tld/sql-rt.tld" prefix="sql_rt" %>
    
    <%-- XML --%>
    <%@ taglib uri="/WEB-INF/tld/x-rt.tld" prefix="x_rt" %>
The tags in the JSTL core tag library can then be referenced using the specified prefix as in:
    <c_rt:if test='<%= request.getParameter("param") != null %>'>
        <%= request.getParameter("param") %>
    </c_rt:if>
See also e1065 Enabling the JSTL Expression Language in a JSP Page.

 Related Examples
e1065. Enabling the JSTL Expression Language in a JSP Page
e1066. Getting a Request Parameter Using JSTL in a JSP Page
e1067. Saving Data Using JSTL in a JSP Page
e1068. Saving and Emitting HTML Fragments Using JSTL in a JSP Page
e1069. Conditionally Generating Output Using JSTL in a JSP Page


© 2002 Addison-Wesley.