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)