Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : JSP : Exception Handling in JSP Pages
 

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".


Previous ( 1 Gone )( No Further Pages )

See all comments and questions (post-ad) posted for this tutorial.


Related Articles
  1. Sending E-Mails with JSP Pages
  2. Installing, Configuring, & Running Tomcat 6.0 Servlet Container on Microsoft Windows, and Developing & Running your first JSP Page
  3. Examining Java Server Pages in detail
  4. Introducing JavaServer Pages
  5. Professional JSP
  6. Combining Servlets, JSP, and JavaBeans
  7. Google on your WAP phone using Java Server Pages

Comments/Questions ( Threads: 2, Comments: 2 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Error handling in JSP
  2. java.io.NotSerializableException

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.