Exception Handling in JSP Pages
 
Building the Demo Pages
 
To demonstrate the run-time exception handling feature of JSP pages, we will build three pages:
• Form.php - Display a Form to the user to enter his or her age.
• FormHandler.jsp - A JSP Page which receives this value and prints it on the user screen.
• ExceptionHandler.jsp - An exception handler JSP page which is actually an error page to which control will be passed when an exception is thrown.
 
Form.jsp
Create a new Form.jsp page. Copy the following code and paste it into the Form.php page :
 
<html>
<head>
<style>
body, input { font-family:Tahoma; font-size:8pt; }
</style>
</head>
<body>

<!-- HTML Form -->
<form action="FormHandler.jsp" method="post">
Enter your age ( in years ) :
<input type="text" name="age" />
<input type="submit" value="Submit" />
</form>

</body>
</html>
 
Explanation
Form.php page simply displays a single input field Form to the user to enter his age in years. The name of input field where user will enter his/her age is "age". We will use this input field name "age" in the FormHandler.jsp page to receive it's value.
Output
 
FormHandler.jsp
Create new FormHandler.jsp page. Copy and paste the following code in it :
 
<%@ page errorPage="ExceptionHandler.jsp" %>
<html>
<head>
<style>
body, p { font-family:Tahoma; font-size:10pt; }
</style>
</head>
<body>

<%-- Form Handler Code --%>
<%
int age;

age = Integer.parseInt(request.getParameter("age"));
%>

<%-- Displaying User Age --%>
<p>Your age is : <%= age %> years.</p>

<p><a href="Form.php">Back</a>.</p>

</body>
</html>
Output
 
Explanation
Code above is rather simple. Notice the first line, the page directive. It specifies an errorPage ExceptionHandler.jsp, our exception handler JSP page.
 
<%@ page errorPage="ExceptionHandler.jsp" %>
 
Then we declare an int variable "age". Then using the static method of Integer class we parse the entered value using Integer.parseInt() method. The value is retrieved using request.getParameter() method. The argument to request.getParameter() is the name of Form field whose value we want to retrieve.
 
<%
int age;

age = Integer.parseInt(request.getParameter("age"));
%>
If all goes well and user entered an int ( e.g. 12345 ) value in the input field then we display that value back to the user.
 
<p>Your age is : <%= age %> years.</p>
 
Now things can go wrong and exceptional events can occur. For example, if user didn't enter a value and what if user entered his name ( e.g. "Aman Kumar"), of String type instead of an integer ?.
 
These things will be handled by the ExceptionHandler.jsp JSP page.
 
ExceptionHandler.jsp
Create a new ExceptionHandler.jsp page. Copy and paste the following code in it :
 
<%@ page isErrorPage="true" import="java.io.*" %>
<html>
<head>
<title>Exceptional Even Occurred!</title>
<style>
body, p { font-family:Tahoma; font-size:10pt; padding-left:30; }
pre { font-size:8pt; }
</style>
</head>
<body>

<%-- Exception Handler --%>
<font color="red">
<%= exception.toString() %><br>
</font>

<%
out.println("<!--");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
out.print(sw);
sw.close();
pw.close();
out.println("-->");
%>

</body>
</html>
Output
 
Explanation
To make a JSP page exception handler ( i.e. errorPage ), we have to specify isErrorPage attribute in the page directive at the top and set it's value to true.
<%@ page isErrorPage="true" %>
 
When a JSP page has been declared an errorPage, it is made available an object with name of "exception" of type java.lang.Throwable. We use different methods of this exception object to display useful information to the user.
<font color="red">
<%= exception.toString() %><br>
</font>
 
We can put the stack trace information for debugging inside HTML <!-- --> comment tags. So that user only sees a useful message and the sysadmin, developers can see the whole things.
<%
out.println("<!--");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
out.print(sw);
sw.close();
pw.close();
out.println("-->");
%>
 
To use the PrintWriter and StringWriter classes we'll have to import java.io.* package in our JSP page by changing the page directive at the top to :
<%@ page isErrorPage="true" import="java.io.*" %>