2. Advantages
• Avoid conflicts between jar versions (XML, JDBC
etc’)
• Eases page development for page authors
• Easy to read and maintain
• Reuse of valuable components
• As a matter of principle – A JSP page should be a
view page without scriptlets.
3. Disadvantages
• Not as flexible as scriptlets
• May seem burdensome for experienced
programmers
• Not a general purpose programming language.
• Hard to debug
4. Installing
• The following must be available in your project
– standard.jar
– jstl.jar
– web.xml
– DOCTYPE – Some containers must have a DOCTYPE
element in web.xml
5. Installing
• web.xml example
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<description>Learning JSTL (or whatever you like</description>
</web-app>
6. EL Expression Language
• A data access language, that eases access to
application data.
• Allows page authors to access an object using a
simplified syntax:
– <someTags:aTag attribute="${aName}">
– <someTags.aTag attribute="${aCustomer.address.country}">
7. Accessing Application Data
• EL expressions must be enclosed between ${ and }.
• ${data} – scoped variable data.
• The dot (.) operator.
• The bracket ['name'] operator.
• E.g.
– ${customer.name}
– ${customer["name"]}
– ${customers[0]}
• ${customer.name} is equivalent to $
{customer["name"]}.
8. Operators
• The second version (in parenthesis) is to avoid the
use of entity references in XML syntax
Operator Description
+ Addition
- Subtraction
* Multiplication
/ ( or div) Division
% ( or mod ) Modulus (Reminder)
== ( or eq ) Equality
!= ( or ne ) Inequity
< ( or lt ) Less than
> ( or gt ) Greater than
<= ( or le ) Less than or equal
>= ( or ge ) Greater than or equal
&& ( or and ) Logical AND
|| ( or or ) Logical OR
! ( or not ) Boolean complement
empty Check for empty value
9. Implicit Objects
Implicit Object Content
pageScope access to the scoped variables
requestScope access to the scoped variables
sessionScope access to the scoped variables
applicationScope access to the scoped variables
param a Map object. param["foo"] returns the first string value
associated with request parameter foo.
paramValues a Map object. paramValues["foo"] returns an array of strings
associated with request parameter foo.
header a Map object. header["foo"] returns the first string value
associated with header foo.
headerValues a Map object. headerValues["foo"] returns an array of strings
associated with header foo.
initParam access to context initialization parameters
cookie exposes cookies received in the request
pageContext PageContext properties (e.g. HttpServletRequest, ServletContext,
HttpSession)
10. Tag libraries
• There are two groups of twin tag libraries
– one that supports the expression language (EL)
– one that supports request time expression values (RT)
(using scriptlets)
EL-based Tag Libraries
Functional Area URI Prefix
core http://java.sun.com/jstl/core
c
XML processing http://java.sun.com/jstl/xml
x
I18N capable
formatting
http://java.sun.com/jstl/fmt
fmt
relational db
access (SQL)
http://java.sun.com/jstl/sql
sql
RT-based Tag Libraries
Functional Area URI Prefix
core http://java.sun.com/jstl/core_rt
c_rt
XML processing http://java.sun.com/jstl/xml_rt
x_rt
I18N capable
formatting
http://java.sun.com/jstl/fmt_rt
fmt_rt
relational db
access (SQL)
http://java.sun.com/jstl/sql_rt
sql_rt
11. Tag libraries
• To use a JSTL library, you declare it:
– E.g.: <@ taglib prefix="c" uri="http://java.sun.com/jstl/core">
• prefix attribute – Namespace reference
12. The Core Tag Library
http://java.sun.com/jstl/core
13. The Core Tag Library
• Supports the following abilities
– Output
– Manipulation of scoped variables
– Conditional logic
– Loops
– URL manipulation
– Error handling.
14. General Purpose Tags
• <c:out> - writing data
• <c:set> - set the value of a scoped attribute
• <c:remove> - remove a scoped variable.
• <c:catch> - handling errors
15. General Purpose Tags
• Print with a default value:
– <c:out value="${customer.city}" default="unknown"/>
• Setting a variable
– <c:set var="name" value="expression"/>
• Setting with an inline body
– <c:set var="xmltext">
<books>
<book>
<title>Book Title A</title>
<author>A. B. C.</author>
<price>17.95</price>
</book>
</books>
</c:set>
• Setting with an inline body
– <c:remove var="cache" scope="application/>
16. Conditional Actions
• <c:if>
• If / then / else – Consists of the following
three tags:
– <c:choose>
– <c:when>
– <c:otherwise>
• Conditional actions use the attribute test to
check weather the condition has a true or
false value
17. Conditional Actions – E.g.
• <c:if> example:
– <c:if test="${customer.country == 'Canada'}">
This customer is based in Canada.
</c:if>
• <c:if> and <c:catch>
• <c:catch var="exception">
<!-- execution we can recover from. -->
...
</c:catch>
<c:if test="${exception != null}">
Processing could not be performed. Here is why....
</c:if>
18. Conditional Actions – E.g.
• <c:choose>
<c:when test="${customer.country == 'UK'}">
UK has mild winters.
</c:when>
<c:otherwise>
Country is unknown.
</c:otherwise>
</c:choose>
21. URL Actions
• <c:import> - import local and remote resources.
• As opposed to the standard jsp include action and
directive, the jstl import allows importing of remote
resources.
– <c:import url="./copyright.html"/>
– <c:import url="http://www.somewhere.com/hello.xml"/>
22. URL Actions
• <c:url> - URL rewriting tasks.
• It can be combined with <c:param>, which encodes
query-string parameters.
• <c:url value="http://www.somewhere.com/customers/register" var="registrationURL">
<c:param name="name" value="${param.name}"/>
<c:param name="country" value="${param.country}"/>
</c:url>
<a href='<c:out value="${registrationURL}"/>'>Customer Registration>/a>
24. The XML Tag Library
http://java.sun.com/jstl/xml
25. The XML Tag Library
• XML actions to address basic needs of page
authors.
• Support XPath expressions - Path
expressions to identify nodes in XML.
26. XML Core actions
• <x:out> - writing XML data
• <x:set> - set the value of an XML attribute
• <x:parse> - parse the document
27. XML Control Flow
• <x:if>
• If .. Then .. else
– <x:choose>
– <x:when>
– <x:otherwise>
• <x:forEach>
• Xml control flow tags use the select
attribute to match to an XPath
expressions.
28. XML control flow
• <x:parse xml="${xmldoc}" var="output"/>
----------------------------------------------------
• <x:if select="$output/portfolio/stock[symbol = 'SUNW']">
You still have Microsystems Stocks!
</x:if>
----------------------------------------------------
• <x:forEach select="$output/portfolio/stock">
<x:out select="price"/>
</x:forEach>
29. XML control flow
<x:choose>
<x:when select="$output/portfolio/stock[price > '70']">
You still have stocks worth over $70.
</x:when>
<x:otherwise>
You have no stocks worth over $70.
</x:otherwise>
</x:choose>
30. Code Sample 1
<c:set var="xmltext">
<books>
<book>
<title>Book Title A</title>
<author>A. B. C.</author>
<price>17.95</price>
</book>
<book>
<title>Book Title B</title>
<author>X. Y. Z.</author>
<price>24.99</price>
</book>
</books>
</c:set>
<x:parse xml="${xmltext}" var="output"/>
The title of the first book is:
<x:out select="$output/books/book[1]/title"/>
The price of the second book:
<x:out select="$output/books/book[2]/price"/>
34. Transforming XML
• Supports XSLT to transform XML within
JSP pages
• The result of the transformation is written
to the page.
35. Code Sample 5: xml-ex3.jsp
<h3>Transforming stocks.xml into HTML using stocks.xsl</h3>
<c:import url="http://www.javacourses.com/stocks.xml" var="xmldocument"/>
<c:import url="./stocks.xsl" var="xslt"/>
<x:transform xml="${xmldocument}" xslt="${xslt}"/>
36. The SQL Tag Library
http://java.sun.com/jstl/sql
37. The SQL Tag Library
• For special and unique situations
• E.g.:
– prototyping/testing
– small scale/simple applications
– lack of developer resources
• Provide basic capabilities to interact with
databases.
38. The SQL Tag Library
• Perform database queries (select)
• Easily access query results
• Perform database updates (insert, update,
delete)
• Run transactions
39. Data Source
• SQL actions operate on a data source
• Is specified by:
– dataSource attribute in SQL actions
– configuration setting:
javax.servlet.jsp.jstl.sql.dataSou
rce.
– data source action:
• JNDI relative path.
• JDBC Parameters
41. Querying a Database
<sql:query var="customers" dataSource="${dataSource}">
SELECT * FROM customers
WHERE country = ’China’
ORDER BY lastname
</sql:query>
----------------------------------------------------------
<c:forEach var="row" items="${customers.rows}">
<c:out value="${row.lastName}"/>
<c:out value="${row.firstName}"/>
<c:out value="${row.address}"/>
</c:forEach>
47. Database Access
• Implemented actions:
– <sql:query>
– <sql:update>
– <sql:transaction>
• DataSource attribute of the SQL action.
• DataSource name at the
“javax.servlet.jsp.jstl.sql.dataSource”
configuration setting:
– DataSource object
– String
• Retrieve the data source from the JNDI relative path
• JDBC parameters
51. Developing and Using Functions
• Define expression functions.
• Must be programmed as
– public static method
– public class.
• Map signature in a (TLD).
– Write the Java code
– Map function's signature in the tag library
– Write the JSP page that uses the function
52. Developing and Using Functions
package jsp2.examples.el;
import java.util.*;
public class Compute {
public static int add(String x, String y) {
int a = 0;
int b = 0;
try {
a = Integer.parseInt(x);
b = Integer.parseInt(y);
}
catch(Exception e) {
}
return a + b;
}
}
53. Developing and Using Functions
<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>
54. Developing and Using Functions
<%@ taglib prefix="my" uri="http://jakarta.apache.org/tomcat/jsp2-example-
taglib %>
<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>
58. Implementing SimpleTag
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!");
}
}
64. Tag File
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<HTML>
<HEAD>
<TITLE>JSP 2.0 Examples - Hello World Using a Tag
File</TITLE>
</HEAD>
<BODY>
<H2>Tag File Example</H2>
<P>
<B>The output of my first tag file is</B>:
<tags:greetings/>
</BODY>
</HTML>
70. I18N Key Concepts
• Three internationalization key concepts:
– Locale
• Language code (e.g. “ca” for Catalan, “zh” for Chinese)
• Optional country code (e.g.“IT” for Italy, “CR” for Costa Rica).
– Resource bundle - contains locale-specific objects. A
specific resource bundle is uniquely identified by
combining its basename with a locale.
– Basename – Identifies resource bundle group
71. I18N Key Concepts
• E.g. basename Registration
• two resource bundles:
– Registration_fr (French language)
– Registration_en (English language).
74. I18n Localization Context
• Localization context contains two pieces of information
– Resource bundle
– Locale for which the resource bundle was found.
• i18n localization is determined in one of several ways:
– <fmt:message> bundle attribute
– Enclosing <fmt:bundle> action and basename attribute
– I18n default localization context
• javax.servlet.jsp.jstl.fmt.localizationContext
configuration setting.
– type LocalizationContext
– type String.
75. I18n Localization Context
<%-- Use configuration setting --%>
----------------------------------------------------------------------
---
<fmt:message key="Welcome" />
----------------------------------------------------------------------
---
<fmt:setBundle basename="Errors" var="errorBundle" />
----------------------------------------------------------------------
---
<fmt:bundle basename="Greetings">
<%-- Localization context established by parent <fmt:bundle> tag
--%>
<fmt:message key="Welcome" />
<%-- Localization context established by attribute bundle --%>
<fmt:message key="WrongPassword" bundle="${errorBundle}" />
</fmt:bundle>
78. Resource Bundle Lookup
• Searched in the following order:
– basename + "_" + language + "_" + country +
"_" + variant
– basename + "_" + language + "_" + country
– basename + "_" + language
79. Resource Bundle Determination
Algorithm
• Step 1: Find a match within the ordered
set of preferred locales
• Step 2: Find a match with the fallback
locale:
javax.servlet.jsp.jstl.fmt.fall
backLocale
• load the root resource bundle with the
given basename
80. Response Encoding
• Set the locale
– <fmt:setLocale>
– <fmt:bundle>
– <fmt:message>
– formatting actions
• Set the encoding
– <fmt:requestEncoding>
81. Commands Summary
• <fmt:setLocale> - stores the locale.
Disables Browser-based locale setting
capabilities.
• <fmt:bundle> - Creates i18n context to
be used by its body content
• <fmt:setBundle> - creates an i18n
context
95. Conclusion
• Simpler consistent programming
environment.
• Advanced programmers may prefer
scriptlets.
• JavaBeans are used in conjunction with
JSTL.
• Easier to develop and maintain.
96. For More Information
• Fast Track JSP 1.2
• JavaServer Pages Technology
• JavaServer Pages Specification (JSR 152)
• The Tomcat 5 Servlet/JSP Container
• JSP Developers Forum
• JavaServer Pages Standard Tag Library (JSTL)
• Faster Development with JavaServer Pages
Standard Tag Library (JSTL 1.0)
Hinweis der Redaktion
Avoid conflicts between jar versions (XML, JDBC etc’)
Eases Web page Development
Open Source Extensions
Easy to read and maintain
Reuse Valuable Components and Reusable standard tags
Eases packaging of the application - Searches for the tld’s in several places (tld, META-INF)
Eases Database Access
As a matter of practice, a JSP page should be a view page, it should not have Java code embedded in scriptlets. This aids in separation of concerns. A JSP page can use JSTL elements for common tasks like conditional processing, database access, internationalization and more.
Database access can be controlled using an data-sources.xml file
The idea is that once the libraries are installed correctly, the page is dead-simple to put together.
It is important to have a DOCTYPE element to a
JSTL is not a general purpose programming language.
Is simply a data access language, which makes it possible to easily access application data.
JSTL allows page authors to access an object using a simplified syntax.
&lt;someTags:aTag attribute=&quot;${aName}&quot;&gt;
&lt;someTags.aTag attribute=&quot;${aCustomer.address.country}&quot;&gt;
As a property of an object, using the dot (.) operator
As named array element using the bracket [&apos;name&apos;] operator.
E.g. ${data} – scoped variable data.
The dot (.) operator is used to retrieve a named property.
${customer.name}
The bracket operator ([]) can be used to retrieve a named property.
${customer[&quot;name&quot;]}.
${customers[0]}
${customer.name} is equivalent to ${customer[&quot;name&quot;]}.
All EL expressions must be enclosed between ${ and }.
EL evaluates an identifier by using PageContext.findAttribute(String).
Supports output, manipulation of scoped variables, conditional logic, loops, URL manipulation, and error handling.
&lt;c:out&gt; - writing data
&lt;c:set&gt; - set the value of a scoped attribute
&lt;c:remove&gt; - remove a scoped variable.
&lt;c:catch&gt; - handling errors. See the next setion for an example
jsp include action and directive does not allow include of remote resources
XML is becoming increasingly important to page authors, and the JSTL provides XML actions that address the basic needs of those developers. The XML actions can be divided into core, control flow, and transformation actions. The XML core actions are similar to those provided in the core actions discussed above, and include &lt;x:out&gt;, &lt;x:set&gt;, and &lt;x:parse&gt;. The main difference between the core actions discussed above and the XML core actions is that XML actions support XPath expressions, while the core actions do not. As a matter of fact, the XML actions are based on XPath expressions. If you&apos;re unfamiliar with XPath, it&apos;s a language for defining parts of an XML document; XPath uses path expressions to identify nodes in an XML document.
XML Core - similar to the basic core actions
&lt;x:out&gt;
&lt;x:set&gt;
and &lt;x:parse&gt;
XML Control Flow
You can implement an if/then/else clause using the &lt;x:choose&gt; action, which selects one among a number of possible alternatives. It consists of a sequence of &lt;x:when&gt; elements, followed by an optional &lt;x:otherwise&gt;. Each &lt;x:when&gt; element has a single attribute, select, that specifies an XPath expression. When a &lt;x:choose&gt; element is processed, each of the &lt;x:when&gt; elements has its expressions evaluated in turn. Only the body of the first &lt;x:when&gt; element (whose result is true) is rendered. If none of the test conditions of nested tags evaluates to true, the body of the &lt;x:otherwise&gt; tag is evaluated, if present.
Here is an example:
&lt;x:parse xml=&quot;${xmldoc}&quot; var=&quot;output&quot;/&gt;
&lt;x:choose&gt;
&lt;x:when select=&quot;$output/portfolio/stock[price &gt; &apos;70&apos;]&quot;&gt;
You still have stocks worth over $70.
&lt;/x:when&gt;
&lt;x:otherwise&gt;
You have no stocks worth over $70.
&lt;/x:otherwise&gt;
&lt;/x:choose&gt;
-----------------------------------------------------------
The &lt;x:forEach&gt; action evaluates the given XPath expression, and iterates over the result.
E.g. Print stock information from an XML document.
&lt;x:parse xml=&quot;${xmldoc}&quot; var=&quot;output&quot;/&gt;
&lt;x:forEach select=&quot;$output/portfolio/stock&quot; var=&quot;item&quot;&gt;
&lt;x:out select=&quot;symbol&quot;/&gt;
&lt;x:out select=&quot;name&quot;/&gt;
&lt;x:out select=&quot;price&quot;/&gt;
&lt;/x:forEach&gt;
parses an XML document, then selects (using an XPath expression) the stock with the symbol SUNW &lt;x:parse xml=&quot;${xmldoc}&quot; var=&quot;output&quot;/&gt;
&lt;x:if select=&quot;$output/portfolio/stock[symbol = &apos;SUNW&apos;]&quot;&gt;
You still have Microsystems Stocks!
&lt;/x:if&gt;
--------------------------------------------------------------------
&lt;x:forEach select=&quot;$output/portfolio/stock&quot;&gt;
&lt;x:out select=&quot;price&quot;/&gt;
&lt;/x:forEach&gt;
--------------------------------------------------------------------
Many web applications use XSLT stylesheets to transform XML within JSP pages, and the JSTL supports this. The XML document to be transformed, as well as the XSLT stylesheet to be used, can be retrieved from a relative, absolute, or remote URL. The result of the transformation is written to the page. As an example, the stocks.xml document shown in Code Sample 2 (which is located at a remote web server) is transformed using the XSLT stylesheet (stocks.xsl) shown in Code Sample 4 but fetched from a local web server:
Code Sample 4: stocks.xsl
&lt;?xml version=&quot;1.0&quot;?&gt; &lt;xsl:stylesheet xmlns:xsl= &quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;&gt; &lt;xsl:output method=&quot;html&quot; indent=&quot;yes&quot;/&gt; &lt;xsl:template match=&quot;/&quot;&gt; &lt;html&gt; &lt;body&gt; &lt;xsl:apply-templates/&gt; &lt;/body&gt; &lt;/html&gt; &lt;/xsl:template&gt; &lt;xsl:template match=&quot;portfolio&quot;&gt; &lt;table border=&quot;2&quot; width=&quot;50%&quot;&gt; &lt;xsl:for-each select=&quot;stock&quot;&gt; &lt;tr&gt; &lt;td&gt; &lt;i&gt;&lt;xsl:value-of select=&quot;symbol&quot;/&gt;&lt;/i&gt; &lt;/td&gt; &lt;td&gt; &lt;xsl:value-of select=&quot;name&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;xsl:value-of select=&quot;price&quot;/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/xsl:for-each&gt; &lt;/table&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; The following example, xml-ex3.jsp (shown in Code Sample 5) demonstrates how to transform an XML document located on a remote web server using the stocks.xsl stylesheet shown in Code Sample 4.
Code Sample 5: xml-ex3.jsp
&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jstl/core&quot; %&gt; &lt;%@ taglib prefix=&quot;x&quot; uri=&quot;http://java.sun.com/jstl/xml&quot; %&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;JSTL: Importing XML Document&lt;/title&gt; &lt;/head&gt; &lt;body bgcolor=&quot;#FFFFCC&quot;&gt; &lt;h3&gt;Transforming stocks.xml into HTML using stocks.xsl&lt;/h3&gt; &lt;c:import url=&quot;http://www.javacourses.com/stocks.xml&quot; var=&quot;xmldocument&quot;/&gt; &lt;c:import url=&quot;./stocks.xsl&quot; var=&quot;xslt&quot;/&gt; &lt;x:transform xml=&quot;${xmldocument}&quot; xslt=&quot;${xslt}&quot;/&gt; &lt;/body&gt; &lt;/html&gt; When you load xml-ex3.jsp, you should see something in the browser similar to Figure 3.
Figure 3: Transforming an XML document using an XSLT stylesheet
Many web applications need to access relational databases as the source of dynamic
data for their presentation layer. While it is generally preferred to have database
operations handled within the business logic of a web application designed with an
MVC architecture, there are situations where page authors require this capability
within their JSP pages (e.g. prototyping/testing, small scale/simple applications,
lack of developer resources).
The JSTL SQL actions provide the basic capabilities to easily interact with relational
databases.
The JSTL SQL actions allow page authors to:
■ Perform database queries (select)
■ Easily access query results
■ Perform database updates (insert, update, delete)
■ Group several database operations into a transaction
SQL actions operate on a data source, as defined by the Java class
javax.sql.DataSource. A DataSource object provides connections to the
physical data source it represents. Within the context of a Connection retrieved
from the DataSource, SQL statements are executed and results are returned.
A data source can be specified explicitly via the dataSource attribute in SQL
actions, or it can be totally transparent to a page author by taking advantage of the
data source configuration setting (javax.servlet.jsp.jstl.sql.dataSource).
There are two ways a data source can be specified as a string.
The first way is through a JNDI relative path, assuming a container supporting
JNDI. For example, with the absolute JNDI resource path java:comp/env/jdbc/
myDatabase, the JNDI relative path to the data source resource would simply be
jdbc/myDatabase, given that java:comp/env is the standard JNDI root for a
J2EE application.
The second way is by specifying the parameters needed by the JDBC
DriverManager class, using the following syntax (see Section 10.6 for details on the
JDBC parameters)
url[,[driver][,[user][,password]]]
For example,
jdbc:mysql://localhost/,org.gjt.mm.mysql.Driver
where the database has been setup for access without any username or password. If
the ‘,’ character occurs in any of the JDBC parameters, it can be escaped by ‘’. The
character ‘’ itself can be escaped in the same way.
While the JDBC DriverManager class provides a low cost way to use SQL actions,
it is not recommended to use it other than for prototyping purposes because it does
not provide connection management features one can expect from a properly
designed DataSource object.
The most common use of the database actions is to query a database and display the
results of the query.
The following sample code selects all customers from China from the customers
table in the database, orders them by last name, and finally displays their last name,
first name, and address in an HTML table.
&lt;sql:query var=&quot;customers&quot; dataSource=&quot;${dataSource}&quot;&gt;
SELECT * FROM customers
WHERE country = ’China’
ORDER BY lastname
&lt;/sql:query&gt;
&lt;table&gt;
&lt;c:forEach var=&quot;row&quot; items=&quot;${customers.rows}&quot;&gt;
&lt;tr&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.lastName}&quot;/&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.firstName}&quot;/&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.address}&quot;/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
This next example shows a generic way to display the results of a query with
column names as headers:
&lt;table&gt;
&lt;!-- column headers --&gt;
&lt;tr&gt;
&lt;c:forEach var=”columnName” items=”${result.columnNames}”&gt;
&lt;th&gt;&lt;c:out value=&quot;${columnName}&quot;/&gt;&lt;/th&gt;
&lt;/c:forEach&gt;
&lt;/tr&gt;
&lt;!-- column data --&gt;
&lt;c:forEach var=&quot;row&quot; items=&quot;${result.rowsByIndex}&quot;&gt;
&lt;tr&gt;
&lt;c:forEach var=&quot;column&quot; items=&quot;${row}&quot;&gt;
&lt;td&gt;&lt;c:out value=&quot;${column}&quot;/&gt;&lt;/td&gt;
&lt;/c:forEach&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
The most common use of the database actions is to query a database and display the
results of the query.
The following sample code selects all customers from China from the customers
table in the database, orders them by last name, and finally displays their last name,
first name, and address in an HTML table.
&lt;sql:query var=&quot;customers&quot; dataSource=&quot;${dataSource}&quot;&gt;
SELECT * FROM customers
WHERE country = ’China’
ORDER BY lastname
&lt;/sql:query&gt;
&lt;table&gt;
&lt;c:forEach var=&quot;row&quot; items=&quot;${customers.rows}&quot;&gt;
&lt;tr&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.lastName}&quot;/&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.firstName}&quot;/&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.address}&quot;/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
This next example shows a generic way to display the results of a query with
column names as headers:
&lt;table&gt;
&lt;!-- column headers --&gt;
&lt;tr&gt;
&lt;c:forEach var=”columnName” items=”${result.columnNames}”&gt;
&lt;th&gt;&lt;c:out value=&quot;${columnName}&quot;/&gt;&lt;/th&gt;
&lt;/c:forEach&gt;
&lt;/tr&gt;
&lt;!-- column data --&gt;
&lt;c:forEach var=&quot;row&quot; items=&quot;${result.rowsByIndex}&quot;&gt;
&lt;tr&gt;
&lt;c:forEach var=&quot;column&quot; items=&quot;${row}&quot;&gt;
&lt;td&gt;&lt;c:out value=&quot;${column}&quot;/&gt;&lt;/td&gt;
&lt;/c:forEach&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
The most common use of the database actions is to query a database and display the
results of the query.
The following sample code selects all customers from China from the customers
table in the database, orders them by last name, and finally displays their last name,
first name, and address in an HTML table.
&lt;sql:query var=&quot;customers&quot; dataSource=&quot;${dataSource}&quot;&gt;
SELECT * FROM customers
WHERE country = ’China’
ORDER BY lastname
&lt;/sql:query&gt;
&lt;table&gt;
&lt;c:forEach var=&quot;row&quot; items=&quot;${customers.rows}&quot;&gt;
&lt;tr&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.lastName}&quot;/&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.firstName}&quot;/&gt;&lt;/td&gt;
&lt;td&gt;&lt;c:out value=&quot;${row.address}&quot;/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
This next example shows a generic way to display the results of a query with
column names as headers:
&lt;table&gt;
&lt;!-- column headers --&gt;
&lt;tr&gt;
&lt;c:forEach var=”columnName” items=”${result.columnNames}”&gt;
&lt;th&gt;&lt;c:out value=&quot;${columnName}&quot;/&gt;&lt;/th&gt;
&lt;/c:forEach&gt;
&lt;/tr&gt;
&lt;!-- column data --&gt;
&lt;c:forEach var=&quot;row&quot; items=&quot;${result.rowsByIndex}&quot;&gt;
&lt;tr&gt;
&lt;c:forEach var=&quot;column&quot; items=&quot;${row}&quot;&gt;
&lt;td&gt;&lt;c:out value=&quot;${column}&quot;/&gt;&lt;/td&gt;
&lt;/c:forEach&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
Updating a Database
The &lt;sql:update&gt; action updates a database. To ensure database integrity, several
updates to a database may be grouped into a transaction by nesting the
&lt;sql:update&gt; actions inside a &lt;sql:transaction&gt; action.
For example, the following code transfers money between two accounts in one
transaction:
&lt;sql:transaction dataSource=&quot;${dataSource}&quot;&gt;
&lt;sql:update&gt;
UPDATE account
SET Balance = Balance - ?
WHERE accountNo = ?
&lt;sql:param value=&quot;${transferAmount}&quot;/&gt;
&lt;sql:param value=&quot;${accountFrom}&quot;/&gt;
&lt;/sql:update&gt;
&lt;sql:update&gt;
UPDATE account
SET Balance = Balance + ?
WHERE accountNo = ?
&lt;sql:param value=&quot;${transferAmount}&quot;/&gt;
&lt;sql:param value=&quot;${accountTo}&quot;/&gt;
&lt;/sql:update&gt;
&lt;/sql:transaction&gt;
Updating a Database
The &lt;sql:update&gt; action updates a database. To ensure database integrity, several
updates to a database may be grouped into a transaction by nesting the
&lt;sql:update&gt; actions inside a &lt;sql:transaction&gt; action.
For example, the following code transfers money between two accounts in one
transaction:
&lt;sql:transaction dataSource=&quot;${dataSource}&quot;&gt;
&lt;sql:update&gt;
UPDATE account
SET Balance = Balance - ?
WHERE accountNo = ?
&lt;sql:param value=&quot;${transferAmount}&quot;/&gt;
&lt;sql:param value=&quot;${accountFrom}&quot;/&gt;
&lt;/sql:update&gt;
&lt;sql:update&gt;
UPDATE account
SET Balance = Balance + ?
WHERE accountNo = ?
&lt;sql:param value=&quot;${transferAmount}&quot;/&gt;
&lt;sql:param value=&quot;${accountTo}&quot;/&gt;
&lt;/sql:update&gt;
&lt;/sql:transaction&gt;
SQL Statement Parameters
The JSTL database actions support substituting parameter values for parameter
markers (“?”) in SQL statements (as shown in the previous example). This form of
parametric replacement is exposed by the SQLExecutionTag interface (see
Section 10.10).
The SQLExecutionTag interface is implemented by the tag handlers for
&lt;sql:query&gt; and &lt;sql:update&gt;. It is exposed in order to support custom parameter
actions. These custom actions may retrieve their parameters from any source and
process them before substituting them for a parameter marker in the SQL statement
of the enclosing SQLExecutionTag action.
For example, a GUI front end may have a user enter a date as three separate fields
(year, month, and day), and use this information in a database query. If the database
table being accessed provides only a single column for the complete date, action
&lt;acme:dateParam&gt; could assemble the three separate input parameters into one and
pass it to the addSQLParameter() method of its enclosing SQLExecutionTag
action:
&lt;sql:update&gt;
UPDATE PersonalInfo
SET BirthDate = ?
WHERE clientId = ?
&lt;acme:dateParam year=&quot;${year}&quot; month=&quot;${month}&quot; day=&quot;${day}&quot;/&gt;
&lt;sql:param value=”${clientId}”/&gt;
&lt;/sql:update&gt;
The JSTL formatting tags may be used to parse the string representation of dates and
numbers into instances of java.util.Date and java.lang.Number, respectively,
before supplying them to an enclosing SQLExecutionTag for parametric
replacement:
&lt;sql:update sql=&quot;${sqlUpdateStmt}” dataSource=&quot;${dataSource}&quot;&gt;
&lt;fmt:parseDate var=&quot;myDate&quot; value=&quot;${someDate}”/&gt;
&lt;sql:param value=&quot;${myDate}&quot;/&gt;
&lt;/sql:update&gt;
SQL Statement Parameters
The JSTL database actions support substituting parameter values for parameter
markers (“?”) in SQL statements (as shown in the previous example). This form of
parametric replacement is exposed by the SQLExecutionTag interface (see
Section 10.10).
The SQLExecutionTag interface is implemented by the tag handlers for
&lt;sql:query&gt; and &lt;sql:update&gt;. It is exposed in order to support custom parameter
actions. These custom actions may retrieve their parameters from any source and
process them before substituting them for a parameter marker in the SQL statement
of the enclosing SQLExecutionTag action.
For example, a GUI front end may have a user enter a date as three separate fields
(year, month, and day), and use this information in a database query. If the database
table being accessed provides only a single column for the complete date, action
&lt;acme:dateParam&gt; could assemble the three separate input parameters into one and
pass it to the addSQLParameter() method of its enclosing SQLExecutionTag
action:
&lt;sql:update&gt;
UPDATE PersonalInfo
SET BirthDate = ?
WHERE clientId = ?
&lt;acme:dateParam year=&quot;${year}&quot; month=&quot;${month}&quot; day=&quot;${day}&quot;/&gt;
&lt;sql:param value=”${clientId}”/&gt;
&lt;/sql:update&gt;
The JSTL formatting tags may be used to parse the string representation of dates and
numbers into instances of java.util.Date and java.lang.Number, respectively,
before supplying them to an enclosing SQLExecutionTag for parametric
replacement:
&lt;sql:update sql=&quot;${sqlUpdateStmt}” dataSource=&quot;${dataSource}&quot;&gt;
&lt;fmt:parseDate var=&quot;myDate&quot; value=&quot;${someDate}”/&gt;
&lt;sql:param value=&quot;${myDate}&quot;/&gt;
&lt;/sql:update&gt;
Database Access
This section describes the algorithm used by the SQL actions (&lt;sql:query&gt;,
&lt;sql:update&gt;, &lt;sql:transaction&gt;) to access a database.
Try to get a reference to a data source as follows:
If the attribute dataSource is specified, use the value specified for that
attribute as the data source.
Otherwise, get the configuration setting associated with
javax.servlet.jsp.jstl.sql.dataSource using Config.find() (see
Section 2.8). Use the value found as the data source if it is not null.
If a data source is obtained from the previous step:
If it is a DataSource object, this is the data source used by the action to access
the database.
Otherwise, if it is a String:
Assume this is a JNDI relative path and retrieve the data source from the
container’s JNDI naming context by concatenating the specified relative path
to the J2EE defined root (java:comp/env/).
If the previous step fails (data source not found), assume the string specifies
JDBC parameters using the syntax described in Section 10.1.1 and do as
follows:
If driver is specified, ensure it is loaded
Access the named URL through the DriverManager class, using an
empty string for user or password if they are not specified.
If the previous step fails, throw an exception.
Otherwise, throw an exception.
An implementation need not create new objects each time a SQL action is called and
the algorithm above does not yield a DataSource object directly; i.e when a JNDI
path or parameters for the JDBC DriverManager class are used. It may reuse
objects that it previously created for identical arguments.
Developing and Using Functions
The expression language allows you to define functions that can be invoked in an expression. Functions must be programmed as a public static method in a public class. Once the function is developed, its signature is mapped in a Tag Library Descriptor (TLD).
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.
Code Sample 3: Compute.java
package jsp2.examples.el;
import java.util.*;
public class Compute {
public static int add(String x, String y) {
int a = 0;
int b = 0;
try {
a = Integer.parseInt(x);
b = Integer.parseInt(y);
}
catch(Exception e) {
}
return a + b;
}
}
Once this is successfully compiled using javac, the next step is to map the function&apos;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.
Code Sample 4: Function Descriptor
&lt;function&gt;
&lt;description&gt;add x and y&lt;/description&gt;
&lt;name&gt;add&lt;/name&gt;
&lt;function-class&gt;jsp2.examples.el.Compute &lt;/function-class&gt;
&lt;function-signature&gt;int add(java.lang.String,java.lang.String) &lt;/function-signature&gt;
&lt;/function&gt;
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 &quot;Add Numbers&quot; button is clicked, the function is called to add the numbers. The result is displayed on the same page.
Code Sample 5: math.jsp
&lt;%@ taglib prefix=&quot;my&quot; uri=&quot;http://jakarta.apache.org/tomcat/jsp2-example-taglib %&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Functions&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H3&gt;Add Numbers&lt;/H3&gt;
&lt;P&gt;
&lt;FORM action=&quot;/developer/technicalArticles/javaserverpages/JSP20/math.jsp&quot; method=&quot;GET&quot;&gt;
X = &lt;input type=&quot;text&quot; name=&quot;x&quot; value=&quot;${param[&quot;x&quot;]}&quot;&gt;
&lt;BR&gt;
Y = &lt;input type=&quot;text&quot; name=&quot;y&quot; value=&quot;${param[&quot;y&quot;]}&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Add Numbers&quot;&gt;
&lt;/FORM&gt;
&lt;P&gt;
The sum is: ${my:add(param[&quot;x&quot;],param[&quot;y&quot;])}
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy Compute.java and save it at: C:Tomcat5.0webappsjsp-examplesWEB-INFclassesjsp2examplesel
Compile Compute.java using javac
Edit the file C:Tomcat5.0webappsjsp-examplesWEB-INFjsp2jsp2-example-taglib.tld and append the snippet of code in Code Sample 4 after the last &lt;/function&gt; in the file and before &lt;/taglib&gt;
Copy math.jsp to c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the jsp file math.jsp from a web browser
If all goes well, you should see something similar to Figure 3.
Figure 3: Using Functions
Developing and Using Functions
The expression language allows you to define functions that can be invoked in an expression. Functions must be programmed as a public static method in a public class. Once the function is developed, its signature is mapped in a Tag Library Descriptor (TLD).
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.
Code Sample 3: Compute.java
package jsp2.examples.el;
import java.util.*;
public class Compute {
public static int add(String x, String y) {
int a = 0;
int b = 0;
try {
a = Integer.parseInt(x);
b = Integer.parseInt(y);
}
catch(Exception e) {
}
return a + b;
}
}
Once this is successfully compiled using javac, the next step is to map the function&apos;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.
Code Sample 4: Function Descriptor
&lt;function&gt;
&lt;description&gt;add x and y&lt;/description&gt;
&lt;name&gt;add&lt;/name&gt;
&lt;function-class&gt;jsp2.examples.el.Compute &lt;/function-class&gt;
&lt;function-signature&gt;int add(java.lang.String,java.lang.String) &lt;/function-signature&gt;
&lt;/function&gt;
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 &quot;Add Numbers&quot; button is clicked, the function is called to add the numbers. The result is displayed on the same page.
Code Sample 5: math.jsp
&lt;%@ taglib prefix=&quot;my&quot; uri=&quot;http://jakarta.apache.org/tomcat/jsp2-example-taglib %&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Functions&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H3&gt;Add Numbers&lt;/H3&gt;
&lt;P&gt;
&lt;FORM action=&quot;/developer/technicalArticles/javaserverpages/JSP20/math.jsp&quot; method=&quot;GET&quot;&gt;
X = &lt;input type=&quot;text&quot; name=&quot;x&quot; value=&quot;${param[&quot;x&quot;]}&quot;&gt;
&lt;BR&gt;
Y = &lt;input type=&quot;text&quot; name=&quot;y&quot; value=&quot;${param[&quot;y&quot;]}&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Add Numbers&quot;&gt;
&lt;/FORM&gt;
&lt;P&gt;
The sum is: ${my:add(param[&quot;x&quot;],param[&quot;y&quot;])}
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy Compute.java and save it at: C:Tomcat5.0webappsjsp-examplesWEB-INFclassesjsp2examplesel
Compile Compute.java using javac
Edit the file C:Tomcat5.0webappsjsp-examplesWEB-INFjsp2jsp2-example-taglib.tld and append the snippet of code in Code Sample 4 after the last &lt;/function&gt; in the file and before &lt;/taglib&gt;
Copy math.jsp to c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the jsp file math.jsp from a web browser
If all goes well, you should see something similar to Figure 3.
Figure 3: Using Functions
Developing and Using Functions
The expression language allows you to define functions that can be invoked in an expression. Functions must be programmed as a public static method in a public class. Once the function is developed, its signature is mapped in a Tag Library Descriptor (TLD).
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.
Code Sample 3: Compute.java
package jsp2.examples.el;
import java.util.*;
public class Compute {
public static int add(String x, String y) {
int a = 0;
int b = 0;
try {
a = Integer.parseInt(x);
b = Integer.parseInt(y);
}
catch(Exception e) {
}
return a + b;
}
}
Once this is successfully compiled using javac, the next step is to map the function&apos;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.
Code Sample 4: Function Descriptor
&lt;function&gt;
&lt;description&gt;add x and y&lt;/description&gt;
&lt;name&gt;add&lt;/name&gt;
&lt;function-class&gt;jsp2.examples.el.Compute &lt;/function-class&gt;
&lt;function-signature&gt;int add(java.lang.String,java.lang.String) &lt;/function-signature&gt;
&lt;/function&gt;
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 &quot;Add Numbers&quot; button is clicked, the function is called to add the numbers. The result is displayed on the same page.
Code Sample 5: math.jsp
&lt;%@ taglib prefix=&quot;my&quot; uri=&quot;http://jakarta.apache.org/tomcat/jsp2-example-taglib %&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Functions&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H3&gt;Add Numbers&lt;/H3&gt;
&lt;P&gt;
&lt;FORM action=&quot;/developer/technicalArticles/javaserverpages/JSP20/math.jsp&quot; method=&quot;GET&quot;&gt;
X = &lt;input type=&quot;text&quot; name=&quot;x&quot; value=&quot;${param[&quot;x&quot;]}&quot;&gt;
&lt;BR&gt;
Y = &lt;input type=&quot;text&quot; name=&quot;y&quot; value=&quot;${param[&quot;y&quot;]}&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Add Numbers&quot;&gt;
&lt;/FORM&gt;
&lt;P&gt;
The sum is: ${my:add(param[&quot;x&quot;],param[&quot;y&quot;])}
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy Compute.java and save it at: C:Tomcat5.0webappsjsp-examplesWEB-INFclassesjsp2examplesel
Compile Compute.java using javac
Edit the file C:Tomcat5.0webappsjsp-examplesWEB-INFjsp2jsp2-example-taglib.tld and append the snippet of code in Code Sample 4 after the last &lt;/function&gt; in the file and before &lt;/taglib&gt;
Copy math.jsp to c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the jsp file math.jsp from a web browser
If all goes well, you should see something similar to Figure 3.
Figure 3: Using Functions
Developing and Using Functions
The expression language allows you to define functions that can be invoked in an expression. Functions must be programmed as a public static method in a public class. Once the function is developed, its signature is mapped in a Tag Library Descriptor (TLD).
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.
Code Sample 3: Compute.java
package jsp2.examples.el;
import java.util.*;
public class Compute {
public static int add(String x, String y) {
int a = 0;
int b = 0;
try {
a = Integer.parseInt(x);
b = Integer.parseInt(y);
}
catch(Exception e) {
}
return a + b;
}
}
Once this is successfully compiled using javac, the next step is to map the function&apos;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.
Code Sample 4: Function Descriptor
&lt;function&gt;
&lt;description&gt;add x and y&lt;/description&gt;
&lt;name&gt;add&lt;/name&gt;
&lt;function-class&gt;jsp2.examples.el.Compute &lt;/function-class&gt;
&lt;function-signature&gt;int add(java.lang.String,java.lang.String) &lt;/function-signature&gt;
&lt;/function&gt;
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 &quot;Add Numbers&quot; button is clicked, the function is called to add the numbers. The result is displayed on the same page.
Code Sample 5: math.jsp
&lt;%@ taglib prefix=&quot;my&quot; uri=&quot;http://jakarta.apache.org/tomcat/jsp2-example-taglib %&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Functions&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H3&gt;Add Numbers&lt;/H3&gt;
&lt;P&gt;
&lt;FORM action=&quot;/developer/technicalArticles/javaserverpages/JSP20/math.jsp&quot; method=&quot;GET&quot;&gt;
X = &lt;input type=&quot;text&quot; name=&quot;x&quot; value=&quot;${param[&quot;x&quot;]}&quot;&gt;
&lt;BR&gt;
Y = &lt;input type=&quot;text&quot; name=&quot;y&quot; value=&quot;${param[&quot;y&quot;]}&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Add Numbers&quot;&gt;
&lt;/FORM&gt;
&lt;P&gt;
The sum is: ${my:add(param[&quot;x&quot;],param[&quot;y&quot;])}
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy Compute.java and save it at: C:Tomcat5.0webappsjsp-examplesWEB-INFclassesjsp2examplesel
Compile Compute.java using javac
Edit the file C:Tomcat5.0webappsjsp-examplesWEB-INFjsp2jsp2-example-taglib.tld and append the snippet of code in Code Sample 4 after the last &lt;/function&gt; in the file and before &lt;/taglib&gt;
Copy math.jsp to c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the jsp file math.jsp from a web browser
If all goes well, you should see something similar to Figure 3.
Figure 3: Using Functions
Tag Handlers
The APIs for classic tag handlers in JSP 1.2 is complicated by the allowance of scriptlets in the body of tags. With the expression language, however, it is now feasible to develop scriptless JSP pages. To this end, JSP 2.0 has introduced a new type of tag extension called a Simple Tag Extension that can be used in one of two ways:
Java developers: by defining a class that implements the javax.servlet.jsp.tagext.SimpleTag interface.
Page authors who do not know Java: by using tag files.
Here is an example of the first approach. Code Sample 6 shows a simple tag handler that prints This is my first tag!.
Code Sample 6: HelloTag.java
package jsp2.examples.simpletag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
/**
* SimpleTag handler that prints &quot;This is my first tag!“
*/
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
getJspContext().getOut().write(&quot;This is my first tag!&quot;);
}
}
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:
Code Sample 7: Tag Descriptor
&lt;tag&gt;
&lt;description&gt;Prints this is my first tag&lt;/description&gt;
&lt;name&gt;hello&lt;/name&gt;
&lt;tag-class&gt;jsp2.examples.simpletag.HelloTag&lt;/tag-class&gt;
&lt;body-content&gt;empty&lt;/body-content&gt;
&lt;/tag&gt;
And finally, Code Sample 8 shows a JSP page that uses the tag I have just developed.
&lt;%@ taglib prefix=&quot;mytag&quot; uri=&quot;/WEB-INF/jsp2/jsp2-example-taglib.tld&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Simple Tag Handler&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;Simple Tag Handler&lt;/H2&gt;
&lt;P&gt;
&lt;B&gt;My first tag prints&lt;/B&gt;: &lt;mytag:hello/&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy HelloTag.java and save it under C:Tomcat5.0webappsjsp-examplesWEB-INFclassesjsp2examplessimpletag
Compile the HelloTag.java using javac
Append the tag description shown in Code Sample 7 to the end of the file: C:Tomcat5.0webappsjsp-examplesWEB-INFjsp2jsp2-example-taglib.tld, before &lt;/taglib&gt;
Copy helloworld.jsp and save it at: c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request helloworld.jsp from a web browser
If all goes well you should see something similar to Figure 4.
Figure 4: Simple Tag Handler Example
Tag Handlers
The APIs for classic tag handlers in JSP 1.2 is complicated by the allowance of scriptlets in the body of tags. With the expression language, however, it is now feasible to develop scriptless JSP pages. To this end, JSP 2.0 has introduced a new type of tag extension called a Simple Tag Extension that can be used in one of two ways:
Java developers: by defining a class that implements the javax.servlet.jsp.tagext.SimpleTag interface.
Page authors who do not know Java: by using tag files.
Here is an example of the first approach. Code Sample 6 shows a simple tag handler that prints This is my first tag!.
Code Sample 6: HelloTag.java
package jsp2.examples.simpletag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
/**
* SimpleTag handler that prints &quot;This is my first tag!“
*/
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
getJspContext().getOut().write(&quot;This is my first tag!&quot;);
}
}
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:
Code Sample 7: Tag Descriptor
&lt;tag&gt;
&lt;description&gt;Prints this is my first tag&lt;/description&gt;
&lt;name&gt;hello&lt;/name&gt;
&lt;tag-class&gt;jsp2.examples.simpletag.HelloTag&lt;/tag-class&gt;
&lt;body-content&gt;empty&lt;/body-content&gt;
&lt;/tag&gt;
And finally, Code Sample 8 shows a JSP page that uses the tag I have just developed.
&lt;%@ taglib prefix=&quot;mytag&quot; uri=&quot;/WEB-INF/jsp2/jsp2-example-taglib.tld&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Simple Tag Handler&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;Simple Tag Handler&lt;/H2&gt;
&lt;P&gt;
&lt;B&gt;My first tag prints&lt;/B&gt;: &lt;mytag:hello/&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy HelloTag.java and save it under C:Tomcat5.0webappsjsp-examplesWEB-INFclassesjsp2examplessimpletag
Compile the HelloTag.java using javac
Append the tag description shown in Code Sample 7 to the end of the file: C:Tomcat5.0webappsjsp-examplesWEB-INFjsp2jsp2-example-taglib.tld, before &lt;/taglib&gt;
Copy helloworld.jsp and save it at: c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request helloworld.jsp from a web browser
If all goes well you should see something similar to Figure 4.
Figure 4: Simple Tag Handler Example
Tag Handlers
The APIs for classic tag handlers in JSP 1.2 is complicated by the allowance of scriptlets in the body of tags. With the expression language, however, it is now feasible to develop scriptless JSP pages. To this end, JSP 2.0 has introduced a new type of tag extension called a Simple Tag Extension that can be used in one of two ways:
Java developers: by defining a class that implements the javax.servlet.jsp.tagext.SimpleTag interface.
Page authors who do not know Java: by using tag files.
Here is an example of the first approach. Code Sample 6 shows a simple tag handler that prints This is my first tag!.
Code Sample 6: HelloTag.java
package jsp2.examples.simpletag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
/**
* SimpleTag handler that prints &quot;This is my first tag!“
*/
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
getJspContext().getOut().write(&quot;This is my first tag!&quot;);
}
}
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:
Code Sample 7: Tag Descriptor
&lt;tag&gt;
&lt;description&gt;Prints this is my first tag&lt;/description&gt;
&lt;name&gt;hello&lt;/name&gt;
&lt;tag-class&gt;jsp2.examples.simpletag.HelloTag&lt;/tag-class&gt;
&lt;body-content&gt;empty&lt;/body-content&gt;
&lt;/tag&gt;
And finally, Code Sample 8 shows a JSP page that uses the tag I have just developed.
&lt;%@ taglib prefix=&quot;mytag&quot; uri=&quot;/WEB-INF/jsp2/jsp2-example-taglib.tld&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Simple Tag Handler&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;Simple Tag Handler&lt;/H2&gt;
&lt;P&gt;
&lt;B&gt;My first tag prints&lt;/B&gt;: &lt;mytag:hello/&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy HelloTag.java and save it under C:Tomcat5.0webappsjsp-examplesWEB-INFclassesjsp2examplessimpletag
Compile the HelloTag.java using javac
Append the tag description shown in Code Sample 7 to the end of the file: C:Tomcat5.0webappsjsp-examplesWEB-INFjsp2jsp2-example-taglib.tld, before &lt;/taglib&gt;
Copy helloworld.jsp and save it at: c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request helloworld.jsp from a web browser
If all goes well you should see something similar to Figure 4.
Figure 4: Simple Tag Handler Example
Tag Handlers
The APIs for classic tag handlers in JSP 1.2 is complicated by the allowance of scriptlets in the body of tags. With the expression language, however, it is now feasible to develop scriptless JSP pages. To this end, JSP 2.0 has introduced a new type of tag extension called a Simple Tag Extension that can be used in one of two ways:
Java developers: by defining a class that implements the javax.servlet.jsp.tagext.SimpleTag interface.
Page authors who do not know Java: by using tag files.
Here is an example of the first approach. Code Sample 6 shows a simple tag handler that prints This is my first tag!.
Code Sample 6: HelloTag.java
package jsp2.examples.simpletag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
/**
* SimpleTag handler that prints &quot;This is my first tag!“
*/
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
getJspContext().getOut().write(&quot;This is my first tag!&quot;);
}
}
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:
Code Sample 7: Tag Descriptor
&lt;tag&gt;
&lt;description&gt;Prints this is my first tag&lt;/description&gt;
&lt;name&gt;hello&lt;/name&gt;
&lt;tag-class&gt;jsp2.examples.simpletag.HelloTag&lt;/tag-class&gt;
&lt;body-content&gt;empty&lt;/body-content&gt;
&lt;/tag&gt;
And finally, Code Sample 8 shows a JSP page that uses the tag I have just developed.
&lt;%@ taglib prefix=&quot;mytag&quot; uri=&quot;/WEB-INF/jsp2/jsp2-example-taglib.tld&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Simple Tag Handler&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;Simple Tag Handler&lt;/H2&gt;
&lt;P&gt;
&lt;B&gt;My first tag prints&lt;/B&gt;: &lt;mytag:hello/&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy HelloTag.java and save it under C:Tomcat5.0webappsjsp-examplesWEB-INFclassesjsp2examplessimpletag
Compile the HelloTag.java using javac
Append the tag description shown in Code Sample 7 to the end of the file: C:Tomcat5.0webappsjsp-examplesWEB-INFjsp2jsp2-example-taglib.tld, before &lt;/taglib&gt;
Copy helloworld.jsp and save it at: c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request helloworld.jsp from a web browser
If all goes well you should see something similar to Figure 4.
Figure 4: Simple Tag Handler Example
Tag Handlers
The APIs for classic tag handlers in JSP 1.2 is complicated by the allowance of scriptlets in the body of tags. With the expression language, however, it is now feasible to develop scriptless JSP pages. To this end, JSP 2.0 has introduced a new type of tag extension called a Simple Tag Extension that can be used in one of two ways:
Java developers: by defining a class that implements the javax.servlet.jsp.tagext.SimpleTag interface.
Page authors who do not know Java: by using tag files.
Here is an example of the first approach. Code Sample 6 shows a simple tag handler that prints This is my first tag!.
Code Sample 6: HelloTag.java
package jsp2.examples.simpletag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
/**
* SimpleTag handler that prints &quot;This is my first tag!“
*/
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
getJspContext().getOut().write(&quot;This is my first tag!&quot;);
}
}
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:
Code Sample 7: Tag Descriptor
&lt;tag&gt;
&lt;description&gt;Prints this is my first tag&lt;/description&gt;
&lt;name&gt;hello&lt;/name&gt;
&lt;tag-class&gt;jsp2.examples.simpletag.HelloTag&lt;/tag-class&gt;
&lt;body-content&gt;empty&lt;/body-content&gt;
&lt;/tag&gt;
And finally, Code Sample 8 shows a JSP page that uses the tag I have just developed.
&lt;%@ taglib prefix=&quot;mytag&quot; uri=&quot;/WEB-INF/jsp2/jsp2-example-taglib.tld&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Simple Tag Handler&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;Simple Tag Handler&lt;/H2&gt;
&lt;P&gt;
&lt;B&gt;My first tag prints&lt;/B&gt;: &lt;mytag:hello/&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy HelloTag.java and save it under C:Tomcat5.0webappsjsp-examplesWEB-INFclassesjsp2examplessimpletag
Compile the HelloTag.java using javac
Append the tag description shown in Code Sample 7 to the end of the file: C:Tomcat5.0webappsjsp-examplesWEB-INFjsp2jsp2-example-taglib.tld, before &lt;/taglib&gt;
Copy helloworld.jsp and save it at: c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request helloworld.jsp from a web browser
If all goes well you should see something similar to Figure 4.
Figure 4: Simple Tag Handler Example
Tag File
The other way of using the Simple Tag Extension is through the use of tag files. A tag file is a source file that provides a way for a page author to abstract a segment of JSP code and make it reusable through a custom action. In other words, tag files allow JSP page authors to create reusable tag libraries using JSP syntax. The required file extension for a tag file is .tag.
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
Hello there. How are you doing?
Once a tag file has been defined, you can write a JSP page that uses this custom action. As an example, consider the JSP page in Code Sample 10.
Code Sample 10: chat.jsp
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;JSP 2.0 Examples - Hello World Using a Tag File&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;Tag File Example&lt;/H2&gt;
&lt;P&gt;
&lt;B&gt;The output of my first tag file is&lt;/B&gt;: &lt;tags:greetings/&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy the tag file, greetings.tag, and save it under c:Tomcat5.0webappsjsp-examplesWEB-INF ags
Copy the JSP page, chat.jsp, and save it at c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the chat.jsp file from a Web browser
If all goes well, you should see something similar to Figure 5.
Figure 5: Simple Tag File Example
Note: What is important to notice here is that I did not need to write a TLD for the greetings tag. I just created the tag file in a special directory, and imported it using the taglib directive, and used it!
Another Tag File Example
Tag files can be used as a template. Consider the tag file, display.tag, shown in Code Sample 11. This example is adapted from the panel example that comes with Tomcat 5.0. The attribute directive is analogous to the &lt;attribute&gt; element in the TLD; it allows for the declaration of custom action attributes.
Code Sample 11: display.tag
&lt;%@ attribute name=&quot;color&quot; %&gt; &lt;%@ attribute name=&quot;bgcolor&quot; %&gt; &lt;%@ attribute name=&quot;title&quot; %&gt; &lt;TABLE border=&quot;0&quot; bgcolor=&quot;${color}&quot;&gt; &lt;TR&gt; &lt;TD&gt;&lt;B&gt;${title}&lt;/B&gt;&lt;/TD&gt; &lt;/TR&gt; &lt;TR&gt; &lt;TD bgcolor=&quot;${bgcolor}&quot;&gt; &lt;jsp:doBody/&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/TABLE&gt;
Code Sample 12 shows a simple JSP page that uses the display tag.
Code Sample 12: newsportal.jsp
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;TITLE&gt;Another Tag File Example&lt;/TITLE&gt; &lt;/HEAD&gt; &lt;BODY&gt; &lt;H2&gt;News Portal: Another Tag File Example&lt;/H2&gt; &lt;TABLE border=&quot;0&quot;&gt; &lt;TR valign=&quot;top&quot;&gt; &lt;TD&gt; &lt;tags:display color=&quot;#ff0000&quot; bgcolor=&quot;#ffc0c0&quot; title=&quot;Travel&quot;&gt; Last French Concorde Arrives in NY&lt;br/&gt; Another Travel Headline&lt;br/&gt; Yet Another Travel Headline&lt;br/&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;TD&gt; &lt;tags:display color=&quot;#00fc00&quot; bgcolor=&quot;#c0ffc0&quot; title=&quot;Technology&quot;&gt; Java for in-flight entertainment&lt;BR&gt; Another Technology Headline&lt;BR&gt; Another Technology Headline&lt;BR&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;TD&gt; &lt;tags:display color=&quot;#ffcc11&quot; bgcolor=&quot;#ffffcc&quot; title=&quot;Sports&quot;&gt; American Football&lt;BR/&gt; NBA&lt;BR/&gt; Soccer&lt;BR/&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/TABLE&gt; &lt;/BODY&gt; &lt;/HTML&gt;
To run this example:
Copy the file display.tag and save it under c:Tomcat5.0webappsjsp-examplesWEB-INF ag
Copy the file newsportal.jsp and save it under c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the newsportal.jsp from a Web browser
Now, you should see something similar to Figure 6.
Figure 6: Using Tag File as a Template
Tag File
The other way of using the Simple Tag Extension is through the use of tag files. A tag file is a source file that provides a way for a page author to abstract a segment of JSP code and make it reusable through a custom action. In other words, tag files allow JSP page authors to create reusable tag libraries using JSP syntax. The required file extension for a tag file is .tag.
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
Hello there. How are you doing?
Once a tag file has been defined, you can write a JSP page that uses this custom action. As an example, consider the JSP page in Code Sample 10.
Code Sample 10: chat.jsp
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;JSP 2.0 Examples - Hello World Using a Tag File&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;Tag File Example&lt;/H2&gt;
&lt;P&gt;
&lt;B&gt;The output of my first tag file is&lt;/B&gt;: &lt;tags:greetings/&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy the tag file, greetings.tag, and save it under c:Tomcat5.0webappsjsp-examplesWEB-INF ags
Copy the JSP page, chat.jsp, and save it at c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the chat.jsp file from a Web browser
If all goes well, you should see something similar to Figure 5.
Figure 5: Simple Tag File Example
Note: What is important to notice here is that I did not need to write a TLD for the greetings tag. I just created the tag file in a special directory, and imported it using the taglib directive, and used it!
Another Tag File Example
Tag files can be used as a template. Consider the tag file, display.tag, shown in Code Sample 11. This example is adapted from the panel example that comes with Tomcat 5.0. The attribute directive is analogous to the &lt;attribute&gt; element in the TLD; it allows for the declaration of custom action attributes.
Code Sample 11: display.tag
&lt;%@ attribute name=&quot;color&quot; %&gt; &lt;%@ attribute name=&quot;bgcolor&quot; %&gt; &lt;%@ attribute name=&quot;title&quot; %&gt; &lt;TABLE border=&quot;0&quot; bgcolor=&quot;${color}&quot;&gt; &lt;TR&gt; &lt;TD&gt;&lt;B&gt;${title}&lt;/B&gt;&lt;/TD&gt; &lt;/TR&gt; &lt;TR&gt; &lt;TD bgcolor=&quot;${bgcolor}&quot;&gt; &lt;jsp:doBody/&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/TABLE&gt;
Code Sample 12 shows a simple JSP page that uses the display tag.
Code Sample 12: newsportal.jsp
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;TITLE&gt;Another Tag File Example&lt;/TITLE&gt; &lt;/HEAD&gt; &lt;BODY&gt; &lt;H2&gt;News Portal: Another Tag File Example&lt;/H2&gt; &lt;TABLE border=&quot;0&quot;&gt; &lt;TR valign=&quot;top&quot;&gt; &lt;TD&gt; &lt;tags:display color=&quot;#ff0000&quot; bgcolor=&quot;#ffc0c0&quot; title=&quot;Travel&quot;&gt; Last French Concorde Arrives in NY&lt;br/&gt; Another Travel Headline&lt;br/&gt; Yet Another Travel Headline&lt;br/&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;TD&gt; &lt;tags:display color=&quot;#00fc00&quot; bgcolor=&quot;#c0ffc0&quot; title=&quot;Technology&quot;&gt; Java for in-flight entertainment&lt;BR&gt; Another Technology Headline&lt;BR&gt; Another Technology Headline&lt;BR&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;TD&gt; &lt;tags:display color=&quot;#ffcc11&quot; bgcolor=&quot;#ffffcc&quot; title=&quot;Sports&quot;&gt; American Football&lt;BR/&gt; NBA&lt;BR/&gt; Soccer&lt;BR/&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/TABLE&gt; &lt;/BODY&gt; &lt;/HTML&gt;
To run this example:
Copy the file display.tag and save it under c:Tomcat5.0webappsjsp-examplesWEB-INF ag
Copy the file newsportal.jsp and save it under c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the newsportal.jsp from a Web browser
Now, you should see something similar to Figure 6.
Figure 6: Using Tag File as a Template
Tag File
The other way of using the Simple Tag Extension is through the use of tag files. A tag file is a source file that provides a way for a page author to abstract a segment of JSP code and make it reusable through a custom action. In other words, tag files allow JSP page authors to create reusable tag libraries using JSP syntax. The required file extension for a tag file is .tag.
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
Hello there. How are you doing?
Once a tag file has been defined, you can write a JSP page that uses this custom action. As an example, consider the JSP page in Code Sample 10.
Code Sample 10: chat.jsp
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;JSP 2.0 Examples - Hello World Using a Tag File&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;Tag File Example&lt;/H2&gt;
&lt;P&gt;
&lt;B&gt;The output of my first tag file is&lt;/B&gt;: &lt;tags:greetings/&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy the tag file, greetings.tag, and save it under c:Tomcat5.0webappsjsp-examplesWEB-INF ags
Copy the JSP page, chat.jsp, and save it at c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the chat.jsp file from a Web browser
If all goes well, you should see something similar to Figure 5.
Figure 5: Simple Tag File Example
Note: What is important to notice here is that I did not need to write a TLD for the greetings tag. I just created the tag file in a special directory, and imported it using the taglib directive, and used it!
Another Tag File Example
Tag files can be used as a template. Consider the tag file, display.tag, shown in Code Sample 11. This example is adapted from the panel example that comes with Tomcat 5.0. The attribute directive is analogous to the &lt;attribute&gt; element in the TLD; it allows for the declaration of custom action attributes.
Code Sample 11: display.tag
&lt;%@ attribute name=&quot;color&quot; %&gt; &lt;%@ attribute name=&quot;bgcolor&quot; %&gt; &lt;%@ attribute name=&quot;title&quot; %&gt; &lt;TABLE border=&quot;0&quot; bgcolor=&quot;${color}&quot;&gt; &lt;TR&gt; &lt;TD&gt;&lt;B&gt;${title}&lt;/B&gt;&lt;/TD&gt; &lt;/TR&gt; &lt;TR&gt; &lt;TD bgcolor=&quot;${bgcolor}&quot;&gt; &lt;jsp:doBody/&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/TABLE&gt;
Code Sample 12 shows a simple JSP page that uses the display tag.
Code Sample 12: newsportal.jsp
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;TITLE&gt;Another Tag File Example&lt;/TITLE&gt; &lt;/HEAD&gt; &lt;BODY&gt; &lt;H2&gt;News Portal: Another Tag File Example&lt;/H2&gt; &lt;TABLE border=&quot;0&quot;&gt; &lt;TR valign=&quot;top&quot;&gt; &lt;TD&gt; &lt;tags:display color=&quot;#ff0000&quot; bgcolor=&quot;#ffc0c0&quot; title=&quot;Travel&quot;&gt; Last French Concorde Arrives in NY&lt;br/&gt; Another Travel Headline&lt;br/&gt; Yet Another Travel Headline&lt;br/&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;TD&gt; &lt;tags:display color=&quot;#00fc00&quot; bgcolor=&quot;#c0ffc0&quot; title=&quot;Technology&quot;&gt; Java for in-flight entertainment&lt;BR&gt; Another Technology Headline&lt;BR&gt; Another Technology Headline&lt;BR&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;TD&gt; &lt;tags:display color=&quot;#ffcc11&quot; bgcolor=&quot;#ffffcc&quot; title=&quot;Sports&quot;&gt; American Football&lt;BR/&gt; NBA&lt;BR/&gt; Soccer&lt;BR/&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/TABLE&gt; &lt;/BODY&gt; &lt;/HTML&gt;
To run this example:
Copy the file display.tag and save it under c:Tomcat5.0webappsjsp-examplesWEB-INF ag
Copy the file newsportal.jsp and save it under c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the newsportal.jsp from a Web browser
Now, you should see something similar to Figure 6.
Figure 6: Using Tag File as a Template
Another Tag File Example
Tag files can be used as a template. Consider the tag file, display.tag, shown in Code Sample 11. This example is adapted from the panel example that comes with Tomcat 5.0. The attribute directive is analogous to the &lt;attribute&gt; element in the TLD; it allows for the declaration of custom action attributes.
Code Sample 11: display.tag
&lt;%@ attribute name=&quot;color&quot; %&gt; &lt;%@ attribute name=&quot;bgcolor&quot; %&gt; &lt;%@ attribute name=&quot;title&quot; %&gt; &lt;TABLE border=&quot;0&quot; bgcolor=&quot;${color}&quot;&gt; &lt;TR&gt; &lt;TD&gt;&lt;B&gt;${title}&lt;/B&gt;&lt;/TD&gt; &lt;/TR&gt; &lt;TR&gt; &lt;TD bgcolor=&quot;${bgcolor}&quot;&gt; &lt;jsp:doBody/&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/TABLE&gt;
Code Sample 12 shows a simple JSP page that uses the display tag.
Code Sample 12: newsportal.jsp
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;TITLE&gt;Another Tag File Example&lt;/TITLE&gt; &lt;/HEAD&gt; &lt;BODY&gt; &lt;H2&gt;News Portal: Another Tag File Example&lt;/H2&gt; &lt;TABLE border=&quot;0&quot;&gt; &lt;TR valign=&quot;top&quot;&gt; &lt;TD&gt; &lt;tags:display color=&quot;#ff0000&quot; bgcolor=&quot;#ffc0c0&quot; title=&quot;Travel&quot;&gt; Last French Concorde Arrives in NY&lt;br/&gt; Another Travel Headline&lt;br/&gt; Yet Another Travel Headline&lt;br/&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;TD&gt; &lt;tags:display color=&quot;#00fc00&quot; bgcolor=&quot;#c0ffc0&quot; title=&quot;Technology&quot;&gt; Java for in-flight entertainment&lt;BR&gt; Another Technology Headline&lt;BR&gt; Another Technology Headline&lt;BR&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;TD&gt; &lt;tags:display color=&quot;#ffcc11&quot; bgcolor=&quot;#ffffcc&quot; title=&quot;Sports&quot;&gt; American Football&lt;BR/&gt; NBA&lt;BR/&gt; Soccer&lt;BR/&gt; &lt;/tags:display&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/TABLE&gt; &lt;/BODY&gt; &lt;/HTML&gt;
To run this example:
Copy the file display.tag and save it under c:Tomcat5.0webappsjsp-examplesWEB-INF ag
Copy the file newsportal.jsp and save it under c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the newsportal.jsp from a Web browser
Now, you should see something similar to Figure 6.
Figure 6: Using Tag File as a Template
Another Tag File Example
Tag files can be used as a template. Consider the tag file, display.tag, shown in Code Sample 11. This example is adapted from the panel example that comes with Tomcat 5.0. The attribute directive is analogous to the &lt;attribute&gt; element in the TLD; it allows for the declaration of custom action attributes.
Code Sample 11: display.tag
&lt;%@ attribute name=&quot;color&quot; %&gt;
&lt;%@ attribute name=&quot;bgcolor&quot; %&gt;
&lt;%@ attribute name=&quot;title&quot; %&gt;
&lt;TABLE border=&quot;0&quot; bgcolor=&quot;${color}&quot;&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;B&gt;${title}&lt;/B&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD bgcolor=&quot;${bgcolor}&quot;&gt;
&lt;jsp:doBody/&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
Code Sample 12 shows a simple JSP page that uses the display tag.
Code Sample 12: newsportal.jsp
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Another Tag File Example&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;News Portal: Another Tag File Example&lt;/H2&gt;
&lt;TABLE border=&quot;0&quot;&gt;
&lt;TR valign=&quot;top&quot;&gt;
&lt;TD&gt;
&lt;tags:display color=&quot;#ff0000&quot; bgcolor=&quot;#ffc0c0&quot; title=&quot;Travel&quot;&gt;
Last French Concorde Arrives in NY&lt;br/&gt;
Another Travel Headline&lt;br/&gt;
Yet Another Travel Headline&lt;br/&gt;
&lt;/tags:display&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;tags:display color=&quot;#00fc00&quot; bgcolor=&quot;#c0ffc0&quot; title=&quot;Technology&quot;&gt;
Java for in-flight entertainment&lt;BR&gt;
Another Technology Headline&lt;BR&gt;
Another Technology Headline&lt;BR&gt;
&lt;/tags:display&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;tags:display color=&quot;#ffcc11&quot; bgcolor=&quot;#ffffcc&quot; title=&quot;Sports&quot;&gt;
American Football&lt;BR/&gt;
NBA&lt;BR/&gt;
Soccer&lt;BR/&gt;
&lt;/tags:display&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy the file display.tag and save it under c:Tomcat5.0webappsjsp-examplesWEB-INF ag
Copy the file newsportal.jsp and save it under c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the newsportal.jsp from a Web browser
Now, you should see something similar to Figure 6.
Figure 6: Using Tag File as a Template
Another Tag File Example
Tag files can be used as a template. Consider the tag file, display.tag, shown in Code Sample 11. This example is adapted from the panel example that comes with Tomcat 5.0. The attribute directive is analogous to the &lt;attribute&gt; element in the TLD; it allows for the declaration of custom action attributes.
Code Sample 11: display.tag
&lt;%@ attribute name=&quot;color&quot; %&gt;
&lt;%@ attribute name=&quot;bgcolor&quot; %&gt;
&lt;%@ attribute name=&quot;title&quot; %&gt;
&lt;TABLE border=&quot;0&quot; bgcolor=&quot;${color}&quot;&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;B&gt;${title}&lt;/B&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD bgcolor=&quot;${bgcolor}&quot;&gt;
&lt;jsp:doBody/&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
Code Sample 12 shows a simple JSP page that uses the display tag.
Code Sample 12: newsportal.jsp
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Another Tag File Example&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H2&gt;News Portal: Another Tag File Example&lt;/H2&gt;
&lt;TABLE border=&quot;0&quot;&gt;
&lt;TR valign=&quot;top&quot;&gt;
&lt;TD&gt;
&lt;tags:display color=&quot;#ff0000&quot; bgcolor=&quot;#ffc0c0&quot; title=&quot;Travel&quot;&gt;
Last French Concorde Arrives in NY&lt;br/&gt;
Another Travel Headline&lt;br/&gt;
Yet Another Travel Headline&lt;br/&gt;
&lt;/tags:display&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;tags:display color=&quot;#00fc00&quot; bgcolor=&quot;#c0ffc0&quot; title=&quot;Technology&quot;&gt;
Java for in-flight entertainment&lt;BR&gt;
Another Technology Headline&lt;BR&gt;
Another Technology Headline&lt;BR&gt;
&lt;/tags:display&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;tags:display color=&quot;#ffcc11&quot; bgcolor=&quot;#ffffcc&quot; title=&quot;Sports&quot;&gt;
American Football&lt;BR/&gt;
NBA&lt;BR/&gt;
Soccer&lt;BR/&gt;
&lt;/tags:display&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
To run this example:
Copy the file display.tag and save it under c:Tomcat5.0webappsjsp-examplesWEB-INF ag
Copy the file newsportal.jsp and save it under c:Tomcat5.0webappsjsp-examplesjsp2-tutorial
Request the newsportal.jsp from a Web browser
Now, you should see something similar to Figure 6.
Figure 6: Using Tag File as a Template
8.1 Overview
There are three key concepts associated with internationalization: locale, resource
bundle, and basename.
A locale represents a specific geographical, political, or cultural region. A locale is
identified by a language code, along with an optional country code1.
1. Language code
The language code is the lower-case two-letter code as defined by ISO-639 (e.g.
“ca” for Catalan, “zh” for Chinese). The full list of these codes can be found at a
number of sites, such as:
http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt
2. Country code
The country code is the upper-case two-letter code as defined by ISO-3166 (e.g.
“IT” for Italy, “CR” for Costa Rica). The full list of these codes can be found at a
number of sites, such as:
http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html.
A resource bundle contains locale-specific objects. Each message in a resource bundle
is associated with a key. Since the set of messages contained in a resource bundle can
be localized for many locales, the resource bundles that translate the same set of
messages are identified by the same basename. A specific resource bundle is therefore
uniquely identified by combining its basename with a locale.
For instance, a web application could define the registration resource bundles with
basename Registration to contain the messages associated with the registration
portion of the application. Assuming that French and English are the only languages
supported by the application, there will be two resource bundles:
Registration_fr (French language) and Registration_en (English language).
Depending on the locale associated with the client request, the key “greeting” could
be mapped to the message “Bonjour” (French) or “Hello” (English).
8.1 Overview
There are three key concepts associated with internationalization: locale, resource
bundle, and basename.
A locale represents a specific geographical, political, or cultural region. A locale is
identified by a language code, along with an optional country code1.
1. Language code
The language code is the lower-case two-letter code as defined by ISO-639 (e.g.
“ca” for Catalan, “zh” for Chinese). The full list of these codes can be found at a
number of sites, such as:
http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt
2. Country code
The country code is the upper-case two-letter code as defined by ISO-3166 (e.g.
“IT” for Italy, “CR” for Costa Rica). The full list of these codes can be found at a
number of sites, such as:
http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html.
A resource bundle contains locale-specific objects. Each message in a resource bundle
is associated with a key. Since the set of messages contained in a resource bundle can
be localized for many locales, the resource bundles that translate the same set of
messages are identified by the same basename. A specific resource bundle is therefore
uniquely identified by combining its basename with a locale.
For instance, a web application could define the registration resource bundles with
basename Registration to contain the messages associated with the registration
portion of the application. Assuming that French and English are the only languages
supported by the application, there will be two resource bundles:
Registration_fr (French language) and Registration_en (English language).
Depending on the locale associated with the client request, the key “greeting” could
be mapped to the message “Bonjour” (French) or “Hello” (English).
8.1.1 &lt;fmt:message&gt;
It is possible to internationalize the JSP pages of a web application simply by using
the &lt;fmt:message&gt; action as shown below:
&lt;fmt:message key=&quot;greeting&quot;/&gt;
In this case, &lt;fmt:message&gt; leverages the default i18n localization context, making it
extremely simple for a page author to internationalize JSP pages.
&lt;fmt:message&gt; also supports compound messages, i.e. messages that contain one or
more variables. Parameter values for these variables may be supplied via one or
more &lt;fmt:param&gt; subtags (one for each parameter value). This procedure is
referred to as parametric replacement.
&lt;fmt:message key=&quot;athletesRegistered&quot;&gt;
&lt;fmt:param&gt;
&lt;fmt:formatNumber value=”${athletesCount}”/&gt;
&lt;/fmt:param&gt;
&lt;/fmt:message&gt;
Depending on the locale, this example could print the following messages:
french: Il y a 10 582 athletes enregistres.
english: There are 10,582 athletes registered.
8.1.1 &lt;fmt:message&gt;
It is possible to internationalize the JSP pages of a web application simply by using
the &lt;fmt:message&gt; action as shown below:
&lt;fmt:message key=&quot;greeting&quot;/&gt;
In this case, &lt;fmt:message&gt; leverages the default i18n localization context, making it
extremely simple for a page author to internationalize JSP pages.
&lt;fmt:message&gt; also supports compound messages, i.e. messages that contain one or
more variables. Parameter values for these variables may be supplied via one or
more &lt;fmt:param&gt; subtags (one for each parameter value). This procedure is
referred to as parametric replacement.
&lt;fmt:message key=&quot;athletesRegistered&quot;&gt;
&lt;fmt:param&gt;
&lt;fmt:formatNumber value=”${athletesCount}”/&gt;
&lt;/fmt:param&gt;
&lt;/fmt:message&gt;
Depending on the locale, this example could print the following messages:
french: Il y a 10 582 athletes enregistres.
english: There are 10,582 athletes registered.
8.2I18n Localization Context
I18n actions use an i18n localization context to localize their data. An i18n
localization context contains two pieces of information: a resource bundle and the
locale for which the resource bundle was found.
An i18n action determine its i18n localization context in one of several ways, which
are described in order of precedence:
&lt;fmt:message&gt; bundle attribute
If attribute bundle is specified in &lt;fmt:message&gt;, the i18n localization context associated with it is used for localization.
&lt;fmt:bundle&gt; action
If &lt;fmt:message&gt; actions are nested inside a &lt;fmt:bundle&gt; action, the i18n localization context of the enclosing &lt;fmt:bundle&gt; action is used for localization. The &lt;fmt:bundle&gt; action determines the resource bundle of its i18n localization context according to the resource bundle determination algorithm in Section 8.3, using the basename attribute as the resource bundle basename.
I18n default localization context
The i18n localization context whose resource bundle is to be used for localization is specified via the javax.servlet.jsp.jstl.fmt.localizationContext configuration setting (see Section 8.11.3). If the configuration setting is of type LocalizationContext, its resource bundle component is used for localization. Otherwise, the configuration setting is of type String, and the action establishes its own i18n localization context whose resource bundle component is determined according to the resource bundle determination algorithm in Section 8.3, using the configuration setting as the resource bundle basename.
The example below shows how the various localization contexts can be established
to define the resource bundle used for localization.
&lt;%-- Use configuration setting --%&gt;
&lt;fmt:message key=&quot;Welcome&quot; /&gt;
&lt;fmt:setBundle basename=&quot;Errors&quot; var=&quot;errorBundle&quot; /&gt;
&lt;fmt:bundle basename=&quot;Greetings&quot;&gt;
&lt;%-- Localization context established by
parent &lt;fmt:bundle&gt; tag --%&gt;
&lt;fmt:message key=&quot;Welcome&quot; /&gt;
&lt;%-- Localization context established by attribute bundle --%&gt;
&lt;fmt:message key=&quot;WrongPassword&quot; bundle=&quot;${errorBundle}&quot; /&gt;
&lt;/fmt:bundle&gt;
I18n actions use an i18n localization context to localize their data. An i18n
localization context contains two pieces of information: a resource bundle and the
locale for which the resource bundle was found.
An i18n action determine its i18n localization context in one of several ways, which
are described in order of precedence:
&lt;fmt:message&gt; bundle attribute
If attribute bundle is specified in &lt;fmt:message&gt;, the i18n localization context associated with it is used for localization.
&lt;fmt:bundle&gt; action
If &lt;fmt:message&gt; actions are nested inside a &lt;fmt:bundle&gt; action, the i18n localization context of the enclosing &lt;fmt:bundle&gt; action is used for localization. The &lt;fmt:bundle&gt; action determines the resource bundle of its i18n localization context according to the resource bundle determination algorithm in Section 8.3, using the basename attribute as the resource bundle basename.
I18n default localization context
The i18n localization context whose resource bundle is to be used for localization is specified via the javax.servlet.jsp.jstl.fmt.localizationContext configuration setting (see Section 8.11.3). If the configuration setting is of type LocalizationContext, its resource bundle component is used for localization. Otherwise, the configuration setting is of type String, and the action establishes its own i18n localization context whose resource bundle component is determined according to the resource bundle determination algorithm in Section 8.3, using the configuration setting as the resource bundle basename.
The example below shows how the various localization contexts can be established
to define the resource bundle used for localization.
&lt;%-- Use configuration setting --%&gt;
&lt;fmt:message key=&quot;Welcome&quot; /&gt;
&lt;fmt:setBundle basename=&quot;Errors&quot; var=&quot;errorBundle&quot; /&gt;
&lt;fmt:bundle basename=&quot;Greetings&quot;&gt;
&lt;%-- Localization context established by
parent &lt;fmt:bundle&gt; tag --%&gt;
&lt;fmt:message key=&quot;Welcome&quot; /&gt;
&lt;%-- Localization context established by attribute bundle --%&gt;
&lt;fmt:message key=&quot;WrongPassword&quot; bundle=&quot;${errorBundle}&quot; /&gt;
&lt;/fmt:bundle&gt;
8.2I18n Localization Context
I18n actions use an i18n localization context to localize their data. An i18n
localization context contains two pieces of information: a resource bundle and the
locale for which the resource bundle was found.
An i18n action determine its i18n localization context in one of several ways, which
are described in order of precedence:
&lt;fmt:message&gt; bundle attribute
If attribute bundle is specified in &lt;fmt:message&gt;, the i18n localization context associated with it is used for localization.
&lt;fmt:bundle&gt; action
If &lt;fmt:message&gt; actions are nested inside a &lt;fmt:bundle&gt; action, the i18n localization context of the enclosing &lt;fmt:bundle&gt; action is used for localization. The &lt;fmt:bundle&gt; action determines the resource bundle of its i18n localization context according to the resource bundle determination algorithm in Section 8.3, using the basename attribute as the resource bundle basename.
I18n default localization context
The i18n localization context whose resource bundle is to be used for localization is specified via the javax.servlet.jsp.jstl.fmt.localizationContext configuration setting (see Section 8.11.3). If the configuration setting is of type LocalizationContext, its resource bundle component is used for localization. Otherwise, the configuration setting is of type String, and the action establishes its own i18n localization context whose resource bundle component is determined according to the resource bundle determination algorithm in Section 8.3, using the configuration setting as the resource bundle basename.
The example below shows how the various localization contexts can be established
to define the resource bundle used for localization.
&lt;%-- Use configuration setting --%&gt;
&lt;fmt:message key=&quot;Welcome&quot; /&gt;
&lt;fmt:setBundle basename=&quot;Errors&quot; var=&quot;errorBundle&quot; /&gt;
&lt;fmt:bundle basename=&quot;Greetings&quot;&gt;
&lt;%-- Localization context established by parent &lt;fmt:bundle&gt; tag --%&gt;
&lt;fmt:message key=&quot;Welcome&quot; /&gt;
&lt;%-- Localization context established by attribute bundle --%&gt;
&lt;fmt:message key=&quot;WrongPassword&quot; bundle=&quot;${errorBundle}&quot; /&gt;
&lt;/fmt:bundle&gt;
Preferred Locales
If the resource bundle of an i18n localization context needs to be determined, it is
retrieved from the web application’s resources according to the algorithm described
in section Section 8.3. This algorithm requires two pieces of information: the
basename of the resource bundle (as described in the previous section) and the
preferred locales.
The method for setting the preferred locales is characterized as either applicationbased
or browser-based.
Application-based locale setting has priority over browser-based locale setting. In this
mode, the locale is set via the javax.servlet.jsp.jstl.fmt.locale
configuration setting (see Section 8.11.1). Setting the locale this way is useful in
situations where an application lets its users pick their preferred locale and then sets
the scoped variable accordingly. This may also be useful in the case where a client’s
preferred locale is retrieved from a database and installed for the page using the
&lt;fmt:setLocale&gt; action.
Offers simpler consistent programming environment.
Advanced programmers may prefer scriptlets over JSTL.
JSTL allows HTML programmers to program in one consistent environment.
Back-end programmers still create JavaBeans, which are used in conjunction with JSTL.
Conclusion
JSP 2.0 makes is easier than ever to rapidly develop and easily maintain dynamic Web pages. Despite the fact the word &quot;Java&quot; appears in JavaServer Pages, with JSP 2.0 page authors can develop innovative dynamic Web pages without having to learn the Java programming language. The examples shown throughout the article demonstrate how easy it is to get started using the new features in JSP 2.0 to develop dynamic Web pages.