JSP Exception Handling
I have 2 simple JSP to check navigation to error page on occurrence of exceptions:
calculation_page.jsp -having the tag - <%@ page errorPage="error_page.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%-- include custom error page in this jsp code --%>
<%@ page errorPage="error_page.jsp" %>
<html>
<head>
<title>calculation page</title>
</head>
<body>
<%
int i = 10;
// This line will create an error so error page will be called
i = i / 0;
%>
</body>
</html>
error_page.jsp - error page having the tag - <%@ page isErrorPage="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page isErrorPage = "true"%>
<%@ page import = "java.io.*" %>
<HTML>
<HEAD>
<TITLE> custom error page </TITLE>
</head>
<body>
<h2>Your application has generated an error</h2>
<h3>Please check for the error given below</h3>
<b>Exception:</b><br&g开发者_如何学JAVAt;
<font color="red"><%= exception.toString() %></font>
</body>
</html>
But when an exception occurs, instead of going from 1st page to the 2nd one, it gives an error – “HTTP 500 Internal Server Error” and the browser says: “The website cannot display the page”.
Kindly help.
your error page looks OK but your errorGenerator page should look like :
<%@page errorPage="/errorPage.jsp" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int i = 10;
i = i / 0;
%>
</body>
</html>
Note:errorPage="/errorPage.jsp"
in page directive
The errorPage attribute of a page directive specifies an alternate page to use as an error page,
Guys, just came across this link: J2EE:error in displaying ERROR page.
It says IE needs the error page to atleast 512kb in size. I tried the page in firefox it is working perfectly fine.
Then, increased the size of the error page including images, now it works fine in IE 8 with the page directive tag as well as with global xml tags.
Dont know why, but its a weird thing. Both IE 7 and 8 have this issue. 512 kb is a pretty big size for an error page.
It's best if you declare the error page in your web.xml
:
<error-page>
<error-code>500</error-code>
<location>error_page.jsp</location>
</error-page>
@org.life.java Thanks for the reply. I ran your code without any change to web.xml i.e. without adding a error page tag, it is still giving the same HTTP 500 error. I think the problem might be with some tomcat configuration. Not sure though. I am using Tomcat v 5.5.
@mvg Thanks for the reply. May be then there is some other configuration issue.
@cherouvim Thanks for the reply. I declared the error page in web.xml as shown by you and removed the page attribute from the jsp. It gives me the same HTTP 500 error. Even tried with exception-type tag in xml, but it gave the same result. Here is my web.xml:
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> abc 12
<servlet>
<description></description>
<display-name>PortalServlet</display-name>
<servlet-name>PortalServlet</servlet-name>
<servlet-class>com.test.portal.batch.PortalServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>PortalServlet</servlet-name>
<url-pattern>/PortalServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>testDefault.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/error_page.jsp</location>
</error-page>
精彩评论