• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Forwarding exception from servlet to JSP

 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a small MVC web application.

The servlet performs different database operations.
If any exception occurs in the servlet, I want to forward to error page.

Is the excpetion thrown in servelet is available as exception object in jsp?

Right now, I am using setAttribute and doing as below:


Is this the only way to forward error from servlet to JSP?
Are there any other simple and efficient ways?
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is this the only way to forward error from servlet to JSP?
Are there any other simple and efficient ways?


Yes. You can put things in your web.xml file that will redirect all exceptions to the same error page, or you can pick out specific exceptions to go to different error pages, or you can go to error pages in response to HTTP status codes, e.g. 404(file not found). The error page is essentially handling the exception, so the container provides the error page with the exception object--if the error page contains the following page directive:

<%@ page isErrorPage="true" %>

Here is an example of what goes in the web.xml file:

for all exceptions
-----------------


The path to the error page is relative to the webapp root and starts with a slash.
[ October 06, 2006: Message edited by: sven studde ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use the built-in mechanism for error handling as sven pointed out (and you should), a lot of information about the nature of the error is availble as scoped variable on the request.

See the Servlet Specification for details.
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case, how can I redirect from servlet to JSP (without actually setting the exception attribute in servlet)?
Can you please give code snippet for this?
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you use the built-in mechanism for error handling as sven pointed out (and you should)

The error page is essentially handling the exception, so the container provides the error page with the exception object--if the error page contains the following page directive:

<%@ page isErrorPage="true" %>


[ October 06, 2006: Message edited by: sven studde ]
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're talking about the JSP page.
My question: how to transfer it from servlet to this page?
Just doing throw exception would be enough?
But doPost can only throw ServletException and IOException only.
How can I just throw any other exception that can be caught by JSP?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JSP should not be catching the exception, and your error handler servlet should throw no exception.

The exception has already been caught by the container when the error handler is called. As I said before, all the information regarding the error is, at that point, available as scoped variables on the request which are available to the error handling servlet or the JSP page that that servlet forwards to in order to report the error.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic