Exception Handling in JSP Along With Error Pages

Exception handling

Exceptions are objects present in the program that are thrown at runtime. Exception handling is the process to handle those exceptions that may turn into runtime errors. They may occur at any time in your web application and might irritate the user. So it's better to be safe by removing those errors and provide your web application with user-friendlier error response pages.

Performing exception handling

The following are two ways to perform exception handling:

  • By errorPage and isErrorPage attirbutes of the page directive.
  • By an <error-page> tag in the deployment descriptor present in the web.xml file.

JSP Error page configuration

To create an JSP error page, we need to set the page directive attribute isErrorPage value to true, then we can access the exception implicit object and can use it to send the error alert to the user. We also need to set the page directive attribute errorPage to define JSP that will handle all the types of exceptions thrown by the JSP service method.

JSP error page deployment descriptor configuration

If you notice that most frequently we come across a common error page that we want to use for all the JSPs, so instead of doing that for all the pages individually, we can define an error page in web.xml with an <error-page> element. By configuring the error pages the exceptions like 404 codes can be handled.

Now let's check this by taking the example.

Example of exception handling in JSP by the elements of a page directive

We are creating here five JSP files for explaining the concept.

Let's start with an index.jsp that will be our main page.

  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  3.    "http://www.w3.org/TR/html4/loose.dtd">  
  4.   
  5. <html>  
  6.     <head>  
  7.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8.         <title>JSP main Page</title>  
  9.     </head>  
  10.     <body>  
  11.         <div id="Login" style="background-color: green">  
  12.         <h1>User Login</h1>  
  13.           
  14.         <form action="User.jsp" method="post">  
  15.             <strong>Username</strong>:<input type="text" name="name"><br><br>  
  16.             <strong>Password</strong>:<input type="password" name="password"><br><br><br>  
  17.             <input type="submit" name="Login">  
  18.         </form>  
  19.         </div>  
  20.         <div id="Calculation" style="background-color: greenyellow">  
  21.             <h1>Computation</h1>  
  22.               
  23.             <form action="User1.jsp" method="post">  
  24.                 <strong>Enter no.1:</strong><input type="text" name="number1"><br><br>  
  25.                 <strong>Enter no.2:</strong><input type="text" name="number2"><br><br><br>  
  26.                 <input type="submit" name="Divide">  
  27.             </form>  
  28.             </div>  
  29.     </body>  
  30. </html> 

Output

Output
User.jsp for the user login:
  1. <%@page contentType="text/html" pageEncoding="UTF-8" errorPage="error.jsp"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  3.    "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5.     <head>  
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7.         <title>User Home Page</title>  
  8.     </head>  
  9.     <body>  
  10.         <%  
  11.     String user = request.getParameter("id");  
  12.     String pwd = request.getParameter("password");  
  13.   
  14.     if(user == null || "".equals(user) || pwd == null || "".equals(pwd)){  
  15.         throw new ServletException("Parameters are missing");  
  16.     }  
  17.   
  18. %>  
  19. Hi <%=user %>  
  20.     </body>  
  21. </html> 

Error.jsp for errors related to login:

  1. <%@page contentType="text/html" pageEncoding="UTF-8" isErrorPage="true"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  3.    "http://www.w3.org/TR/html4/loose.dtd">  
  4.   
  5. <html>  
  6.     <head>  
  7.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8.         <title>Error Page</title>  
  9.     </head>  
  10.     <body>  
  11.         <h1>Parameters are missing..!!</h1>  
  12. Please go to <a href="index.jsp">home page</a>  
  13.     </body>  
  14. </html> 

Output for login:

Output for login
Now User1.jsp for computation:

  1. <%@page contentType="text/html" pageEncoding="UTF-8" errorPage="error1.jsp"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  3.    "http://www.w3.org/TR/html4/loose.dtd">   
  4. <html>  
  5.     <head>  
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7.         <title>User Home Page</title>  
  8.     </head>  
  9.     <body>  
  10.         <%   
  11. String num1=request.getParameter("number1");  
  12. String num2=request.getParameter("number2");  
  13.   
  14. int a=Integer.parseInt(num1);  
  15. int b=Integer.parseInt(num2);  
  16. int c=a/b;  
  17. out.print("division of numbers is: "+c);  
  18. %>  
  19.     </body>  
  20. </html> 

Error1.jsp related to computation:

  1. <%@page contentType="text/html" pageEncoding="UTF-8" isErrorPage="true"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  3.    "http://www.w3.org/TR/html4/loose.dtd">  
  4.   
  5. <html>  
  6.     <head>  
  7.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8.         <title>Error Page</title>  
  9.     </head>  
  10.     <body>  
  11.         <h3>Sorry an Exception Occured..!!</h3>  
  12. Exception is:<%=exception%>  
  13.     </body>  
  14. </html> 
Now watch.
watch 
When we submit the query then the following output will be the result.

Output for computation:

Output for computation
 Again watch.
Again watch
After submitting the query:

Output
 
submitting the query 

Up Next
    Ebook Download
    View all
    Learn
    View all