JSP Standard Tag Library eases Webpage
development
Tutorial Details:
JSP Standard Tag Library eases Webpage development
JSP Standard Tag Library eases Webpage development
By: By Steve Small
Learn how JSTL improves upon JSP for simpler Webpage implementation
he 1996 introduction of Java servlets made Java a reasonable choice for dynamic Webpage development. The subsequent debut of JavaServer Pages (JSP) followed by the inclusion of support for JSP tags were logical evolutionary steps toward fast, maintainable Java Webpage implementation. But the mid-2002 release of JSTL (JSP Standard Tag Library) represents perhaps the biggest step yet in speeding and simplifying the development process further.
In this article, I explain JSTL's capabilities and cover everything you need to get started with JSTL. It's assumed you have a basic understanding of Java, JSP, XML, and setting up a Web container. If you're not comfortable with these topics, you might want to browse the background references in Resources . Additional assumed knowledge is described in the XML and SQL sections below.
Installing JSTL support
For our JSTL installation example, we use Tomcat 4.1 (although any servlet container that supports the Servlet 2.3 and JSP 1.2 specifications should work). First, download Tomcat and follow the setup instructions. (Note that JSTL requires a JSP 1.2 Web container.)
Start Tomcat with tomcat4 start and load the index.html page to make sure Tomcat is alive and well.
Next, you'll need to install JSTL support. You can download JSTL support from the Jakarta Website then follow these steps:
Download the JSTL archive (binaries not source) from the Jakarta Website. Unzip/untar the file.
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.
For any Web application for which you want to use JSTL, copy the .tld files to the WEB-INF directory in your Web application.
For your JSTL Web application, edit your web.xml file and add the following entries:
http://java.sun.com/jstl/fmt
/WEB-INF/fmt.tld
http://java.sun.com/jstl/core
/WEB-INF/c.tld
http://java.sun.com/jstl/sql
/WEB-INF/sql.tld
http://java.sun.com/jstl/x
/WEB-INF/x.tld
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 .
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.
The basics
First, all JSTL pages are also JSP pages. JSTL is just a superset of JSP functionality.
Also, all JSTL tags are valid XML. That means if you treat the context of a page outside the JSTL tags as template text (which will normally be HTML), the remaining JSTL tags must parse as valid XML. This has some important implications, most of which are good.
JSTL provides a set of four standard tag libraries (core, internationalization/format, XML, and SQL) and support for an EL. A primary design goal for JSTL and the EL was to simplify Webpage development and implementation.
In this article, we follow the JSTL specification's naming convention and refer to JSTL tags as actions. A JSTL tag corresponds to some action; calling them actions explicitly remind us that they add dynamic behavior to an otherwise static page.
The JSTL tag library comes in two versions: one lets you plug in the standard JSP expressions you've used in the past, such as <%= . . . %> , and one uses a JSTL EL. I further discuss EL support in JSTL below.
EL support
To understand the current state of EL support in JSTL, let's examine how the relevant specifications have been handled. The Java Specification Request (JSR) expert group members decided, for good reasons, that an expression language specification should be part of the JSP, not the JSTL, specification. Complete specification of an expression language will be part of the JSP 2.0 specification. Since JSTL 1.0 was finalized before JSP 1.3, the JSTL authors had to make an educated guess (which will probably turn out to be quite good) on an EL support implementation for JSTL 1.0. A JSTL maintenance release will coincide with finalization of the JSP 1.3 specification and will contain any tweaks to the EL required for consistency with the final JSP 1.3 specification.
The bottom line is that the EL described here may change a bit in an upcoming JSTL release. However, any changes will likely be small.
The EL simply defines a powerful language for expressing simple expressions in a syntax that's easy to learn. It has the flavor of a cross between JavaScript and the better parts of Perl. EL expressions, combined with JSTL tags from the four standard tag libraries, provide a large, flexible feature set.
All EL expressions are enclosed by ${ } . Expressions in JSTL are always part of attribute values in JSTL tags. The expressions may be the only part of the attribute or may be combined and embedded in string literals. JSTL attributes may also contain simple string literals. In the following JSTL, we show each of these cases in a c:out action, which is an action from the core library that spews the contents of its value attribute to the JSP's output:
The EL also defines a set of rules for coercing values in an expression to a type that corresponds to the context in which the values are used. We won't cover these rules in detail here; however, the approach is very similar to that defined in Perl. (As with most things Perl, this approach works reasonably well, but, on occasion, gives results the language happily accepts, but may not be quite what you expect.)
The EL provides support for accessing object properties and collection elements, a set of implicit objects, and using relational, logical, and arithmetic operators. For indexed properties, including arrays and java.util.List classes, elements can be accessed with syntax like the following:
${alist[4]}
${aList[someVariable]}
Both JavaBean properties and java.util.Map elements (which represent a set of name/value pairs) can be accessed using one of the following ways. In the first two expressions below, we can access a property named aProperty in a JavaBean or a Map entry with the key aProperty . In the third expression (note I've left out the quotes), we access an element in anObject with a name held in the variable aVariableContainingPropertyName :
${anObject.aProperty}
${anObject["aPropertyName"]}
${anObject[aVariableContainingPropertyName]}
There are a number of implicit varibles defined in the EL:
pageContext : the pageContext object for that Webpage
pageScope , requestScope , sessionScope , and applicationScope : these are Map collections that map variable names in each of these scopes to values
param and paramValues : parameters passed with the page request; same as in JSP
header and headerValues : headers passed with the page request; same as in JSP
cookie : Map that maps cookie names to a particular cookie object
The EL defines a full set of operators that corresponds closely to those you're familiar with in Java. Arithmetic operators include + , - , * , / (or div ), and % (or mod ). Relational operators include == , != , < , > , <= , >= , which correspond to eq , ne , lt , gt , le , and ge , respectively. I won't elaborate on these operators because they are all self-explanatory.
JSTL tag libraries
Now that I've covered some basics and looked at EL syntax, I can discuss the four JSTL tag libraries specifically. I discuss the core library most since it's the one you'll certainly use; but I'll also cover the rest in enough detail to get you started.
First, though, I should talk more about the JSTL tag libraries' two flavors. I mentioned above that each JSTL tag library comes in two versions: one that supports expressions in the EL and one that supports standard JSP expressions. When you import any tag library into a JSP page, you define a prefix that designates a namespace corresponding to the tags in that library.
The four standard tag libraries, with their JSTL spec-defined prefix conventions, are listed below. Note that you could define your own prefixes, but there is absolutely no good reason for this.
Four standard tag libraries
Library
EL library prefix
Request-time (RT) library prefix
Core
c
c_rt
Internationalization/format
fmt
fmt_rt
SQL/DB support
sql
sql_rt
XML
x
x_rt
To use the EL core tag library in your page (you're really just giving your page visibility into the namespace defined in the library), include the following example directive at the top of your page:
<%@ taglib prefix="c" uri=http://java.sun.com/jstl/core %>
To use the tags in that core library, prefix each tag in your page with the prefix you've designated in your include statement:
The core tag library
Let's examine the core tag library in more detail. We look at the most commonly used functionality first.
Displaying/setting values and exception handling
The core library's most basic tag is the c:out tag, which displays an EL expression's value in a page. An example expression that uses c:out might look like this:
We have
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: JSP Standard Tag Library eases Webpage
development
View Tutorial: JSP Standard Tag Library eases Webpage
development
Related
Tutorials:
Solid JRun serves
up Java on a budget - JavaWorld June 2000
Solid JRun serves
up Java on a budget - JavaWorld June 2000 |
Encapsulate reusable functionality in JSP
This tutorial shows you how you can encapsulate the resuable functionality in JSP pages. |
An open alternative to JSP - The faults of JSP So what's wrong with JSP?
How the template-based, open source API FreeMarker trumps JSP |
Combine the power
of XPath and JSP
tag libraries -
JavaWorld January
2001
Combine the power
of XPath and JSP
tag libraries -
JavaWorld January
2001 |
Add the power of asynchronous processing to your JSPs - JavaWorld February 2001
Create custom JSP tags to use with JMS ost JavaServer Pages (JSP) developers that |
JSP best practices
Follow these tips for reusable and easily maintainable JavaServer Pages |
Use Web services
to integrate Web applications with
EISs
Use Web services
to integrate Web applications with
EISs |
Take command of your
software
Take command of your
software |
A first look at JavaServer Faces, Part I
A first look at JavaServer Faces, Part Learn how to implement Web-based user interfaces with JSF |
JSP Standard Tag Library eases Webpage
development
JSP Standard Tag Library eases Webpage
development |
Call JavaBean methods from JSP
Call JavaBean methods from JSP 2.0 pages |
JSP 2.0: The New Deal, Part 3
JSP 2.0: The New Deal, Part 3
More Flexible JSP Document Format Rules
The JSP specification supports two types of JSP pages: regular JSP pages containing any type of text or markup, and JSP Documents, which are well-formed XML documents; i.e., docum |
JAVASERVER PAGESTM JAVASERVER PAGESTM
JSPTM tag libraries define declarative, modular functionality that can be reused by any JSP page. Tag libraries reduce the necessity to embed large amounts of Java code in JSP pages by moving the functionality provided by the tags into tag implementation |
JSP Tags
JSP tags do not use <%, but just the < character. A JSP tag is somewhat like an HTML tag. JSP tags can have a "start tag", a "tag body" and an "end tag". The start and end tag both use the tag name, enclosed in < and > characters. The end starts with |
Jakarta Taglibs
This project is an open-source repository for JSP custom tag libraries and associated projects, such as TagLibraryValidator classes and extensions to page-creation tools to support tag libraries. |
This tutorial shows how to Combine the power of XPath and JSP tag libraries
In this article, we'll examine the XPath custom tag library for JSPs and see a tag collection that provides simple control constructs and a uniform attribute value substitution facility, all of which combine to reduce complexity and improve functionality. |
Strut your stuff with JSP tags
Learn how to use the custom tags from the open source Struts library and create extensions that ease the coding of properties associated with field values and user input validation. The Struts package is part of the open source Jakarta project. |
Caching Dynamic Content with JSP 2.0
Server-side caching is a powerful and popular technique for improving the performance of server-side applications. After all, why compute twice what you can compute once and hang on to? Andrei Cioroianu shows you how to exploit this technique in JSP 2.0. |
JSP FUNDAMENTALS
JSP FUNDAMENTALS
JSP FUNDAMENTALS
By: Hrishikesh Deshpande
Introduction :
JSP termed as Java Server Pages is a technology introduced by Sun Microsystems Inc. to develop the web application in more efficient way than Servlets. It has got many |
Struts HTML Tags
Struts HTML Tags
Struts HTML Tags
Struts provides HTML tag library for easy creation of user interfaces. In this lesson I will show you what all Struts HTML Tags are available to the JSP for the development of user interfaces.
To use the Struts |
|
|
|