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.
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!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=UTF-8">
- <title>JSP main Page</title>
- </head>
- <body>
- <div id="Login" style="background-color: green">
- <h1>User Login</h1>
-
- <form action="User.jsp" method="post">
- <strong>Username</strong>:<input type="text" name="name"><br><br>
- <strong>Password</strong>:<input type="password" name="password"><br><br><br>
- <input type="submit" name="Login">
- </form>
- </div>
- <div id="Calculation" style="background-color: greenyellow">
- <h1>Computation</h1>
-
- <form action="User1.jsp" method="post">
- <strong>Enter no.1:</strong><input type="text" name="number1"><br><br>
- <strong>Enter no.2:</strong><input type="text" name="number2"><br><br><br>
- <input type="submit" name="Divide">
- </form>
- </div>
- </body>
- </html>
Output
User.jsp for the user login:
- <%@page contentType="text/html" pageEncoding="UTF-8" errorPage="error.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=UTF-8">
- <title>User Home Page</title>
- </head>
- <body>
- <%
- String user = request.getParameter("id");
- String pwd = request.getParameter("password");
-
- if(user == null || "".equals(user) || pwd == null || "".equals(pwd)){
- throw new ServletException("Parameters are missing");
- }
-
- %>
- Hi <%=user %>
- </body>
- </html>
Error.jsp for errors related to login:
- <%@page contentType="text/html" pageEncoding="UTF-8" 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=UTF-8">
- <title>Error Page</title>
- </head>
- <body>
- <h1>Parameters are missing..!!</h1>
- Please go to <a href="index.jsp">home page</a>
- </body>
- </html>
Output for login:
Now User1.jsp for computation:
- <%@page contentType="text/html" pageEncoding="UTF-8" errorPage="error1.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=UTF-8">
- <title>User Home Page</title>
- </head>
- <body>
- <%
- String num1=request.getParameter("number1");
- String num2=request.getParameter("number2");
-
- int a=Integer.parseInt(num1);
- int b=Integer.parseInt(num2);
- int c=a/b;
- out.print("division of numbers is: "+c);
- %>
- </body>
- </html>
Error1.jsp related to computation:
- <%@page contentType="text/html" pageEncoding="UTF-8" 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=UTF-8">
- <title>Error Page</title>
- </head>
- <body>
- <h3>Sorry an Exception Occured..!!</h3>
- Exception is:<%=exception%>
- </body>
- </html>
Now watch.
When we submit the query then the following output will be the result.
Output for computation:
Again watch.
After submitting the query:
Output