In this tutorial you will learn about how to handle Exceptions in JSP.
JSP errorPage Example
In this tutorial you will learn about how to handle Exceptions in JSP.
In this section I am trying to demonstrate you about handling the exception in JSP. Here I am assuming that you are aware from the Exception in Java if not click here. In JSP there may also an exception arises when a jsp page is executed. So to handle the exception a web server should have a mechanism to forward the control to an error page, a page directive attribute "errorPage" is used for this purpose. This page directive attribute should be used in the beginning of the JSP page and when you will use this page directive attribute a web server is specified that when there may an exception will occur control will be forwarded to the error page defined as top of the JSP page.
Example :
An example is being given below will demonstrate you about an errorPage/exception handling. In this example I have created a JSP page on which at the beginning of the page used the page directive errorPage="errorPage.jsp" and then designed a form where taken a text box to input a value and a submit button. In the next step first get the value of the parameter inputted in the text box and then checked for its null value. In the next step I have parsed the inputted string value to the integer then divided this number by zero, this process will throw an exception and the web server will forward the control to the error page. The page where the control is forwarded should have used the attribute isErrorPage="true".
ExceptionHandling.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page errorPage="errorPage.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Exception Handling</title> </head> <body> <form> <table> <tr> <td>Enter a number :</td> </tr> <tr> <td><input type="text" name="num"/></td> </tr> <tr> <td align="center"><input type="submit" value="submit"/> </tr> </table> </form> <% String num = request.getParameter("num"); if(num != null) { String number = num.trim(); int no = Integer.parseInt(number); out.println(no); int value= no/0; } %> </body> </html>
errorPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page isErrorPage="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>errorPage</title> </head> <body> <h3>Due to following reasons an error has occurred</h3> <ul> <li><%=exception.getClass() %></li> <li><%=exception.getMessage() %></li> </ul> </body> </html>
Output :
1. When you will execute the ExceptionHandling.jsp page you will get the output as follows :
2. When you will input a number and click on submit button you will get the output as :
[ 0 ] Comments