Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

redirect to error page

 
Travis Williams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
Rgds
Manohar
 
Travis Williams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
Regards,
Yuqing
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 java.io.* %>
<p> <%=exception.getMessage()%>
Hope this would help
Napa
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
{
errorPageLocation=request.getContextPath()+"somerelevant.html";
}
else if(exception instanceof IllegalStateException)
{
String msg=exception.getMessage();
System.out.println("error.jsp:: the message is: "+msg);
</head>
<body>
<script>
document.location='<%= errorPageLocation %>'
</script>

</body>
</html>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic