Best way to handle 404 - page not found error in a jsp.


Web tier: servlets, JSP, Web frameworks: Best way to handle 404 - page not found error in a jsp.

  1. hi

    The problem that i am facing is I have a simple test application if a page is not found than it show 404 error but instead of this page i want to show my own custom error page.

    Threaded Messages (15)

  2. Hey Amit,

    In your deployment descriptor (web.xml) file, use an <error-page> element to define the 404 error, as follows:


    -- Joe
  3. Hey joe,

    I have tried that but it is still not working.
    Suppose that a Application has a form in a form.jsp with an action attribute set another action.jsp page and in errorpage attribute set to "Error.jsp".
    And then what happens is that the application does not find the action.jsp as it does not exits.
    solve this problem pls
  4. HTTPErrorTrapping[ Go to top ]

    Hi Amit,

    Alternatively, you can specify an HTTP error code in the <exception-type> element

    Handling HTTP error codes in web.xml


  5. Its possible that from the Microsoft Internet Explorer, you may not be able to see some of the customized error pages that are configured in web.xml. Instead you will see 'Page Not Found' - 404 error. To rectify this problem, from the browser's 'Tools' main menu , choose the option 'Internet Options'. Click on the 'Advanced' tab. Uncheck the option 'Show friendly HTTP error messages' and try invoking the servlets again.

  6. 404 Exception handling[ Go to top ]

    Hi Subalan,

    The thing that you suggest to show the custom 404 error page are not give desired result for my application. Is there any way that I can configure this custom error page on my server(Tomcat) so that it gives the desired result.

    thanks for all the above tips.
    Amit Sharma.
  7. 404 Exception handling[ Go to top ]

    hi subalan

    thanks yaar your tip is working it was due to the Explorer .

    thanks for the help.
    Amit Sharma
  8. Thanks Sunbalan.....[ Go to top ]

    It really worked for me....

    -Bhuvana Sundaramoorthy
  9. You need a 5KB+ error page

    from :

    IE overrides several HTTP error status pages but it has a size threshold. Basically if the error page send by the server has a large enough body then IE decides it's meaningful and displays it.

    Usually to be safe you should make error pages that are larger then 512 bytes. The threshold varies per HTTP status code. You can look at what your thresholds are currently set to. In IE 5 and greater the settings are stored in the registry under[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\ErrorThresholds]

    Err Size(bytes)
    400 512
    403 256
    404 512
    405 256
    406 512
    408 512
    409 512
    410 256
    500 512
    501 512
    505 512
  10. hi ,

    but it wont be possible to say the client to adjust with internet tools option. Is there any other way apart from .htaccess 

  11. Try a filter[ Go to top ]

    You cannot get around the fact that ie default shows its own error page, when the response contains a error code.

    But as a workarround you can create a filter like this:

    public class ErrorHandlerFilter implements Filter {

        public void init(FilterConfig arg0) throws ServletException {

        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

            HttpServletResponse response = (HttpServletResponse) servletResponse;

            if (!(response instanceof ErrorAwareRequestWrapper)) {
                response = new ErrorAwareRequestWrapper(response);

            filterChain.doFilter(servletRequest, response);

        public void destroy() {

    And a wrapper for the response like this:


    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServletResponseWrapper;

    public class ErrorAwareRequestWrapper extends HttpServletResponseWrapper{

        public ErrorAwareRequestWrapper(HttpServletResponse httpServletResponse) {

        public void sendError(int errorCode) throws IOException {
            if (errorCode == HttpServletResponse.SC_NOT_FOUND){
                throw new PageNotFoundException();

    The exception:

    public class PageNotFoundException extends RuntimeException{

    In your web.xml, define the filer (should be the first filter):

             <!-- These are needed by Tomcat 5 for forwards -->



  12. Forgot something[ Go to top ]

    Your errorpage (in my example: 404.jsp) must have this:

    <%@ page language="java" isErrorPage="true" %>

  13. Question about Wrapper[ Go to top ]

    When is your sendError(int sc) called ?
    And how/where can you determine the status code ?
  14. RE: Question about Wrapper[ Go to top ]


    The sendError(int sc) in ErrorAwareRequestWrapper is callede (or should be) by the webcontainer, when it "discovers" that there are no mappings (or static content) to a given web-resource.

    The filter is mappede to recive all request, also those who result in a "page not found", otherwise it would be impossible to trace these incidents.

    The status code can be determined by the int parameter (sc), see

  15. Re: Try a filter[ Go to top ]

    thanks for the solution!!!!
  16. Try a filter[ Go to top ]

    Hi Morten,

    I did all the steps you mentioned in the article. it compiles and and in run time it's not calling sendError method in, due to which it's not displaying the error page. what am I missing?. I kept some Sysytem.out lines in all the methods. From the stdout I see that it's not printing what's there in sendError method. what am I missing?