Java2all.com  




Share This Topic -

Exception handling in jsp with deployment descriptor:

InputData.html :

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

Calculator.jsp :

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

Error.jsp : 

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

Web.xml :

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <error-page>
  	<exception-type>java.lang.NumberFormatException</exception-type>
  	<location>/Error.jsp</location>
  </error-page>
</web-app> 

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.