JSP Exception handling

I keep forgetting this, so I thought Id make it easier for myself and other schizophrenics like me. Huh 🙂
To demonstrate the run-time exception handling feature of JSP pages, we will build three pages.

Form.html – 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.
i. Form.html
Create a new Form.html page.

<html>
<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.html page simply displays a single input field Form to the user to enter his age in months (Yes, I calculate it in months.. innovative ha). 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.

ii. FormHandler.jsp
Create new FormHandler.jsp page. :

<%@ page errorPage=”ExceptionHandler.jsp” %> <html> <body> <%– Form Handler Code –%> <% int age; age = Integer.parseInt(request.getParameter(“age”)); %> <%– Displaying User Age –%> <p>Your age is : <%= age %> months.</p> </body> </html>

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. 666 ) value in the input field then we display that value back to the user.

<p>Your age is : <%= age %> months.</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. “Marshall Mathers”), of String type instead of an integer ?.

These things will be handled by the ExceptionHandler.jsp JSP page.

iii. ExceptionHandler.jsp
Create a new ExceptionHandler.jsp page.

<%@ page isErrorPage=”true” import=”java.io.*” %>
<html>
<head>
<title>Exceptional Even Occurred!</title>
 </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>

Explanation
To make a JSP page exception handler ( i.e. errorPage ), you 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 entire story.

<%
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 you’ll have to import java.io.* package in your JSP page by changing the page directive at the top to :

<%@ page isErrorPage=”true” import=”java.io.*” %>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: