You are on page 1of 16

Error and exception handling in JSP:

 We already know about error and exception.


 Errors can occur in a JSP in two different phases of its life. 
 Translation time errors
 JSP Request Time Error
Translation Time Error
• This type of Error occurs during the initial request. When a JSP page is
first requested and goes through the initial translation from
a JSP source file into a corresponding Servlet class file.
• These errors are usually the result of compilation failures and are
known as Translation Time Errors.
• They are reported to the requesting Client with an Error Status Code
500 or Server Error and usually contain the reported compilation
error. 
• Translation Time Errors are handled by the JSP Engine. 
JSP Request Time Error
•  This type of Error occurs during subsequent requests. These Errors
are Run-Time errors that can occur in either the body of the JSP page
or in some other object that is called from the body of the JSP Page. 
• Request Time Errors result in an Exception being thrown.
• These Exceptions can be caught and appropriately handled in the
body of the calling JSP, which would be the end of the error.
• Those Exceptions that are not caught result in the forwarding of
the Client request, including the Uncaught Exception, to the Error
Page specified by the offending JSP.
• Exception can be handled in JSP in three ways:
a) Java Exception Handling mechanism
b) Dealing with exception with page directive
c) Dealing with exception in Deployment Descriptor.
JSP Exception handling with try catch:

<!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>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-
1"%>
<html>
  <head>
   <title> Calculator.jsp </title>
  </head>
   
  <body>
  <% try
     {
        int i1 = Integer.parseInt(request.getParameter("n1"));
        int i2 = Integer.parseInt(request.getParameter("n2"));
        int add = i1 + i2;
        out.print("Addition = "+add);
     }
     catch(NumberFormatException ne)
     {
        out.print("Esception : "+ne);  
     } 
  %>
    
  </body>
</html>
• Input the integer value in text fields and click ADD button.
• The browser display the below message,
• Addition of 6+5=11
• Now, input the float value in any of the text field and click ADD button
so the browser display the message.
• Esception : java.lang.NumberFormatException: For input string: "6.3"
Jsp exception handling with page directive:
The two attributes of page directive errorPage and isErrorPage are used to deal
with exception.

DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<!

<html>
  <head>
    <title>InputData.html</title>
  </head>
  <body>
    <form action="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 of 6+5=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"
Exception handling in jsp with deployment descriptor:

<!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.

You might also like