Wednesday, January 25, 2012

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

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.

how do make error page in servlet jsp web application javaIn this java 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. This is in continuation of my earlier post on Servlet and JSP like top 10 Servlet interview questions and top 10 JSP interview questions.

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. For example in below JSP pages error.jsp is an error page which can be used to display custom error messages if any unhandled exception is thrown from login.jsp (because it is defined as errorpage for login.jsp)

//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 its applicable to 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 addtion 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 Error code and Java Exception type.


Default Error page based on Exception type:

<error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/error.htm</location>
</error-page>


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>

That's all on how to 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 error page defined in web.xml.

Other Java post you may like

4 comments :

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.

emi_me said...

hey look up this wesite for programmers...www.countcode.com, i worked by myself for 5 months to make it run...you 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

Post a Comment