<%@ page language="java" %> <html> <body> <% try { int x = Integer.parseInt(request.getParameter("x")) ; %> <h2>You pass the number: <%=x%></h2> <% } catch (NumberFormatException ex) { %> <h2>Error: x must be a number</h2> <% } %> </body> </html>The above code snippet tries to parse a number from a parameter which is passed from URL query string. If the parameter is not a number (indicated by the NumberFormatException is thrown), shows an error message.Following is a more meaningful example which is a JSP page that calculates sum of two numeric parameters passed from query string:
<%@ page language="java" contentType="text/html; charset=UTF-8" 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>Sum calculation</title> </head> <body> <%! int x; int y; %> <% try { x = Integer.parseInt(request.getParameter("x")) ; } catch (NumberFormatException ex) { %> <h2>Error: x must be a number</h2> <% return; } try { y = Integer.parseInt(request.getParameter("y")) ; } catch (NumberFormatException ex) { %> <h2>Error: y must be a number</h2> <% return; } %> <h1> Sum of <%=x%> and <%=y%> is <%=(x + y) %> </h1> </body> </html>If the two parameters are numeric, the page displays sum of them:If either a number is not numeric, an appropriate error message is displayed:
Let’s see an example. Consider the following JSP page:
<%@ page language="java" contentType="text/html; charset=UTF-8" errorPage="error_jstl.jsp" 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>Calculate division of two numbers</title> </head> <body> <% int x = Integer.parseInt(request.getParameter("x")); int y = Integer.parseInt(request.getParameter("y")); int div = x / y; %> <h2>Division of <%=x%> and <%=y%> is <%=div%></h2> </body> </html>
This page calculates division of two numbers passed from parameters query string. There are two possible exceptions here:
<%@ page language="java" contentType="text/html; charset=UTF-8" isErrorPage="true" 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>Error</title> </head> <body> <h2> Error: <%=exception.getClass().getName() %><br/> <%=exception.getMessage() %><br/> </h2> </body> </html>Here are some outputs when running this example:
Error: ${pageContext.exception}To print the exception stack traces using JSTL:
<c:forEach var="trace" items="${pageContext.exception.stackTrace}"> ${trace}<br/> </c:forEach>Here is a better version of the error.jsp page using JSTL:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page language="java" contentType="text/html; charset=UTF-8" isErrorPage="true" 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>Error</title> </head> <body> <h2> Error: ${pageContext.exception} <br/> </h2> Exception stack trace:<br/> <c:forEach var="trace" items="${pageContext.exception.stackTrace}"> ${trace}<br/> </c:forEach> </body> </html>Output:Benefits:
<error-page> <exception-type>fully_qualified_name_of_exception_class</exception-type> <location>path_to_error_jsp_page</location> </error-page>For example:
<error-page> <exception-type>java.sql.SQLException</exception-type> <location>/dbError.jsp</location> </error-page>That will tell the server to redirect the clients to the dbError.jsp page whenever any JSP/Servlet throws an exception of type java.sql.SQLException. The dbError.jsp page does not have to declare the attribute isErrorPage.Using this approach we can also specify a custom error handling page for specific HTTP error code like 404, 500… The syntax is:
<error-page> <error-code>HTTP_error_code</error-code> <location>path_to_error_jsp_page</location> </error-page>For example:
<error-page> <error-code>404</error-code> <location>/404error.jsp</location> </error-page>That will tell the server redirects all requests causing HTTP 404 error to the custom page 404error.jsp, instead of showing the default 404 error. Following is example code of the 404error.jsp page:
<%@ page language="java" contentType="text/html; charset=UTF-8" 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>404 Error</title> </head> <body> <center> <h2> Apologize, we could not find the page you were looking for: </h2> ${pageContext.errorData.requestURI} </center> </body> </html>The errorData object provides detailed information about the error such as the request URI, status code, servlet name, and the Throwable object that caused the error:Here is the output when trying to access non-exist URL on the server:Benefit: