Java2all.com  

By page directive





Share This Topic -

Jsp exception handling with page directive:

The two attributes of page directive errorPage and isErrorPage are used to deal with exception.

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" errorPage="Error.jsp"%>
<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> 

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 generate an Exception.
For input string: "6.3"