There are two ways to define Error page in Java web application written using Servlet and JSP. First way is page wise error page which is defined on each jsp page and if there is any unhanded exception thrown from that page, corresponding error page will be displayed. Second approach is an application wide general or default error page which is shown if any Exception is thrown from any Servlet or JSP and there is no page specific error page defined.
In this tutorial we will see both approach to declare error page in JSP and when should we use page specific error page and when should we choose generate default application wide error page in Java web application.
Introduction to HTTP standard error codes
This section focus on the existing http errors we can face, each error type is associated with a server returned http error code :
- class 1 return codes (1xx) : Information
- class 2 return codes (2xx) : Success response, the request has been correctly executed -> expected answer from server (http code 200).
- class 3 return codes (3xx) : Redirection response. The resource has moved and is not any more at this URL.
Here are the errors we will be interested in.
- class 4 return codes (4xx) : Error on client side. Probably most known of all is error 404 : Not Found
- class 5 return codes (5xx) : Error on client side. Something went wrong on the server in that case it can data access issue, bugs, etc… but it is developers responsibility and in that case displaying something which tells the user “his action has been cancelled because of a technical problem” is mandatory else the user can wait for while before he receives his confirmation…
Page Specific Error page in JSP
Every JSP page has an attribute called "errorpage" on page directive, by using this attribute you can define an error page for any particular JSP. After that if any unhandled Exception thrown from that JSP , this error page will be invoked. In order to make any JSP page as an error page you need to use "isErrorPage" attribute of page directive and mark it true.
Listing1: Defining the error.jsp page
//error.jsp <%@ page isErrorPage="true"%> //login.jsp <%@ page errorPage="error.jsp"%>
This is preferred way of showing error messages in Java web application if you have custom error messages based on JSP and it also supersede any application wide error page defined in web.xml.
Error page in Java Web Application JSP Servlet
Application wide Error page in Java web application
There is another way to define error pages in java web application written using servlet and JSP. This is called application wide error page or default/general error page because it’s applicable to whole web application instead of any particular servlet or JSP. It is a recommended practice for every Java web application to have a default error page in addition of page specific error pages. This error page is defined in web.xml by using tag <error-page>. <error-page> allows you to define custom error message based upon HTTP error code or any Java Exception. You can define a default error message for all exception by specifying <exception-type> as java.lang.Throwable and it would be applicable to all exception thrown form any Servlet or JSP from web application.
Here is an example of declaring default error page in Java web application based on HTTP Errorcode and Java Exception type.
Listing2: Default Error page based on Exception type:A
<error-page> <exception-type>java.lang.Throwable</exception-type> <location>/error.htm</location> </error-page>
Listing3: Default Error page based on HTTP Error code:
<error-page> <error-code>500</error-code> <location>/internal-server-error.htm</location> </error-page> <error-page> <error-code>404</error-code> <location>/page-not-found-error.htm</location> </error-page>
This is how we define custom error page in Java application both page specific and an application wide default error page. Important point to note is that page specific error pages takes precedence over application wide default.
Create your custom error pages
First of all you have to design the pages you want to display to user in case of error: let’s say 2 pages, 1 for 4xx errors and another for 5xx errors (however another for 3xx can be useful). You can use whatever you want (html, jsp, jsf, etc…) but be aware that if something went wrong on server side maybe your Tomcat server is having issues and therefore you shouldn’t be charging it with jsp pages or something else. That is why we always recommend building error pages using HTML.
Now we have:
- 400.html -> telling the user did something wrong
- 500.html -> telling the server did something wrong
Add it to your deployment descriptor (web.xml)
Now the only thing we have to do is to say our J2EE server we want to use these pages in case of errors. Since only integers are allowed for error-code we cannot use patterns, therefore we list all of them.
Listing4: Using the pages in case of errors
<error-page> <error-code>400</error-code> <location>/404.html</location> </error-page> <error-page> <error-code>401</error-code> <location>/404.html</location> </error-page> ... <error-page> <error-code>500</error-code> <location>/500.html</location> </error-page> <error-page> <error-code>501</error-code> <location>/500.html</location> </error-page>
That’s all on the tutorial to create some nice and attractive error pages to your customers. Hope you had a nice time learning this.