|
Although our demo application worked and you learned about specifying error pages
e.g. ExceptionHandler.jsp, in your normal JSP pages e.g. FormHandler.jsp, there is still
a catch. The exception message that was given to the user i.e. java.lang.NumberFormatException,
is not very useful, is it? To give a more useful message keep reading.
Improved FormHandler.jsp page
Remove the FormHandler code in FormHandler.jsp page and add the following one :
<%
int age;
try {
age = Integer.parseInt(request.getParameter("age"));
} catch (NumberFormatException e) {
throw new JspException("Please enter a valid integer value!");
}
%>
Explanation
This time we catch the NumberFormatException locally and throw a new JspException
with our own exception message. JspException is a JSP special exception class which
extends java.lang.Exception. So all we had to do is to give our own message to the
JspException class. Now lets see what will happen this time.
We will just have to edit one single line in ExceptionHandler.jsp for it to work.
Improved ExceptionHandler.jsp
Simply change the exception handler code with this one :
<font color="red">
<%= exception.getMessage() %><br>
</font>
That's it. Now view the Form.html page again and don't enter any value and press the
submit button. This time you will not see "java.lang.NumberFormatException", rather the
message will be :
Please enter a valid integer value!
And like before you can see the complete stack trace by viewing the source for the
FormHandler.jsp page in your browser.
Summary
In this article we learned about exception handling feature of JSP pages. We learned
that to make a JSP an error page, we have to set the isErrorPage attribute in the
page directive at the top to true. Now this JSP page will be an error page and it will
be made available as java.lang.Throwable object with name "exception". We can use methods of
"exception" object e.g. exception.getMessage(), exception.toString(), exception.printStackTrace(),
to display useful information to the user.
To make exceptions from other JSP pages to go to the error page above, we have to
set the errorPage attribute in the page directive at the top to the location of our
JSP error page e.g. errorPage="ExceptionHandler.jsp".
Although our demo application worked and you learned about specifying error pages
e.g. ExceptionHandler.jsp, in your normal JSP pages e.g. FormHandler.jsp, there is still
a catch. The exception message that was given to the user i.e. java.lang.NumberFormatException,
is not very useful, is it? To give a more useful message keep reading.
Improved FormHandler.jsp page
Remove the FormHandler code in FormHandler.jsp page and add the following one :
<%
int age;
try {
age = Integer.parseInt(request.getParameter("age"));
} catch (NumberFormatException e) {
throw new JspException("Please enter a valid integer value!");
}
%>
Explanation
This time we catch the NumberFormatException locally and throw a new JspException
with our own exception message. JspException is a JSP special exception class which
extends java.lang.Exception. So all we had to do is to give our own message to the
JspException class. Now lets see what will happen this time.
We will just have to edit one single line in ExceptionHandler.jsp for it to work.
Improved ExceptionHandler.jsp
Simply change the exception handler code with this one :
<font color="red">
<%= exception.getMessage() %><br>
</font>
That's it. Now view the Form.html page again and don't enter any value and press the
submit button. This time you will not see "java.lang.NumberFormatException", rather the
message will be :
Please enter a valid integer value!
And like before you can see the complete stack trace by viewing the source for the
FormHandler.jsp page in your browser.
Summary
In this article we learned about exception handling feature of JSP pages. We learned
that to make a JSP an error page, we have to set the isErrorPage attribute in the
page directive at the top to true. Now this JSP page will be an error page and it will
be made available as java.lang.Throwable object with name "exception". We can use methods of
"exception" object e.g. exception.getMessage(), exception.toString(), exception.printStackTrace(),
to display useful information to the user.
To make exceptions from other JSP pages to go to the error page above, we have to
set the errorPage attribute in the page directive at the top to the location of our
JSP error page e.g. errorPage="ExceptionHandler.jsp".
|