You are on page 1of 2

Hans's Top Ten JSP Tips - O'Reilly Media http://www.oreillynet.com/pub/a/oreilly/java/news/jspt...

Sign In/My Account | View Cart

O'Reilly Home Community Books & Videos Safari Books Online Conferences Training School of Technology About

Search Search Tips

Listen
Hans's Top Ten JSP Tips Print Resource Management: A
Subscribe to Newsletters Critical Look at RAII Michael
by Hans Bergsten
11/20/2000 ShareThis Feathers

JavaServer Pages (JSP) is the latest tool for Java-based Web application development. A JSP page contains regular markup The Logs That Bind.. Michael
code plus special JSP elements. When the page is requested, the static markup code and the dynamic content produced by the Feathers
JSP elements are combined to form the complete response to the request.
Outside the Design
It's impossible to tell you everything you need to know to use JSP effectively in a short article like this (that's why I wrote Boundary Michael Feathers
JavaServer Pages). Instead, this article focuses on the frequently asked questions that I've heard from people who have just
Can a program lie to you
started to play around with JSP.
the way a story or essay
1. Choosing the Right Include Mechanism can? Andy Oram

> More from Beautiful Code


A JSP page can include page fragments from other files to form the complete response. You can use this, for instance,
to keep header, footer, and navigation bar content in separate files and include them in all other pages. There are two
include mechanisms: the include directive and the include action. It's not always obvious which one to use, though.

The include directive, <%@ include file="filename.inc" %>, includes the content of the specified file during the
Tagged Articles
translation phase--when the page is converted to a servlet. The main page and the included file are simply merged. This
means that scripting variables declared in one file (using scripting elements or an action element like <jsp:useBean>)
are visible in all files and must have unique names. Some containers detect changes in files included with the directive,
Post to del.icio.us
but the specification doesn't require it. Therefore, changes you make to the included file in a running system may not
be reflected immediately; you may have to update the main JSP page, or remove the class file generated for the main
This article has been
page in order to see the change.
tagged:
The include action, <jsp:include page="pagename.jsp" flush="true" />, jsp
includes the response generated by executing the specified page (a JSP page or a
servlet) during the request processing phase--when the page is requested by a Related Reading
user. As opposed to the include directive, the page name can be specified as a Articles that share the tag
so-called request-time attribute value, so which page to include can be decided jsp:
when the main page is requested. Since it's the response generated by the page
that is included, not the content of the page itself, scripting variables declared in Improving JSF by Dumping
one file are not available to the other files. To share an object between the pages JSP (74 tags)
you must instead place it in one of the following JSP scopes: request, session or
application scope. If you change a page included with the include action, the JSP 2.0: The New Deal, Part 3
change always takes effect immediately. (24 tags)

My rule of thumb for when to use the different mechanisms is this: Six Cool New JSP and Servlet
Features (19 tags)
Use the include directive if the file changes rarely. It's the fastest
mechanism. If your container doesn't automatically detect changes, you JSP 2.0: The New Deal, Part 2
(19 tags)
can force the changes to take effect by deleting the main page class file.
What Is a Portlet (17 tags)
Use the include action only for content that changes often, and if which
page to include cannot be decided until the main page is requested. View All
2. Dealing with Buffer Flushing Issues

An HTTP response message contains both headers and a body. The headers tell
the browser things like what type of data the body contains (HTML text, an Java Server Pages
By Hans�Bergsten Sponsored Resources
image), the size of the body, if the body can be cached, and so on. Headers are
also used to set cookies and to tell the browser to automatically get another page
(a redirect). All response headers must be sent to the browser before the body is
Inside Lightroom
sent.

To allow parts of the body to be produced (from static template text as well as content generated dynamically by JSP
elements) before headers are set, the body is buffered. Instead of sending the response to the browser as soon as
something is written to the response body, the JSP container writes all static markup code and all dynamic content
generated by JSP elements to the buffer. At some point, such as when the buffer is full or the end of the page is
reached, the container sends all headers that have been set followed by the buffered body content. In servlet speak,
this is called committing the response. After the response has been committed, you can't set headers, such as for
cookies or a redirection instruction. Another thing you can't do is forward the request to another page.

In most cases, this is not a problem. The default buffer size is 8KB, more than enough for a typical page, and you can
increase it with the buffer attribute of the page directive. But if you use the include action in a page, you may be in for
a surprise. Due to limitations in the way the servlet features used by <jsp:include> are specified, the buffer is always
flushed before the target page is invoked. This means that you can't set headers or use <jsp:forward> after a
<jsp:include> action.

An unfortunate side-effect of this automatic flushing is that runtime errors triggered by JSP elements after a
<jsp:include> action may not be reported correctly, since many JSP containers use the forward mechanism to display
the error page. If you see an error message like "response already committed" in a page with <jsp:include> elements,
I suggest that you use the include directive instead (at least until you have isolated the problem).

3. Passing Data Between Pages Processing the Same Request

Quite often, more than one JSP page is used to process a single request. One example is when you use the include
action to include a common navigation bar in all pages. Another example is when you use one page for request
processing (e.g., input validation and database access) and then use the forward action, <jsp:forward
page="pagename.jsp" />, to let another page generate the response. There are two ways to pass data from one page to
another: using request parameters or request attributes.

First of all, the page invoked using a <jsp:include> or <jsp:forward> action has access to all HTTP request parameters

1 of 2 14/6/2010 2:05 AM
Hans's Top Ten JSP Tips - O'Reilly Media http://www.oreillynet.com/pub/a/oreilly/java/news/jspt...

About O'Reilly Content Archive More O'Reilly Sites


Academic Solutions Business Technology O'Reilly Radar
Authors Computer Technology Ignite
Contacts Google Tools of Change for Publishing
©2010, O'Reilly Media, Inc. Customer Service Microsoft Digital Media
(707) 827-7000 / (800) Jobs Mobile Inside iPhone
998-9938 Newsletters Network makezine.com
All trademarks and registered O'Reilly Labs Operating System craftzine.com
trademarks appearing on Press Room Digital Photography hackszine.com
oreilly.com are the property of Privacy Policy Programming perl.com
their respective owners. RSS Feeds Software xml.com
Terms of Service Web
User Groups Web Design Partner Sites
Writing for O'Reilly InsideRIA
java.net
O'Reilly Insights on
Forbes.com

2 of 2 14/6/2010 2:05 AM

You might also like