Home arrow Java & J2EE arrow 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.

TABLE OF CONTENTS:
  1. The JSP Files (part 7): Bugs, Beans And Banks
  2. Bugathon
  3. Exceptionally Clever
  4. Bad News
  5. You Throw(), I'll Catch
  6. Bean Bag
  7. Turning Up The Heat
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 2
April 18, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

 
 
>>> More Java & J2EE Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: