Catching Exceptions and Error Handling
- Catching and throwing exceptions in JSP is much like Java.
- To catch exceptions you use a try and catch clause.
- The example below shows the "throws", "try", and "catch" clauses.
- Note the use of the PrinterWriter that allows the scriplet to
call printStackTrace
- "out" is an output stream representing the HTML page being
created.
- A PrintWriter can be created using "out" , this is then passed
to printStackTrace (it expects a PrintWriter object)
ExceptionDemo.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head><title>Exceptions Demo</title></head>
<body>
<h3> Exception Demo </h3>
<form action = "ExceptionDemo.jsp" name="loginform" method="post">
<table>
<tr><td> X </td><td> <input
type="text" name="xfield" value="" length=10></td></tr>
<tr><td> Y </td><td> <input
type="text" name="yfield" value="" length=10></td></tr>
<tr><td colspan=2><input type="submit" value=" + "
name="function">
<input type="submit"
value=" - " name="function">
<input type="submit"
value=" * " name="function">
<input type="submit"
value=" / " name="function">
</td></tr>
</table>
</form>
<p>
<%
String xinput = request.getParameter("xfield");
String yinput = request.getParameter("yfield");
String function = request.getParameter("function");
if(xinput != null && yinput != null && function
!= null)
{
try{
double x = Double.parseDouble(xinput);
double y = Double.parseDouble(yinput);
double z=0;
if(function.equals(" + "))
z = x+y;
if(function.equals(" - "))
z = x-y;
if(function.equals(" * "))
z = x*y;
if(function.equals(" / "))
z = x/y;
%>
<p>
<b> <%= x +function+y+" = "+z %> </b>
<%
}// end of try
catch(NumberFormatException nfe)
{
%>
<p>
<b><blink> X and Y must be integer numbers, try
again!</blink></b>
<p>
<%= "Input Error : "+nfe.getMessage()+"\n"%>
<p>
<%
java.io.PrintWriter writer = new java.io.PrintWriter(out);
writer.println("Stack trace :");
writer.println("<p>");
nfe.printStackTrace(writer);
}// end of catch
} // end of if
%>
</body>
</html>
Error Pages
- An error page is a web page that can be used to inform the user
of errors that occur in the JSP.
- It can be used as a catch-all problem page to handle exceptions
not caught explicitly by the JSP code
- Error pages are declared using the jsp errorPage
directive tag (<%@)
<%@
page errorPage =
"ErrorPage.jsp" %>
- When an uncaught exception occurs on a jsp page with the
errorPage directive, then the speficied jsp is loaded.
- This jsp (ErrorPage.jsp) can be used by one or more JSP to handle
errors.
- When creating the ErrorPage jsp, you should use the isErrorPage
directive, this allows the jsp to access the exception object from the
offending page
<%@page
isErrorPage = "true" %>
- the exception object can be used to access the getMessage() and
printStackTrace() methods.
- In the example below, the try/catch code was removed and the
error page code added.
- Using error pages is not as precise as try/catch, but it is
better than nothing.As a general rule, use try/catch on common errors
you want to handle, and error pages to catch anything else that might
come up.
ExceptionDemo2.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head><title>Exceptions Demo 2</title></head>
<body>
<%@ page errorPage = "ErrorPage.jsp" %>
<h3> Exception Demo </h3>
<form action = "ExceptionDemo2.jsp" name="loginform"
method="post">
<table>
<tr><td> X </td><td> <input
type="text" name="xfield" value="" length=10></td></tr>
<tr><td> Y </td><td> <input
type="text" name="yfield" value="" length=10></td></tr>
<tr><td colspan=2><input type="submit" value=" + "
name="function">
<input type="submit"
value=" - " name="function">
<input type="submit"
value=" * " name="function">
<input type="submit"
value=" / " name="function">
</td></tr>
</table>
</form>
<p>
<%
String xinput = request.getParameter("xfield");
String yinput = request.getParameter("yfield");
String function = request.getParameter("function");
if(xinput != null && yinput != null && function
!= null)
{
double x = Double.parseDouble(xinput);
double y = Double.parseDouble(yinput);
double z =0;
if(function.equals(" + "))
z = x+y;
if(function.equals(" - "))
z = x-y;
if(function.equals(" * "))
z = x*y;
if(function.equals(" / "))
z = x/y;
%>
<p>
<b> <%= x +function+y+" = "+z %> </b>
<%
} // end of if
%>
</body>
</html>
ErrorPage.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page isErrorPage = "true" %>
<html>
<head><title>Error</title></head>
<body>
<h1>An error occurred while processing your request</h1>
<input type="button" value="click
here to try again" onclick="javascript:history.back()">
<p>
<%= "Input Error :
"+exception.getMessage()+"\n"%>
<p>
<%
java.io.PrintWriter
writer = new java.io.PrintWriter(out);
writer.println("Stack
trace :");
writer.println("<p>");
exception.printStackTrace(writer);
%>
</body>
</html>