Pin It

How to create error page in a JSP and Servlet to handle exceptions ?


A simple Web Application that deals with handling of exceptions coming in one JSP and forwarding it to another JSP for handling the exception

1. welcome.jsp


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>A User Input Page !!!</title>
</head>
<body bgcolor="#ADB1A9">

 <h1>A Simple Division Application</h1>
 <form method="POST" action="throwError.jsp">
  Number 1 : <input type="text" id="num1" name="num1" /><br>
  Number 2 : <input type="text" id="num2" name="num2" /><br> <br>
  <input type="submit" value="Submit" />
 </form>

</body>
</html>

2. throwError.jsp


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>

<!-- To tell JSP Engine that if error occurs on this page than 
 forward it to mentioned page for handling of Error -->
<%@ page errorPage="error.jsp"%>
<!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=ISO-8859-1">
<title>Result !!!</title>
</head>
<body bgcolor="#ADB1A9">
 <h2>The result of division of num1 / num2 is :</h2>

 <%
  Integer num1 = Integer.parseInt(request.getParameter("num1"));
  Integer num2 = Integer.parseInt(request.getParameter("num2"));
 %>

 <!-- It will throw NumberFormatException if any of number 
 comes out to be String -->
 <p><%=num1 / num2%></p>

</body>
</html>


3. error.jsp


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 
<!-- To tell JSP Engine that this page is an Error page --> 
<%@ page isErrorPage="true"%>

<!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=ISO-8859-1">
<title>Error Page</title>
</head>
<body bgcolor="#ADB1A9">

 <h2>This is our first simple error page in jsp and servlet</h2>
 </br>

 <h3>Below is the stack trace of the error that has occured :</h3>
 </br>


 <p style="color: #FF0000;">
  Error Stack Trace :
  <%=exception.toString()%>
</body>
</html>

Running of the application - (Success)


































Running of the application - (Failure)























Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot