Tuesday, June 24, 2014

Servlet Custom error page not shown


The Servlet 2.2 specification allows you to specify an error page (a servlet or a JSP) for different kinds of HTTP errors or ServletExceptions. You can specify this in deployment descriptor of the web application as:

<error-page>
   <exception-type>FooException</exception-type>
   <location>/error.jsp</location>
 </error-page>

where FooException is a subclass of ServletException.
The web container invokes this servlet in case of errors, and you can access the following information from the request object of error servlet/JSP: error code, exception type, and a message. Refer to section 9.8 of Servlet 2.2 spec.

Here is the document:

 
<?xml version="1.0"?>

<!DOCTYPE web-app

   PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

   "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<error-page>

   <error-code>404</error-code>

   <location>/error404.jsp</location>

</error-page>

</web-app>

Problem:

The problem I am experiencing is that Internet Explorer refuses to show my error page and shows his.
I tried with different browsers (Mozilla, Opera) and the output of the 404.jsp page is nicely shown.

Solution 1: (Client Side)

Explorer catches the HTTP message code 404 and replaces all the content with its own error page.

To switch this feature off go to:
Tools -> Internet Options... -> Advanced.

Then look for "Show friendly HTTP error messages" and uncheck it. Click OK.

Solution 2: (Working Solution)

The custom error pages are catched and replaced if they are "too small".

Looks like that Explorer thinks your "small" page is too ugly and so decides to show his.

If the page weights a few Kb then it is shown: so you can either create
a nice looking page or you can add some extra spaces to you page to
meet the Explorer's expectations.


Solution 3:

try setting the status to 200 in your error jsp.
 
<% response.setStatus(200); %>
This should tell the browser that the request was valid and it will show your page instead of the 'Friendly Error Page'.

No comments:

Post a Comment