Core JSTL[c] Mastering the JSP Standard Tag Library

   

Getting started with JSTL is easy; you need:

  • The Java 2 Platform, either J2SE (Standard Edition) or J2EE (Enterprise Edition)

  • A servlet container that supports the Servlet 2.3 and JSP 1.2 APIs

  • JSTL

If you don't plan to use J2EE features such as Java Naming and Directory Interface (JNDI) or Enterprise JavaBeans (EJB), you're probably better off with J2SE, which is a subset of J2EE.

You can download the J2SE, which is available for Linux, Solaris, and Windows, at http://java.sun.com/j2se/1.4/download.html; you can download J2EE at http://java.sun.com/j2ee/sdk_1.3. For other platforms, such as Mac OSX, check to see if a Java 2 implementation is preinstalled .

JSTL requires a servlet container that supports the Servlet 2.3 and JSP 1.2 APIs. For running the Web applications discussed in this book, I recommend either Tomcat 4.1.3 (or later) or Resin 2.1.2 (or later). See "Tomcat" on page 26 and "Resin" on page 28, for more information about those servlet containers.

Finally, of course, you need a JSTL implementation. Some servlet containers, such as Resin, already offer their own JSTL implementations, but as this book went to press, those implementations were immature. To test this book's examples, I recommend you use the JSTL Reference Implementation, which is discussed in "The JSTL Reference Implementation" on page 24.

This section shows you how to download the JSTL specification and download and install Tomcat, Resin, and the JSTL Reference Implementation. "A Simple JSTL Web Application" on page 30 discusses a simple JSTL example to get you started.

The JSTL Specification

You may find it useful to have the JSTL specification on hand while you use JSTL, so before we discuss downloading servlet containers and JSTL, let's take a look at how to download the specification. If you're not interested in the specification, you may prefer to skip ahead to one of the following destinations:

  • "The JSTL Reference Implementation" on page 24

  • "Downloading and Installing a Servlet Container" on page 26

  • "A Simple JSTL Web Application" on page 30

You can download the JSTL specification at this URL: http://jcp.org/aboutJava/communityprocess/final/jsr052/. The specification is a PDF file, so you will probably use Acrobat to view it, as shown in Figure 1-1.

Figure 1-1. The JSTL 1.0 Specification

It's not necessary to download the JSTL specification, but you might find it useful anyhow, especially for legalistic issues. From here on out, our discussion will revolve around the software required for JSTL: a JSTL implementation (which may be bundled with your servlet container) and a servlet container.

The JSTL Reference Implementation

The JSTL Reference Implementation is a mature JSTL implementation that's the last word on any ambiguities in the specification. But it's not fast. For a speedy implementation, you should look to your servlet container; for example, Resin's JSTL is called Fast JSTL . If your servlet container does not provide a JSTL implementation, then you must come up with one of your own. Fortunately, the JSTL Reference Implementation is freely available; you can download it from this URL: http://jakarta.apache.org/builds/jakarta- taglibs /releases/standard. That webpage is shown in Figure 1-2.

Figure 1-2. Downloading the JSTL Binary Distribution

As you can see from Figure 1-2, you have two choices: you can download either the 1.0 release or a nightly build. I recommend that you download the former for the sake of reliability. If you download the 1.0 release, you can choose between ZIP and TAR files.

Once you've downloaded the Reference Implementation, you can unzip or untar the file that you downloaded in a directory of your choice. Decompressing that file will produce a top-level directory, so you don't have to worry about scattering files about in the directory of your choice.

Now you know how to download and install the reference implementation. In "A Simple JSTL Web Application" on page 30, we discuss how to use it.

The following two sections discuss how to download and install Tomcat and Resin. If you already have a servlet container installed that supports the Servlet 2.3 and JSP 1.2 APIs, you can skip those sections and start reading at "A Simple JSTL Web Application" on page 30.

Downloading and Installing a Servlet Container

