Datasheet
Patrick c01.tex V3 - 09/18/2009 12:15pm Page 15
Chapter 1: Building Web Applications in WebLogic
Accessing the
ErrorCreator.jsp
page from a browser now causes a useful error message to be displayed
to the user. The page could conform to the look and feel of the site itself and could easily include links to
retry the failed operation, send an email to someone, or go back to the previous page.
As an alternative to specifying the
errorPage
on each individual JSP page, a default error-handling page
may be specified for the entire web application using the
<error-page>
element in
web.xml
:
<error-page>
<error-code>500</error-code>
<location>/ErrorPage.jsp</location>
</error-page>
These two mechanisms for specifying the error page may look very similar but are, in fact, implemented
quite differently by WebLogic Server. The
<%@ page errorPage="
...
"%>
directive modifies the generated
servlet code by placing all JSP scriptlet code, output statements, and other servlet code in a large try/catch
block. Specifying the error page in
web.xml
does not affect the generated servlet code in any way. Instead,
uncaught exceptions that escape the
_jspService()
method in the original page are caught by the web
container and forwarded to the specified error page automatically.
Which technique is best? Unless the target error page must differ based on the page encountering the
error, we recommend the
<error-page>
element in
web.xml
for the following reasons:
❑ A declarative and global technique has implicit benefits over per-page techniques. Individual
pages that require different error pages can easily override the value in
web.xml
by including the
page
directive.
❑ The information describing the original page request is more complete if the
<error-page>
element is used rather than the
page
directive. Specifically, calling
request.getRequestURL()
in the error page returns the URL of the original page rather than the URL of the error page,
and additional attributes are placed on the request that are not present if the
page
directive is
employed.
Best Practice
Create a friendly and useful error page, and make it the default error page for all server
errors using the
<error-page>
element in
web.xml
. Override this default error page
using the
page
directive in specific pages, if necessary.
Use JSTL Tags to Reduce Scriptlet Code
The JavaServer Pages Standard Tag Library (JSTL) is a custom tag library that encapsulates many core
functions required within JSP pages and virtually eliminates the need for JSP scriptlet code. Common
constructs, such as conditionals, loops, accessing request or session data, placing data in the response
HTML output, formatting output, displaying language-sensitive strings, and many other functions, are
implemented in the JSTL library in a standard way. JSTL represents a huge improvement over the old
jsp:useBean
and
jsp:getProperty
techniques.
Custom tags can be difficult to create, but no knowledge of their construction is required to use them
successfully. All you need is a good reference on the tag library you are trying to use and a basic
understanding of the syntax for calling custom tags within your JSP pages.
15