File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes redirect to error page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "redirect to error page" Watch "redirect to error page" New topic

redirect to error page

Travis Williams

Joined: Jan 28, 2002
Posts: 10
Hi, I know that in a JSP page you can just do:
<% page errorPage="errorPage.jsp" %>
But is there any way for me to map to this error page when an unforeseen error occurs in one of my servlets/class files instead of the server's "500 servlet exception page?"
Thanks for any help
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
I am not sure..But try this and more probably it will work..
In Servlet or any other class, catch possible exception and redirect response to the error page that u desired..
Travis Williams

Joined: Jan 28, 2002
Posts: 10
Thanks for your response,
I'll work on that . . . I was hoping there might be some kind of special way to catch-all errors in servlets without having to explicitly write my own, but I haven't run across it in the books I've read.
Yuqing Zhu

Joined: Jan 17, 2002
Posts: 8
Hi Travis,
<error-page> may be able to solve your issue. as mention in SRV.9.9.2
If a servlet generates an error that is not handled by the error page mechanism as described above, the container must ensure the status code of the response is set
to status code 500.

But my experience has some difficulties, see my recent post subject "error-page"
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

There is a way to make your servlet use your JSP errorPages, as opposed to the container's generic "500 error" page.

And well, right now I'm not at work, so I forget how it was accomplished.

I'll look up the code tomorrow and post.
[ January 29, 2002: Message edited by: Mike Curwen ]
Napa Sreedhar
Ranch Hand

Joined: Jan 29, 2002
Posts: 62
In the calling JSP page use..
<%page errorPage="ErrorPage.jsp"
import ...
if (error)
throw new Exception("message to be displayed")
and in ErrorPage.jsp include the statements
<%@ page isErrorPage="true"%>
<%@ page import* %>
<p> <%=exception.getMessage()%>
Hope this would help
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Napa, that wasn't quite what was asked.

From a servlet, you can redirect to a JSP error page, but there is one thing you need to set first.

Here is the code I developed for doing this in my servlets:
hanumanth reddy
Ranch Hand

Joined: Jun 12, 2000
Posts: 118
Try to place this following code in the error page it iwll dispaly the relevant msg

<%@ page isErrorPage="true" %>
<%-- work out the error page location --%>

String errorPageLocation="eg.html";
if(exception instanceof NullPointerException)
else if(exception instanceof IllegalStateException)
String msg=exception.getMessage();
System.out.println("error.jsp:: the message is: "+msg);
document.location='<%= errorPageLocation %>'


<a href="" target="_blank" rel="nofollow"></a><br /> 'Add Job To Life'
I agree. Here's the link:
subject: redirect to error page
It's not a secret anymore!