Six Cool New JSP and Servlet Features

If you use a web container such as Tomcat 5.x, which supports Servlet API 2.4
and JSP 2.0, then you can use a number of useful new features. These include:

Using a servlet as a welcome file.

Mapping filters to RequestDispatchers.

The new ServletRequestListener and
ServletRequestAttributeListener interfaces.

Using Expression Language (EL) code within template text, not just as tag
attribute values.

Writing tag files.

Writing Expression Language qualified functions.

In this article, we'll delve into how you can use each of these features in your projects, using working code examples.
Let's take a look at the first one on the list.

1. Servlets as Welcome Files

Welcome files are automatic web server responses to requests that specify only
directories, not specific web components or files. When a visitor wants to request
a home page, for example, they normally will just type in the name of the protocol
(HTTP) and the host name, as in: http://www.google.com. Using Java web
components, you can configure the response that is automatically sent to
requests of this type by using a welcome-file element in WEB-INF/web.xml.
The element looks like this, with index.html as the welcome file, followed by its
alternative, default.jsp (if index.html does not exist in the web application, which
would be admittedly odd, considering that it is configured as a welcome file).

With Servlet 2.4, now you can have a servlet as a welcome file, which is useful
for applications that use servlets as "Front Controllers." This is a design pattern
representing the web components that grab the request and figure out the best
place to which it should be routed among several alternatives. Here's how to
configure a servlet as a welcome file.

Make sure to use the servlet name in the welcome-file element without a
forward slash (/) preceding it. With the prior configuration, the servlet is the
primary welcome file; however, default.jsp will be the welcome file, if a servlet of
that registered name does not exist in the web application.

2. Mapping Filters to RequestDispatchers

Filters are great servlet API features that can intercept specified requests and
initiate logging, security measures, data compression, form-parameter validation,
or whatever task you need them to accomplish before the filter sends the request
back on its way. Servlet 2.4 now allows filters to intervene with
javax.servlet.RequestDispatcher objects that are including the
output of a web component or forwarding a request to another web component.
This feature is once again configured in WEB-INF/web.xml.

The dispatcher elements in the example configuration specify that the
LogFilter applies to requests for the servlet path /requestheaders, as well as
to any RequestDispatchers that include the output of the servlet path
/requestheaders.

Similarly, if you want to initiate a filter when you are using a
RequestDispatcher to forward a request to another component, use the
FORWARD value with the dispatcher element:

3. Using Listeners with Servlet Requests

Servlet 2.4 provides two interfaces that you can use as application event
listeners for HTTP requests: javax.servlet.ServletRequestListener and
javax.servlet.ServletRequestAttributeListener. An
application event listener is an object that is notified when certain events occur,
so you can include objects that initiate tasks when a new request comes into
your application. For example, this code counts the number of web application
requests by incrementing a static variable inside of a
ServletRequestListener.

Each time the web application receives a new request, the listener is notified and
its requestInitialized() method is called. This method's parameter is a
javax.servlet.ServletRequestEvent type. Calling this object's
getServletRequest() method gives the developer access to the new
request, a javax.servlet.ServletRequest type (to do whatever they
want with the new request).

The listener must have a constructor with no arguments. You have to register the
ServletRequestListener in web.xml:

The web container then creates an instance of the listener when it deploys your
web application.

Note: Servlet 2.4 also includes a
ServletRequestAttributeListener interface. An
object that implements this interface can receive notifications
of when object attributes are added to or removed from a
ServletRequest. You also have to register these listener
types in web.xml.

4. Using EL Code Within Template Text

The Expression Language (EL) is a powerful tool that you can use with JSPs.
What's different with JSP 2.0 is that the JSP container is now responsible for the
EL, and you can use EL code embedded inside of template text. For example,
imagine that you had an object attribute named user stored in a session. The
user object has a getName() method that returns the user's name. With a
JSP 2.0 container, you can display the user's name in a JSP in the following
manner:

One caveat is that if your web application is still using the Servlet 2.3 format of
web.xml, then the JSP container will automatically deactivate the evaluation of EL
expressions in template text, and usages such as in the example will not work
as expected. Therefore, in most cases, you should upgrade your application to
the Servlet 2.4 format of web.xml.

5. Writing Tag Files

JSP 2.0 has added the tag file feature, which is designed to make it much easier
for developers that are not Java specialists to create a custom tag. Tag files can
be written in either JSP syntax or XML. Therefore, you can skip the stage of
writing the Java source file and compiling it.

The required extensions for a tag file are .tag if you write the file in JSP syntax,
and .tagx if the file is only composed of XML elements. The JSP 2.0
specification requires you to place the tag file in the WEB-INF/tags directory, or
a subdirectory thereof. If you want to package the tag file in a Java Archive
.jar file as part of a custom tag library, then you can store it in META-INF/tags
(or a subdirectory of META-INF/tags), and then describe the tag file in a Tag Library
Descriptor (TLD), a type of configuration file. You do not have to describe the
tag files that are placed beneath WEB-INF/tags in a TLD, but you can, if you want
to consolidate a library of traditional custom tags and tag files in one TLD.

The following tag file generates the HTML text for inserting a logo image inside
of the page.

In the example above, imgDir is a servlet context attribute representing the image
directory, so the Expression Language may access the attribute value with the
syntax ${imgDir}.

Inside of the JSPs that use the custom tag, you specify the location of the tag file
for the JSP container by using the taglib directive's tagDir attribute. In
other words, the tagDir attribute provides the path to the web application
directory where you stored the tag file. Here's an example:

<%@ taglib prefix="cbck" tagdir="/WEB-INF/tags" %>

As long as you place the tag file, which has a .tag extension (or .tagx
extension if the tag file is in XML syntax), in /WEB-INF/tags, then JSPs will be
able to use the tag associated with the example tag file. Here is an example of how
the tag would be used inside of a JSP:

6. Writing EL-Qualified Functions

You can embed your own Java functions within Expression Language (EL) code
with JSP 2.0. This allows developers to expand the capabilities of the EL's built-in
functions and objects. Java Servlet & JSP Cookbook uses the example of an EL
function inside of a JSP that calls an Oracle stored procedure (Recipe 21.8). The
JSP 2.0 specification calls them "qualified" functions because the function call is
qualified by a particular namespace, the namespace represented by your custom
tag library.

First, you write the Java source file and include a public static method. Here's an
example:

This is a pretty good way to use JSPs as the presentation component for an
application that is based on stored procedures. In general, creating your own
functions is a great way to extend the features of the EL for the benefit of your
project.