Login

The JSP Files (part 7): Bugs, Beans And Banks

In this week’s episode, find out how the JSP Exception object
provides developers with a graceful way to recover from script errors. And
then take a quick tour of the JSP directives you need to know in order to
integrate standalone JavaBeans into your JSP scripts.No programmer, no matter how good (s)he is, writes bug-free code all the time.
And so, most programming languages come with built-in capabilities to catch
errors and take remedial action. This action could be something as simple as
displaying an error message, or as complex as heating your computer’s innards
until they burst into flame (just kidding!)

Over the next couple of
pages, we’re going to look at some of the error-management techniques available
in JSP, and also demonstrate some of the JSP directives used to integrate
standalone JavaBeans into your JSP scripts.{mospagebreak title=Bugathon} First,
though, a quick lesson in semantics. There’s an important distinction to be
aware of between “errors” and “exceptions”.

JSP “errors” cannot usually
be trapped, as they usually involve problems beyond the application developer’s
control – things like the server running out of memory, missing files or a
corrupted filesystem.

The term “exceptions”, on the other hand, refers to
those errors which can be tracked and controlled. For example, if an argument
provided to a function is incorrect, JSP will “throw” an “invalid argument”
exception, together with a stack trace or detailed explanation of the problem.
Exceptions like these can be “caught” by the application, and appropriately
diverted to an exception-handling routine.

Since JSP shares much in
common with Java, it won’t surprise you to learn that JSP’s concept of
exceptions is derived almost entirely from Java. In Java, an exception is an
instance of an object; in JSP, an implicit Exception object is available to
identify and manage exceptions.{mospagebreak title=Exceptionally Clever} There
are two basic components to the process of handling JSP exceptions:

1.
Add a directive to your JSP script identifying the name of the file to call when
an exception occurs.

2. Create an appropriate “error page”, optionally
using the Exception object to obtain greater detail about the
exception.

Let’s illustrate this process with a simple example. Here’s a
JSP script that divides a number by zero – a process guaranteed to make any
programming language scream in anguish.

<%
int a = 19;
int b = 0;
int c = a/b;
%>

Here’s the output:

Error: 500
Internal Servlet Error:
javax.servlet.ServletException: / by zero
at
org.apache.jasper.runtime.PageContextImpl.handlePageException
(PageContextImpl.java:459)
at _0002fb_0002ejspb_jsp_2._jspService(_0002fb_0002ejspb_jsp_2.java:72)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.jasper.servlet.JspServlet$JspServletWrapper.service
(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile
(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service
(JspServlet.java, Compiled Code)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at
org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:79
7)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at
org.apache.tomcat.service.connector.Ajp12ConnectionHandler.processConnection
(Ajp12ConnectionHandler.java:166)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java,
Compiled Code)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java,
Compiled Code)
at java.lang.Thread.run(Thread.java, Compiled Code)
Root cause:
java.lang.ArithmeticException: / by zero
at _0002fb_0002ejspb_jsp_2._jspService(_0002fb_0002ejspb_jsp_2.java:62)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.ja
va:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFil(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service
(JspServlet.java, Compiled Code)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService
(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at
org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:79
7)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at
org.apache.tomcat.service.connector.Ajp12ConnectionHandler.processConnection
(Ajp12ConnectionHandler.java:166)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java,
Compiled Code)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run
(ThreadPool.java, Compiled Code)
at java.lang.Thread.run(Thread.java, Compiled Code)

Ugly, huh?

In order to have this exception handled
by JSP, so that the user never has to see something so ugly, you need to simply
add a

<%@ page errorPage=”error.jsp” %>

to the script, so that it looks like this:

<%
int a = 19;
int b = 0;
int c = a/b;
%>

An exception thrown by the script will now be caught by
JSP, and automatically routed to “error.jsp”. Let’s look at that
next.{mospagebreak title=Bad News} The exception-handling routine “error.jsp”
can be as simple or complex as you want to make it. If you want something
friendly and easy to understand, you could use the following:

Now, if you run the example above again, JSP should
automatically divert you to this page instead of grossing you out with long and
icky error messages.

The script above simply notifies the user that an
error has occurred; it says nothing about the type of error, or the reasons for
its occurrence. If you’d like to display this as well, you need to use the
built-in Exception object to obtain information about the exception. The next
example uses a modified “error.jsp” to illustrate this:

tells JSP that this is an error page, and initializes an
instance of the Exception object for use.

The Exception object comes with
a couple of useful methods – the exception.getMessage() method is used to obtain
a description of the error, while the exception.printStackTrace() method is used
to print debugging information.{mospagebreak title=You Throw(), I’ll Catch} It’s
also possible to use the Java “throw” construct to artificially induce an
exception in your JSP script. This comes in handy, for example, when validating
form field data – if the values entered are not in the expected format, you can
throw an exception (with an informative error message) and re-direct the user to
an error page.

Here’s an example of how this can be used. This is a
simple form which asks you to enter a number

Next up, a brief look at JavaBeans and how they integrate
with the JSP environment.{mospagebreak title=Bean Bag} One of the most
compelling things about JSP, from a development point of view, is that it allows
for the easy integration of existing JavaBeans into JSP scripts (for those of
you not in the loop, JavaBeans is object-oriented technology which allows
developers to build reusable Java components or applications). The advantages of
this are obvious: an organization which has already made an investment in
JavaBeans technology can leverage off it to quickly reuse existing code modules,
at minimal time and cost.

We’re not going to get into the nitty-gritty of
building a JavaBean here – there are innumerable tutorials out there on the
topic, including a good one from Sun Microsystems at http://java.sun.com/docs/books/tutorial/javabeans/.
Instead, we’re simply going to touch briefly on the JSP constructs which allow
you to import a Bean into your JSP script, set Bean properties and access Bean
methods.

JavaBeans are brought into a JSP script by means of the
action, which creates an instance of the Bean and identifies the
scope of its activities.

The following code snippet creates an instance
of the Bean “iceCream”, identifies it with the unique ID “vanilla” and defines
its scope to be limited to the “page”.

The “scope” attribute above defines the extent of the
Bean’s influence. For example, “scope=page” implies that the instance will
remain active for the current page, while “scope=session” indicates that the
instance will remain available throughout the session.{mospagebreak title=Taking
It To The Bank} Closely related to <jsp:useBean> is
<jsp:setProperty>, typically used to set Bean properties; these properties
may be set explicitly, or on the basis of parameters available in the Request
object (you remember this, don’t you?)

The following code snippet uses
the <jsp:setProperty> action to assign the value “7683” to the Bean
property “accountBalance”. Note that the <jsp:setProperty> action
references an instance of a previously-defined Bean, named “account”

If you’d like to set instance properties on the basis of
data in the Request object – say, form values – you could use

<jsp:setProperty name=”account” property=”*” />

and JSP would automatically iterate through the Request
object, match parameter names with available Bean properties, and assign values
appropriately.{mospagebreak title=Turning Up The Heat} In order to illustrate
how this works, we’ve written a simple Bean which accepts temperature values and
converts them between Celsius and Fahrenheit scales. Here it is:

Temperature in Celsius is 10.0
Temperature in Celsius is now 36.8
36.8 Celsius is 98.24 Fahrenheit
98.24 Fahrenheit is 36.8 Celsius

And that’s about it for the moment. In the next – and
final – article in this series, we’ll be exploring JSP’s tag libraries, which
allow Web designers to add powerful funtionality to their Web pages without
knowing JSP. Make sure you make it out here for that one!