Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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.
Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article