How to create and use custom error page in jsp

This is detailed java code how to create and use custom
error page in
jsp and display an error message.
Before run this java code create a new directory named
"user" in the tomcat-6.0.16/webapps and paste WEB-INF directory in
same directory.
error_page.jsp
<!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>
<font color="red"><%= exception.toString() %></font>
</body>
</html>
|
Save this code as a .jsp file named "error_page.jsp"
in the directory Tomcat-6.0.16/webapps/user/
calculation_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>
|
Save this code as a .jsp file named "calculation_page.jsp"
in the directory Tomcat-6.0.16/webapps/user/ and run this page with the
following url in address bas in browser http://localhost:8080/user/calculation_page.jsp
When run this jsp page in browser, mathematical
calculation (devide by 0) will generate an error so the error page will be called on
that time and result will be the custom error page.

Download source code

|