IBM Skip to main content
Search for:   within 
      Search help  
     IBM home  |  Products & services  |  Support & downloads   |  My account

developerWorks > Java technology
developerWorks
JSP best practices: Update your JSP pages with JSTL
63 KBe-mail it!
Contents:
JSTL setup
Incorporating JSTL tags
Resources
About the author
Rate this article
Related content:
JSP best practices series
JSP taglibs: Better usability by design
Subscriptions:
dW newsletters
dW Subscription
(CDs and downloads)
Standardized JSTL tags bring even more functionality to your Web pages

Level: Introductory

Brett McLaughlin (mailto:brett@oreilly.com?cc=&subject=Update your JSP pages with JSTL)
Author, O'Reilly and Associates
27 May 2003

Column iconThe JSP Standard Tag Library (JSTL) is a collection of custom tags that enable many common Web site functions in a standardized format. In this installment of JSP best practices, Brett McLaughlin introduces JSTL and shows you how to convert your JSP pages to use its highly functional tags.

One of the strengths of JSP technology is its custom tag library facility. In addition to the core JSP tags such as jsp:include, the custom tag library facility lets you create unique tags to describe behavior that is specific to your site or application. Once you've created a custom tag library (taglib for short) you can reuse the tags as you extend your project or move on to other projects. You can also pass on your taglib to other developers for use on their sites or on different parts of your Web application. In a future installment of this series, I'll show you how to create your own custom tag library, but for now we'll focus on the JSP Standard Tag Library, or JSTL.

JSTL is actually a collection of custom tag libraries that have been standardized for JSP 1.2. Many of the JSTL tags are analogous to the core JSP tags, although they do much more. For example, the c:import tag is very similar to the include and jsp:include directives we've worked with in the previous installments of this series (see Resources). Like includes, c:import helps you populate the pages of your Web site or Web application, but its functionality extends beyond what the core JSP tags can do.

In this installment of JSP best practices, we'll use the c:import tag to learn about converting a Web page from JSP to JSTL. Before we can begin working with the new tag, however, we need to load JSTL onto your Web container.

JSTL setup
While JSTL is expected to be bundled into the next wave of Web containers, you currently have to set it up for yourself. Follow these step-by-step directions to load JSTL onto your Web container:

  1. Download jakarta-taglibs-standard-current.tar.gz from apache.org.

  2. Expand the downloaded archive.

  3. Copy jakarta-taglibs/standard-1.0.3/tld/c.tld to your WEB/INF/tlds directory.

  4. Copy all the JAR files in jakarta-taglibs/standard-1.0.3/lib to your WEB-INF/lib directory.

  5. Add the entry shown below to your WEB-INF/web.xml deployment descriptor.
    
         <taglib>
           <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
           <taglib-location>/WEB-INF/tlds/c.tld</taglib-location>
         </taglib>
    


  6. Restart your Web container.

That's the crash-course version of the current JSTL setup, but in most cases it should be sufficient. See the Resources section if you need more detailed instructions.

Incorporating JSTL tags
If you want to use any of the JSTL tags on a JSP page, you'll first have to declare the tag library prefix and URI on that page. We're using the core JSTL library, so the standard prefix to use is c (for core). Listing 1 shows a Web site index file with the taglib directive added to it:

Listing 1. The taglib directive for an index file

<%@ page language="java" contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
     <title>newInstance.com</title>
     <meta http-equiv="Content-Type" content="text/html; 
       charset=iso-8859-1" />
     <link href="/styles/default.css" rel="stylesheet" type="text/css" />
</head>

<body>

<jsp:include page="header.jsp" flush="true">
     <jsp:param name="pageTitle" value="newInstance.com"/>
     <jsp:param name="pageSlogan" value=" " />
</jsp:include>
<%@ include file="/navigation.jsp" %>
<jsp:include page="bookshelf.jsp" flush="true" />

<jsp:include page="/mt-blogs/index.jsp" flush="true" />

<%@ include file="/footer.jsp" %>
</body>
</html>

After you've added the JSTL taglib to your JSP page you can use any tag in the core library, just by prefixing the tag with c. To see how this works, we'll try converting a page from the jsp:include tag to the c:import tag. Listing 2 shows the jsp:include example from the second installment in this series (see Resources) converted to use the new c:import tag.

Listing 2. Converting to JSTL

<%@ page language="java" contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
     <title>newInstance.com</title>
     <meta http-equiv="Content-Type" content="text/html; 
       charset=iso-8859-1" />
     <link href="/styles/default.css" rel="stylesheet" type="text/css" />
</head>

<body>

<jsp:include page="header.jsp" flush="true">
     <jsp:param name="pageTitle" value="newInstance.com"/>
     <jsp:param name="pageSlogan" value=" " />
</jsp:include>
<%@ include file="/navigation.jsp" %>
<c:import url="bookshelf.jsp" />

<c:import url="/mt-blogs/index.jsp" />

<%@ include file="/footer.jsp" %>
</body>
</html>

The only difference between this code and the previous JSP code is that we've used the url attribute in c, rather than the page attribute in jsp:include. Pretty simple, huh? For a fun exercise, try converting a page for yourself. Just take the JSP code we used in "Manipulate dynamic content with jsp:include" (the second installment in this series; see Resources) and convert it to JSTL as shown in Listing 2.

If you've been paying close attention, you probably noticed that I avoided changing the header inclusion code in Listing 2. As you may recall, the header code contains parameters, which add another layer of complexity to the discussion here. In the next installment of JSP best practices, you'll learn how to pass parameters using c:import. I'll also show you some of the features of the c:import tag, which does a whole lot more than the simple jsp:include tag. Until then, practice the simple JSTL conversion technique you've learned here, and I'll see you online.

Resources

About the author
Photo of Brett McLaughlinBrett McLaughlin has been working in computers since the Logo days (remember the little triangle?). He currently specializes in building application infrastructure using Java and Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine. Contact Brett at brett@oreilly.com.


63 KBe-mail it!

What do you think of this document?
Killer! (5) Good stuff (4) So-so; not bad (3) Needs work (2) Lame! (1)

Comments?



developerWorks > Java technology
developerWorks
  About IBM  |  Privacy  |  Terms of use  |  Contact