Share This Topic -

Exception handling in jsp with deployment descriptor:

InputData.html :

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <form action="../JSPFILE/Calculator.jsp">
    <input type="text" name="n1"> <br>
    <input type="text" name="n2"> <br>
    <input type="submit" value="ADD">

Calculator.jsp :

 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
   <title> Calculator.jsp </title>
  		int i1 = Integer.parseInt(request.getParameter("n1"));
  	 	int i2 = Integer.parseInt(request.getParameter("n2"));
  	 	int add = i1 + i2;
  	 	out.print("Addition = "+add);

Error.jsp : 

 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1" isErrorPage="true"%>
   Your page generate an Exception. <br>
   <%= exception.getMessage() %>

Web.xml :

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 

NOTE : web.xml (deployment descriptor) file is available in WEB-INF folder at WebRoot.


Input the integer value in textfields and click ADD button.

The browser display the below message,

Addition = 11

Now, input the float value in any of the textfield and click ADD button so the browser display the message,

Your page generates an Exception.
For input string: "6.3"

This deployment descriptor entry means that whenever a web component throws a NumberFormatException from any web page in the whole application(web project), the web container call the Error.jsp file, which simply reports the error message in web browser.