You can use the JSTL Reference Implementation with any servlet container that supports Servlet 2.3 and JSP 1.2. I recommend that, if possible, you use two servlet containers installed on your desktop to test your Web applications. Using servlet containers that are stored on your machine will give you more control over how those containers are configured and used. Using more than one servlet container to test your code will ensure that you don't inadvertently use nonportable constructs.

This section discusses downloading two servlet containers: Tomcat 4.1.3 and Resin 2.1.2. All of the code in this book was tested with both of those servlet containers.

Core Approach

Use two servlet containers to test your code to ensure quality and portability.

Tomcat

At the time this book was written, Tomcat 4.1.3 was the latest version of the reference implementation for the Servlet and JSP APIs. You can download Tomcat at this URL: http://jakarta.apache.org/builds/jakarta-tomcat-4.0/. That webpage is shown in Figure 1-3.

Figure 1-3. Downloading Tomcat

As you can see from Figure 1-3, you can download the latest Tomcat 4 milestone or nightly releases. I suggest that, for stability, you download the latest milestone release. Like the JSTL Reference Implementation, you can download Tomcat in a ZIP or TAR file. Once you have downloaded that file, decompress it and a top-level directory will be created.

By default, Tomcat will run on port 8080 instead of the standard port ” 80 ”to avoid conflicts with other Web services that may be using the standard port. If you're not using the standard port, you might want to reconfigure Tomcat to run on that port so you don't have to include the port number in your URLs. To change that port, just edit the file $TOMCAT/conf/server.xml , where $TOMCAT represents the top-level directory that was created when you decompressed the ZIP or TAR file that you downloaded.

Once you open that file, search for the string port=8080 and change 8080 to 80 , like this:

<!-- Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 --> <Connector className="org.apache.coyote.tomcat4.CoyoteConnector" port="80" minProcessors="5" maxProcessors="75" enableLookups="true" redirectPort="8443" acceptCount="10" debug="0" connectionTimeout="20000" useURIValidationHack="false" />

Now you're ready to run Tomcat. In the $TOMCAT/bin directory, you will find the files startup.sh and startup.bat that you use to start Tomcat for UNIX and Windows, respectively. The files shutdown.sh and shutdown.bat in the same directory shut down Tomcat on UNIX and Windows, respectively. For convenience, I recommend that you create desktop icons for starting up and shutting down.

After you start Tomcat, access this URL to make sure Tomcat is working: http://localhost. [12] You should see a webpage similar to the one shown in Figure 1-4.

[12] If you're not using port 80 , use this URL: http://localhost: XXXX , where XXXX represents the port number.

Figure 1-4. Running Tomcat

Once you have Tomcat installed and running, you're ready to start using JSTL. See "A Simple JSTL Web Application" on page 30 for details on how to do that.

Resin

Resin is one of the most popular servlet containers because it's stable and fast, with numerous unique features that support XML. The process for downloading and installing Resin is similar to the same process for Tomcat: Download Resin, decompress the file that you downloaded, thereby creating a top-level directory, and modify the port number, if desired. You can download Resin at this URL: http://www.caucho.com/download. That webpage is shown in Figure 1-5.

Figure 1-5. Downloading Resin

As you can see from Figure 1-5, you can download a ZIP file or a TAR file. Once you've downloaded the file, decompress it and a top-level directory will be created.

As with Tomcat, you may want to change the port that Resin runs on to the standard 80 port so that you don't have to include the port number in your URLs. To do that, edit the file $RESIN/conf/resin.conf and change the port number like this: [13]

[13] $RESIN represents the top-level directory created when you decompressed the downloaded file.

<http port='80'/>

In the $RESIN/bin directory you will find an executable for Resin named httpd.exe for Windows and httpd.sh for Unix. Unlike Tomcat, Resin has no application for shutting itself down, because Resin creates a small window that lets you restart Resin or shut it down.

Once you've started Resin, you can verify that it's working by accessing the http://localhost URL, as shown in Figure 1-6.

Figure 1-6. Running Resin

   

Категории