HomeJava & J2EE Page 3 - The JSP Files (part 7): Bugs, Beans And Banks
Exceptionally Clever - Java
In this week's episode, find out how the JSP Exception objectprovides developers with a graceful way to recover from script errors. Andthen take a quick tour of the JSP directives you need to know in order tointegrate standalone JavaBeans into your JSP scripts.
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.