logo

Exception Handling In JSP


Show

An exception is generally an object that's thrown at runtime. Exception Handling is that the process to manage runtime errors. There might take place exceptions at any time in your web application. So managing exceptions may be a safer point for the web developer. Perform exception handling has two ways in JSP:

  • By errorPage and isErrorPage features of page directive
  • By component in web.xml file

Example of exception handling in JSP by the components of page directive

In this case, you want to explain and make a page to manage the exceptions, as within the error.jsp page. The pages where an exception might occur, explain the errorPage feature of page directive, as within the process.jsp page.

There are 3 files:

  • index.jsp for input values
  • process.jsp for dividing the 2 numbers and displaying the result
  • error.jsp for handling the exception

Index.jsp

<form action="process.jsp">  
No1:<input type="text" name="n1" /><br/><br/>  
No1:<input type="text" name="n2" /><br/><br/>  
<input type="submit" value="divide"/>  
</form>

process.jsp

<%@ page errorPage="error.jsp" %>  
<%  
  
String num1=request.getParameter("n1");  
String num2=request.getParameter("n2");  
  
int a=Integer.parseInt(num1);  
int b=Integer.parseInt(num2);  
int c=a/b;  
out.print("division of numbers is: "+c);    

%>  

Error.jsp

<%@ page isErrorPage="true" %>   

<h3>Sorry an exception occured!</h3>  
  
Exception is: <%= exception %> 

download this example

Output of the Above-Given example

a.lang.Exception within the exception-type element. let's examine the straightforward example:

Example of Exception Handling in jsp by specifying the error-page element in web.xml file

This approach is best because you do not have to specify the errorPage attribute in each jsp page. Specifying the only entry within the web.xml file will handle the exception. during this case,

  • web.xml file for defining the error-page component

index.jsp for input values either specify exception-type or error-code with the situation element. If you would like to handle all the exceptions, you'll need to specify the jav

There are 4 files:

  • process.jsp for splitting the two numbers and showing the result
  • error.jsp for showing the exception

web.xml file if you want to manage any exception

<web-app>  

 <error-page>  
  <exception-type>java.lang.Exception</exception-type>  
  <location>/error.jsp</location>  
  </error-page>  `
   
</web-app> 

This detain is good if you need to manage any exceptions. If you know any specific error code and you want to manage that exception, define the error-code component rather than exception-type as given below:

1) web.xml file if you want to handle the exception for a specific error code

<web-app>  

 <error-page>  
  <error-code>500</error-code>  
  <location>/error.jsp</location>  
  </error-page>   

</web-app>

2) index.jsp file is the same as in the above example

3)process.jsp

Now, you don't have to define the errorPage feature of the page directive in the jsp page.

<%@ page errorPage="error.jsp" %>  
<%  

String num1=request.getParameter("n1");  
String num2=request.getParameter("n2");  
  
int a=Integer.parseInt(num1);  
int b=Integer.parseInt(num2);  
int c=a/b;  
out.print("division of numbers is: "+c);   

%>

4) error.jsp file is the same as in the above example

download this example´╗┐