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

Squash bugs, heat Beans and learn a litle more about JSP.

Burning Up

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.

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.

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(PageContextImp
l.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.ja
va: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:797)
	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.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:797)
	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:

<%@ page errorPage="error.jsp" %>
<%
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.

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:

<html>
<head>
<basefont face="Arial">
</head>

<body bgcolor="white">
<h2>Oops!</h2>
Something bad just happened. Click here to go back to the main page.
</body>

</html>

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:

<%@ page isErrorPage="true" %>
<html>
<head>
<basefont face="Arial">
</head>

<body bgcolor="white">
<h2>Oops!</h2>
Something bad just happened:
<br>
<b><i><%= exception.getMessage() %></i></b>

</body>
</html>

And this time, the output will be a little more helpful.

Oops!
Something bad just happened:
/ by zero

Pay special attention to the first line of the script; the directive

<%@ page isErrorPage="true" %>

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.

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

<html>
<head>
<basefont face="Arial">
</head>

<body>

<form action="number.jsp">
Enter a number between 1 and 3 <input type=text name=number size=1>
</form>

</body>
</html>

and this is the server-side JSP script which checks it for errors, and throws an exception if certain conditions are not met.

<html>
<head>
<basefont face="Arial">
</head>

<body>
<%@ page errorPage="error.jsp" %>
<%
String temp = request.getParameter("number");

int number = Integer.parseInt(temp);

if (number != 2)
	{
	throw new Exception ("How dumb can you get?!") ;
	}
 else
 	{
	out.println("Hmmm...maybe you're not as dumb as you look!");
	}
 %>

</body>
</html>

Next up, a brief look at JavaBeans and how they integrate with the JSP environment.

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 <jsp:useBean> 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”.

<jsp:useBean id="vanilla" scope="page" class="iceCream">
</jsp:useBean>

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.

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”

<jsp:useBean id="account" scope="page" class="Bank">
<jsp:setProperty name="account" property="accountBalance" value="7683" />
</jsp:useBean>

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.

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:

Download: temperature.zip

And here’s a page which uses it:

<html>
<head>
<basefont face="Arial">
</head>
<body>
<!-- initialize the Bean, set some defaults -->

<jsp:useBean id="c" scope="page" class="Temperature">
<jsp:setProperty name="c" property="celsius" value="10" />
</jsp:useBean>

<%
// get the current temperature
out.println("Temperature in Celsius is " + c.getCelsius() + "<p>");

// turn up the heat
c.setCelsius(36.8);

// get the current temperature
out.println("Temperature in Celsius is now " + c.getCelsius() + "<p>");

// convert the temperature to Fahrenheit
out.println(c.getCelsius() + " Celsius is " +
c.convertCelsiusToFahrenheit(c.getCelsius()) + " Fahrenheit<p>");

// ...and back again
out.println(c.getFahrenheit() + " Fahrenheit is " +
c.convertFahrenheitToCelsius(c.getFahrenheit()) + " Celsius<p>");
%>

</body>
</html>

And here’s the output:

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!

Note: All examples in this article have been tested on Linux/i586 with Tomcat 3.2 and JServ 1.1. Examples are illustrative only, and are not meant for a production environment. YMMV!

This article was first published on 18 Apr 2001.