Reputation: 11112
I am trying to handle exception using JSP errorpage configuration. However it fails. Please point out where I am doing wrong. This is not for any real time app, I was trying out a scenario.
The initial jsp page:
page1.jsp
<body>
<form action="process.do" method="post">
<input type="Submit" />
</form>
</body>
web.xml
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.examples.example.Servlet1</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/errorpage.jsp</location>
</error-page>
<error-page>
<exception-type>java.sql.SQLException</exception-type>
<location>/errorpage.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorpage.jsp</location>
</error-page>
Servlet1.java
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String s = null;
if (s.equals("")) {
}
} catch (Exception ex) {
throw new ServletException("my custom exception message");
}
}
errorpage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage='true'%>
<html>
<head>
<title>Error Page</title>
</head>
<body>
<%
out.print("Error Message : ");
out.print(exception.getMessage());
%>
</body>
</html>
Question - I expect this error page to be displayed. But instead, I get a HTTP 500 error, and this is what i see in console logs:
SEVERE: Servlet.service() for servlet [servlet1] in context with path [/simpleServlet] threw exception [my custom exception message] with root cause
javax.servlet.ServletException: my custom exception message
at com.examples.example.Servlet1.doPost(Servlet1.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
Upvotes: 2
Views: 4314
Reputation: 2154
This is what I found on one of the comments on the blogs link below
There is a severe issue with using "errorpage" "isErrorpage" and declaring error page in web.xml file, it doesn't display properly in Internet Explorer at least version 6 and 7, instead it shows it own IE error page for page 404 and internal server error 500. better approach is using Spring exception handling and defining default error view for Spring's Exception handler. in this way you will get consistent behavior over all browser because error page will not be treated as error page instead it will be treated as another jsp page.I have spent lot of time to figure out this bug with IE6, you may save your precious time. Also there is another theory that IE and chrome will display its own error page if size of error page will be less than 512 bytes. I have tested this but in my case even if size was more than 512 bytes it was still showing "Internal server error-500" Only solution which worked was Using Spring Exception handling
Read more: http://javarevisited.blogspot.com/2012/01/error-page-in-java-web-application.html#ixzz2xZ35ziPQ
Upvotes: 3