Installing JSTL

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Chapter 1.  Understanding JSP Custom Tags


Before you can use the JSTL components, you must have a JSP 1.2-compliant Web server installed, such as Tomcat 4. Installation on other JSP Web servers should be similar. The source code we present in this book should work with any JSP Web server.

If you are not familiar with the installation of Apache Tomcat, you can refer to Appendix B, "Installing Tomcat and JSTL." In this appendix, we tell you how to acquire and install both Tomcat and JSTL.

Adding JSTL to Your Web Server

You must download and install JSTL into your Web server if you want to use the JSTL tags. The process for installing JSTL is similar to the process for installing any other tag library.

You can download JSTL from the Apache Taglibs Project site at http://jakarta.apache.org/taglibs/. From the Apache site, choose to download the Standard Tag Library. Again, the complete process for installing JSTL is covered in Appendix B.

Using JSTL in Your JSP Pages

After you have properly installed JSTL into your Web server, you can use it in any of your JSP pages. However, the JSTL libraries do not just become automatically available to each of your JSP pages. Any JSP pages that need to make use of one of the JSTL tag libraries must include an appropriate taglib directive at the top of the JSP page. As we've pointed out, the taglib directive used to access the Core taglib is as follows:

<%@ taglib uri="http://java.sun.com/jstl/ea/core" prefix="c" %> 

With this directive included near the top, this JSP page is now ready to use JSTL taglibs. It is also necessary to make the correct additions to the web.xml file and ensure that JSTL is properly installed, as we explain in Appendix B.

The following is a typical JSTL tag:

<c:forEach var="i" begin="1" end="10" step="1"> 

The first part of this tag identifies the tag to be used. In this case, we are using the tag <c:forEach>. The part of the tag immediately before the colon specifies the tag library prefix to be used. This allows multiple tag libraries to have tags named the same thing. Here, the c means that we are using the JSTL Core tag library. It would be perfectly valid to use a tag library with the prefix of d that also contained a <forEach> tag. Using the prefixes prevents tag library collisions.

Following the tag name are the parameters for this particular tag type. These parameters are specified as XML attributes, as shown in the code snippet. In our example, the forEach tag includes the parameters var, begin, end, and step. The number and exact names of these parameters will vary, depending on the individual tag you are using.

Like every XML tag, JSTL tags must connect with an ending tag. The forEach tag that we just examined must end with the following statement:

</c:forEach> 

XML tags allow you specify additional information, or other tags, between the beginning and ending tags. This practice is used extensively in JSTL. Any tags or data that occur in-between the <c:forEach> and its ending tag will be executed repeatedly as part of the forEach tag. The overall structure of such tags is as follows:

<c:forEach var="i" begin="1" end="10" step="1">    ...other tags or data... </c:forEach> 

Not every JSTL tag requires a beginning and an ending tag. Another common JSTL tag is the output, or <c:out> tag. This tag allows you to output data to the browser. The <c:out> tag is commonly used to print out variables or data retrieved from a database. The following output tag prints out the contents of the variable i+1:

<c:out value="${i+1}" /> 

As with XML, we are required to put the trailing / on the tag because this tag does not include an ending tag. All the formatting rules that apply to XML also apply to JSTL tags.

The simple output tag can be combined with the forEach tag. In the following code snippet, these two tags are used together:

<c:forEach var="i" begin="1" end="10" step="1">   <c:out value="${i}" /> </c:forEach> 

Testing the Counting Example

Now that you have seen the basics of constructing a JSP page, you are ready to execute the counting example. This example, as well as the others in this text, can be freely downloaded from the Sams Web site (http://www.samspublishing.com/). You can find these examples in a directory called example. We suggest you simply copy the example directory in its entirety to your Web root directory. The example directory contains the file that corresponds to Listing 1.4, named countJSTL.jsp. If you start Tomcat, you should be able to access the file. The URL used to display this example is http://127.0.0.1:8080/example/ch1/count/countJSTL.jsp.

Appendix B describes how to install as well as use Tomcat in conjunction with Apache. If all goes well, you should see the page shown in Figure 1.1.

Figure 1.1. The counting example.

graphics/01fig01.jpg

It is not necessary to enter the exact URL of every example in the book. We provide an index at both the chapter and book level. To view an index of all chapters and links to their examples, navigate to the URL http://127.0.0.1:8080/example. You will see the index shown in Figure 1.2.

Figure 1.2. The example index.

graphics/01fig02.jpg

Troubleshooting the Counting Example

You have to copy numerous files to various locations to ensure that the counting example will work properly. If you run into problems, reread the previous section to ensure that you followed the steps correctly. If you did, you are ready to run the counting example and most other examples from this book. Appendix B contains more specific information about troubleshooting problems related to installing JSTL.

Summary

This chapter provided a high-level overview of JSTL and tags in general. The next chapter begins the process of showing you how to create programs in JSTL. We cover such basic concepts as variables and scope.


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/ch01lev1sec3]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net