Exception handling in JSP is done by JSP exception pages.
Sometime back I wrote a post about Servlet Exception Handling and why do we need it. Same explanation is also applicable for JSP pages also and that’s why Java EE provides a clear approach for exception handling in JSP using JSP error pages. To handle exceptions thrown by the JSP page, all we need is an error page and define the error page in JSP using jsp page directive.
To create a JSP error page, we need to set page directive attribute isErrorPage value to true, then we can access exception jsp implicit object in the JSP and use it to send customized error message to the client.
We need to set page directive errorPage attribute to define the JSP that will handle any exception thrown by the JSP service method. When JSP Error page is translated to servlet code, it extends org.apache.jasper.runtime.HttpJspBase
in Tomcat.
Most of the times, we have a common error page that we want to use for all the JSPs, so rather than configuring it in all the JSPs individually, we can define error page in web.xml with error-page element. We can configure JSP error page to handle other error codes like 404 also. Let’s see how all these fit together in a web application. We will create a simple web application JSPExceptionHandling whose project structure will look like below image. The entry point of the application is index.jsp
whose code is given below.
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<form action="login.jsp" method="post">
<strong>User ID</strong>:<input type="text" name="id"><br>
<strong>Password</strong>:<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
When we submit the form, request will be sent to login.jsp
, code is like below.
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII" errorPage="error.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<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("Mandatory Parameter missing");
}
%>
<%-- do some DB processing, not doing anything for simplicity --%>
Hi <%=user %>
</body>
</html>
Notice that if input parameters are null or empty, its throwing ServletException
with a proper message and errorPage
is defined as error.jsp
whose code is like below.
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Error Page</title>
</head>
<body>
<% if(response.getStatus() == 500){ %>
<font color="red">Error: <%=exception.getMessage() %></font><br>
<%-- include login page --%>
<%@ include file="index.jsp"%>
<%}else {%>
Hi There, error code is <%=response.getStatus() %><br>
Please go to <a href="/index.jsp">home page</a>
<%} %>
</body>
</html>
Notice the isErrorPage
page directive attribute value is true
. When application resources throw exceptions, the error code is 500, the code is written to handle both application level exceptions and errors such as 404 - page not found. Also notice the use of include directive to present user with login page incase of any exception. Here is the web.xml where we are defining the error page for the application.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>JSPExceptionHandling</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
</web-app>
Now when we run above application, we get following pages as response. Login Page JSP Error Page for Exception JSP Error Page for 404 Error Code Thats all for exception handling in JSP pages, its very easy to implement and we should use it to make sure we handle all the exceptions and error codes and send a useful response to client rather than container default error page.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
HI Pankaj, Hope you are doing fine. !! I work on web application built on Struts 1.3 and have requirement to disable to flash player from login page. Will you be able to help? Whenever someone login to application he/she is seeing the message “Click to enable Adobe Flash player”. I just want to disable this message from login page. Can you help ?
- Milin Modi
thank u so much sir for help :)
- yukti singh
Good exaplanation. Why Interrnet explorer not support for display error messages handling in jsp using iserrropage & errorpage. same app works with others browsers.
- Siddu