ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

JSP 1.2: Great news for the JSP Community, Part 1

by Hans Bergsten

This article is the first installment of a two-part article. Part One, presented here, covers the new features in JSP 2.1; Part Two offers details about the changes and additions in the custom tag handler API.

Ever since Sun released the JavaServer Pages (JSP) 1.1 specification in December 1999, thousands of developers have flocked to this powerful technology for dynamic Web content development. JSP-INTEREST is now Sun's second most popular mailing list, with more than 4,000 subscribers; new Web sites dedicated to JSP pop up every day; and some 30 JSP books have been released so far. JSP 1.1 is supported by most, if not all, Web servers and application servers, and developers can take advantage of numerous JSP tag libraries and frameworks -- of both the open source and the commercial varieties.

(For more on JSP, Servlets, and Tomcat, see ONJava's JSP and Servlets topic page.)

While Web application developers all over the world have been busy developing new applications based on JSP 1.1, the JSP 1.2 specification has been taking shape within the Java Community Process group called JSR-053. Members of this group include authors, application server developers from big and small commercial vendors as well as the open source community, and servlet and JSP developers.

Released September 17th, the JSP 1.2 specification is now ready for public consumption. It contains everything from brand new features to corrections and clarifications of areas that were not quite right in the previous version. The most important changes:

  • JSP 1.2 is based on Servlet 2.3 and Java 2.
  • The include action can now be used without flushing.
  • The XML syntax for a JSP page has been finalized.
  • Tag libraries can make use of Servlet 2.3 event listeners.
  • A new type of page validator has been added.
  • New options for tag library distribution and deployment have been added.
  • Two new tag interfaces have been added.
  • The conversion rules for String literal tag attribute values now include conversion to the Object type.
  • A PropertyEditor can be used for custom attribute value conversion.
  • The tag handler lifecycle has been clarified.
  • The Tag Library Descriptor has been aligned with other J2EE descriptors and extended with new elements.

Tomcat 4.0 is the reference implementation for the JSP 1.2 and Servlet 2.3 specifications. The Tomcat server is a Java-based Web application container that was created to run Servlet and JavaServer Page Web applications. Tomcat 4.0 was released at the same time as the JSP 1.2 spec, so you can try out the new features now. Some of the commercial Web containers, such as New Atlanta's ServletExec 4.1, already support the new specifications as well.

This article will cover all of the new stuff the specification group has cooked up and show you how to use the new features. Most of the changes apply to programmers and container developers, so this article assumes you're a Java programmer familiar with the JSP 1.1 specification. If you're primarily a page author, I'm afraid there's not much of interest for you in this article, but rest assured that you will benefit from JSP 1.2's more powerful custom tag libraries, more efficient containers, and better portability between containers.

Servlet 2.3 and Java 2

JSP 1.2 is based on the latest Servlet specification: Servlet 2.3. This gives a JSP 1.2 application access to all new servlet features, such as listeners, filters, and internationalization improvements. Later in this article, I describe how you can use listeners in a JSP tag library. I also recommend that you read Jason Hunter's Servlet 2.3 articles in JavaWorld (see the Resources section) to learn how to take advantage of the other new Servlet features.

Both JSP 1.2 and Servlet 2.3 require the Java 2 platform. The good news is that Web containers can now take advantage of all the new features added since JDK 1.1, such as collections, more advanced class loaders, and flexible security. The same is, of course, true for your own Web applications. The bad news is that JSP 1.2 applications will not run on platforms that only support JDK 1.1. There are very few of these left at this point, so this should not be a problem for most of us.

JSP 1.2 is backwards compatible with JSP 1.1. If you have a JSP 1.1 application, it should run without any tweaking in a JSP 1.2- compliant container.

Include Without Flushing

Comment on this articleWhat are your thoughts on the new JSP 1.2 specification?
Post your comments

In JSP 1.1, the flush attribute was mandatory and only accepted the value true. This meant that as soon as you used the include action, all content generated up to that point was flushed to the browser, and you could no longer forward to another page or set response headers. This caused a lot of confusion and side-effect problems that were hard to understand and deal with. Readers of the JSP-INTEREST mailing list, or any other JSP forum, know what I'm talking about.

JSP 1.2 takes advantage of improvements in the Servlet 2.3 specification to finally get rid of this restriction. The flush attribute of the include action now accepts the value false. In fact, the attribute is now optional and false is the default value, so in most cases you can just omit it and forget about all the flushing issues of the past.

With JSP 1.1, a page like this resulted in an error due to the use of a forward after an include:

<jsp:include page="common.jsp" flush="true" /> 
<% if (someCondition) { %> 
  <jsp:forward page="another.jsp" /> 
<% } %>

In JSP 1.2 it's legal to change the flush attribute value to false or omit it.

You can even use the include action in the body of a custom action element:

<xsl:apply xsl="style.xsl"> 
  <jsp:include page="someXML.jsp" /> 

This was also a no-no in JSP 1.1 due to buffering issues that have been resolved in JSP 1.2.

Pages: 1, 2, 3, 4

Next Pagearrow