Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Monday, July 26, 2021

How to define Error page in Java Web Application - Servlet JSP Example

There are two ways to define the Error page in Java web application written using Servlet and JSP. The first way is page wise error page which is defined on each jsp page and if there is any unhandled exception thrown from that page, the corresponding error page will be displayed. The 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.

how do make error page in servlet jsp web application javaIn this java tutorial we will see both approaches to declare error page in JSP and when should we use page specific error page and when should we choose to generate a default application-wide error page in Java web application. 

This is in continuation of my earlier post on Servlet and JSP like the top 10 Servlet interview questions and the top 10 JSP interview questions. If you have' read them yet, you can also read them to learn about essential Servlet and JSP concepts. 

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 is 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 the page directive and mark it true. 

For example, in the below JSP pages, error.jsp is an error page that can be used to display custom error messages if any unhandled exception is thrown from login.jsp (because it is defined as error page for login.jsp)

<%@ page isErrorPage="true"%>

<%@ page errorPage="error.jsp"%>

This is a preferred way of showing error messages in Java web applications if you have custom error messages based on JSP and it also supersedes any application-wide error page defined in web.xml. You can also join these Servlet and JSP courses to learn more about page directives in JSP. 

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 applications written using servlet and JSP. This is called an application-wide error page or default/general error page because it's applicable to the whole web application instead of any particular servlet or JSP. Its recommended practice for every Java web application to have a default error page in addition to 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 messages based upon HTTP error code or any Java Exception. you can define a default error message for all exceptions by specifying <exception-type> as java.lang.Throwable and it would be applicable to all exceptions are thrown from any Servlet or JSP from web application. here is an example of declaring default error page in Java web application based on HTTP Error code and Java Exception type.

Default Error page based on Exception type:


Default Error page based on HTTP Error code:


That's all on how to define the custom error page in Java application both page-specific and an application-wide default error page. An important point to note is that page-specific error pages take precedence over application-wide default error pages defined in web.xml.

Other Java posts you may like


Heidarzadeh said...

Hi Javin
Please let us to see hole content for each post in google reader. like before!
In my country blogspot is filtered and unfortunately I can not see the hole content of each post in google reader.
Thanks in advance.

Anonymous said...

There is a severe issue with using "errorpage" "isErrorpage" and declaring error page in web.xml file, it doesn't display properly in Internet Explorer at least version 6 and 7, instead it shows it own IE error page for page 404 and internal server error 500. better approach is using Spring exception handling and defining default error view for Spring's Exception handler. in this way you will get consistent behavior over all browser because error page will not be treated as error page instead it will be treated as another jsp page.I have spent lot of time to figure out this bug with IE6, you may save your precious time. Also there is another theory that IE and chrome will display its own error page if size of error page will be less than 512 bytes. I have tested this but in my case even if size was more than 512 bytes it was still showing "Internal server error-500" Only solution which worked was Using Spring Exception handling.

Anonymous said...

I think best way to handle page not found error (Error code 404) and Internal Server Error(Error code 500) is to use Spring framework. Spring MVC provides Exception handling mechanism which can be setup in dispatcher-servlet.xml file and it can have different error pages for different Exception type and can have a default one as well but I am not sure if supports error code as well.

Emime said...

hey look up this wesite for, i worked by myself for 5 months to make it can share and download codes, ask or answer forum questions, and you can count your code lines from your whole life of programming, sincerely Emi

for ict 99 said...


Post a Comment