Académique Documents
Professionnel Documents
Culture Documents
Article
Developing Web Applications With JavaServer Pages 2.0
by Qusay H. Mahmoud
July 2003
JavaServer Pages (JSP) technology, which abstracts servlets to a higher level, is an open, freely-available specification developed by the Java Community Process (JCP) for
generating dynamic content, and a key component of the Java 2 Enterprise Edition (J2EE) specification. Many commercially available application servers (such as BEA
WebLogic, IBM WebSphere, Live JRun, and Orion) support JSP technology.
JSP technology is being used everywhere on the Web including airline reservation systems, banking systems, and shopping. The new release, Version 2.0, is an upgrade to
JSP 1.2 with several interesting new features. The objective of JSP 2.0 is to make the task of developing dynamic Web pages easier than ever without having to learn the
Java programming language.
This article:
• Provides a fast track code intensive tutorial to get started with JSP 2.0
• Offers a flavor of the effort involved in developing applications using JSP 2.0
• Provides sample code that you can adapt for your own applications
If you are new to JSP, it might be a good idea to start directly with JSP 2.0. However, if you wish to learn about JSP 1.2 you may want to start with this JSP tutorial.
JSP 2.0
JSP 2.0 is an upgrade to JSP 1.2 with several new interesting features that make the lives of Web application designers and developers easier. The objective of JSP 2.0 is to
make JSP easier to use than ever, and more importantly to be used without having to learn the Java programming language itself. It simplifies the Tag APIs by adding a new
extension mechanism called SimpleTag.
In addition to several other improvements, the new key features that have been introduced in JSP 2.0 are:
1. A simple expression language (EL), which can used to easily access data from JSP pages. The expression language simplifies writing scriptless JSP-based
applications without using Java scriptlets or Java expressions.
2. New syntax for defining reusable custom actions using JSP technology directly. The syntax is delivered into .tag and .tagx files which can be written by
developers and page authors.
3. The XML syntax has been improved substantially. The new standard filename extensions (.tagx for tag files and .jspx for JSP files) have been added.
In this article I concentrate on the Expression Language, the simplified Tag API, and tag files. I believe existing JSP developers will find these key features to be very
interesting and useful.
Prior to JSP 2.0, a page author had to use the expression <%= aName %> to access the value of a system, as in the following example:
An expression language allows a page author to access an object using a simplified syntax. For example, to access a simple variable, you can use something like:
<someTags:aTag attribute="${aName}">
And to access a nested JavaBeans property, you would use something like:
${aCustomer.address.country}
But, you might ask, isn't this JavaScript syntax? You are absolutely right! If you've worked with JavaScript, you will feel right at home, because the EL borrows the JavaScript
syntax for accessing structured data.
Note: The Expression Language was originally developed as part of the JavaServer Pages Standard Tag Library (JSTL) 1.0, which is a standard tag library that provides
support for common, structural tasks, such as iteration and conditionals, processing XML documents, internationalization, and database access using the Structured Query
Language (SQL). The JSTL specification is being developed by the JSR 52 expert group. For a tutorial on JSTL, please see Faster Development with JSTL.
The expression ${data} represents the scoped variable named data. You can retrieve properties from collections using either the dot (.) or bracket ([]) operator:
• The dot (.) operator is used to retrieve a named property. For example, the expression ${customer.name} indicates the name property of the customer
scoped variable.
• The bracket operator ([]) can be used to retrieve a named property, as in ${customer["name"]}. The bracket operator can also be used as $
{customers[0]} to refer to the first item in the customers collection.
The expression language unifies the treatment of dot (.) and bracket ([]) operators. Therefore, ${customer.name} is equivalent to ${customer["name"]}. As you can
see, all EL expressions must be enclosed between ${ and }.
The EL evaluates an identifier by looking up its value as an attribute using PageContext.findAttribute(String). If the attribute is not found, null is returned.
Operators
The EL supports arithmetic, relational, and logical operators to handle the most common data manipulations. In addition, a special operator for testing if an object is empty is
provided. The operators are shown in Table 1. You can use the empty operator to determine whether a collection or a string is empty or null. For example, ${empty
param.name} will be true only if the request parameter named param is not present. The empty operator can be combined with the ! operator, as in the expression ${!
empty param.name}, which evaluates to true if the request parameter named param is present.
Operator Description
+ Addition
- Subtraction
* Multiplication
/ or div Division
== or = Equality
!= or != Inequality
|| or or Logical OR
a ? b : c Conditional operator
Implicit Objects
In addition to operators, the EL defines implicit objects to support access to application data that is of interest to page authors. The implicit objects defined by the EL are
shown in Table 2. An example of how to use some of these implicit objects is provided later.
EL Example
As you can tell, web page authors can use the Expression Language without having to learn Java. Code Sample 1 shows some EL expressions as well as the use of explicit
objects.
<HTML>
<HEAD>
<TITLE>Expression Language Examples</TITLE>
</HEAD>
<BODY>
<TABLE BORDER="1">
<THEAD>
<TD><B>Expression</B></TD>
<TD><B>Value</B></TD>
</THEAD>
<TR>
<TD>\${2 + 5}</TD>
<TD>${2 + 5}</TD>
</TD>
<TR>
<TD>\${4/5}</TD>
<TD>${4/5}</TD>
</TR>
<TR>
<TD>\${5 div 6}</TD>
<TD>${5 div 6}</TD>
</TR>
<TR>
<TD>\${5 mod 7}</TD>
<TD>${5 mod 7}</TD>
</TR>
<TR>
<TD>\${2 < 3}</TD>
<TD>${2 < 3}</TD>
</TR>
<TR>
<TD>\${2 gt 3}</TD>
<TD>${2 gt 3}</TD>
</TR>
<TR>
<TD>\${3.1 le 3.2}</TD>
<TD>${3.1 le 3.2}</TD>
</TR>
<TR>
<TD>\${(5 > 3) ? 5 : 3}</TD>
<TD>${(5 > 3) ? 5 : 3}</TD>
</TR>
<TR>
<TD>\${header["host"]}</TD>
<TD>${header["host"]}</TD>
</TR>
<TR>
<TD>\${header["user-agent"]}</TD>
<TD>${header["user-agent"]}</TD>
</TR>
</TABLE>
</BODY>
</HTML>
In order to run this, do the following. Here I assume that Tomcat 5.0 is installed at c:\Tomcat5.0
2. Create a directory and name it whatever you like, let's say jsp2-tutorial
You should something similar to Figure 1. It is that simple to use the Expression Language!
Figure 1: JSP Expression Language and Implicit Objects
Note: In this article, all JSP pages will be saved under c:\Tomcat5.0\webapps\jsp-examples\jsp2-tutorial.
Fill-Out-Form Example
The implicit object, $param[var] can be used to read values from a fill-out form. Code Sample 2 shows a simple example of a fill-out form that prompts the user to enter a
name.
<HTML>
<HEAD>
<TITLE>Form Content</TITLE>
</HEAD>
<BODY>
<H3>Fill-out-form</H3>
<P>
<FORM action="/developer/technicalArticles/javaserverpages/JSP20/form.jsp" method="GET">
Name = <input type="text" name="name" value="${param['name']}">
<input type="submit" value="Submit Name">
</FORM>
<P>
The Name is: ${param.name}
</BODY>
</HTML>
In this example, when the user enters a name and clicks the "Submit Name" button, the name entered will be displayed on the same page next to "The Name is: " as shown
in Figure 2.
Again, in order to run this example, simply copy form.jsp to c:\Tomcat5.0\webapps\jsp-examples\jsp2-tutorial and request that page from a Web browser.
To illustrate the use of functions, I use a simple example to add two numbers. First we write the Java code for the method to add two numbers. Code Sample 3 shows a
static method that accepts two Strings, parses them to integers, and returns their sum.
package jsp2.examples.el;
import java.util.*;
Once this is successfully compiled using javac, the next step is to map the function's signature in the tag library. Code Sample 4 shows how to map the add function to the
class containing the implementation of the function and the signature of the function. I will tell you where to add this later.
<function>
<description>add x and y</description>
<name>add</name>
<function-class>jsp2.examples.el.Compute
</function-class>
<function-signature>int
add(java.lang.String,java.lang.String)
</function-signature>
</function>
Now, we can write the JSP page that uses the function. Code Sample 5 shows a form with two fields. The user enters two numbers and when the "Add Numbers" button is
clicked, the function is called to add the numbers. The result is displayed on the same page.
<HEAD>
<TITLE>Functions</TITLE>
</HEAD>
<BODY>
<H3>Add Numbers</H3>
<P>
<FORM action="/developer/technicalArticles/javaserverpages/JSP20/math.jsp" method="GET">
X = <input type="text" name="x" value="${param["x"]}">
<BR>
Y = <input type="text" name="y" value="${param["y"]}">
<input type="submit" value="Add Numbers">
</FORM>
<P>
The sum is: ${my:add(param["x"],param["y"])}
</BODY>
</HTML>
3. Edit the file C:\Tomcat5.0\webapps\jsp-examples\WEB-INF\jsp2\jsp2-example-taglib.tld and append the snippet of code in Code Sample 4
after the last </function> in the file and before </taglib>
Here is an example of the first approach. Code Sample 6 shows a simple tag handler that prints This is my first tag!.
package jsp2.examples.simpletag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
/**
* SimpleTag handler that prints "This is my first tag!"
*/
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
getJspContext().getOut().write("This is my first tag!");
}
}
Once this is successfully compiled, the next step is to define a tag descriptor in a TLD. Code Sample 7 shows a sample tag descriptor:
<tag>
<description>Prints this is my first tag</description>
<name>hello</name>
<tag-class>jsp2.examples.simpletag.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
And finally, Code Sample 8 shows a JSP page that uses the tag I have just developed.
<BODY>
<H2>Simple Tag Handler</H2>
<P>
<B>My first tag prints</B>: <mytag:hello/>
</BODY>
</HTML>
3. Append the tag description shown in Code Sample 7 to the end of the file: C:\Tomcat5.0\webapps\jsp-examples\WEB-INF\jsp2\jsp2-example-
taglib.tld, before </taglib>
To demonstrate how easy this is, consider the tag file in Code Sample 9. That is right, this is a tag file!
Code Sample 9: greetings.tag
Code Sample 12 shows a simple JSP page that uses the display tag.
• Faster Development with JavaServer Pages Standard Tag Library (JSTL 1.0)
Acknowledgments
Special thanks to Gregory Murray and Mark Roth of Sun Microsystems, whose feedback helped me improve this article.