| Author |
Forwarding exception from servlet to JSP
|
Surendra Kumar
Ranch Hand
Joined: Jul 04, 2006
Posts: 87
|
|
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?
|
 |
sven studde
Ranch Hand
Joined: Sep 26, 2006
Posts: 148
|
|
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 ]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56187
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Surendra Kumar
Ranch Hand
Joined: Jul 04, 2006
Posts: 87
|
|
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
Joined: Sep 26, 2006
Posts: 148
|
|
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
Joined: Jul 04, 2006
Posts: 87
|
|
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
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56187
|
|
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.
|
 |
 |
|
|
subject: Forwarding exception from servlet to JSP
|
|
|