Tải bản đầy đủ (.pdf) (15 trang)

The JSP Files (Part 7) - Bugs, Beans and Banks

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (27.1 KB, 15 trang )

The JSP Files (part 7): Bugs, Beans And Banks
By Vikram Vaswani and Harish Kamath
This article copyright Melonfire 2000−2002. All rights reserved.
Table of Contents
Burning Up..........................................................................................................................................................1
Bugathon..............................................................................................................................................................2
Exceptionally Clever...........................................................................................................................................3
Bad News..............................................................................................................................................................6
You Throw(), I'll Catch......................................................................................................................................8
Bean Bag............................................................................................................................................................10
Taking It To The Bank.....................................................................................................................................11
Turning Up The Heat.......................................................................................................................................12
The JSP Files (part 7): Bugs, Beans And Banks
i
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.
Burning Up 1
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.
Bugathon 2
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
Exceptionally Clever 3
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.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
The JSP Files (part 7): Bugs, Beans And Banks
Exceptionally Clever 4

×