1

I am working on error handling in jsp/tomcat. My web.xml is like this.

<servlet>
        <description></description>
        <display-name>ErrorServlet</display-name>
        <servlet-name>ErrorServlet</servlet-name>
        <servlet-class>
                com.nsn.flexi.webtest.ErrorServlet
        </servlet-class>
        </servlet>
       <servlet-mapping>
        <servlet-name>ErrorServlet</servlet-name>
        <url-pattern>/ErrorServlet</url-pattern>
        </servlet-mapping>
    <error-page>
        <error-code>404</error-code>
        <location>/ErrorServlet</location>
    </error-page>

With few more error code. If the status is 404, I want to redirect the user to an error page in the Servlet.

Integer statusCode = (Integer)
    request.getAttribute("javax.servlet.error.status_code");
        switch(statusCode){
        case 403:
            System.out.println("error 403 --");
            break;
        case 404:
            System.out.println("error 404 ");
        **//SEND REDIRECT**
            break;
        default:
            System.out.println("default error");
        }

My problem here is, this code is also getting executed if some java scripts are not found in jsp. Is there anyway, I could get this code executed only if the 404 error comes for JSP or Servlet.

sabu
  • 1,969
  • 4
  • 18
  • 28
  • 1
    Why don't you make sure that your Javascript files are found in the first place? – Uooo Jan 13 '14 at 10:06
  • Because the webapp can be customized. The user has to add a configuration js file. If its not found then we go with default settings. – sabu Jan 13 '14 at 10:11
  • I think `BalusC` & `Bozho` can help you out. http://stackoverflow.com/questions/4948275/get-url-of-page-requested-that-caused-a-404 . Get the url and figure out if its the jsp that is causing the 404. – Sorter Jan 13 '14 at 10:17
  • @BalusC. Could you please help. – sabu Jan 15 '14 at 04:13

2 Answers2

1

Your web.xml should look like

<error-page>
    <error-code>404</error-code>
    <location>/ErrorServlet?error=404</location>
</error-page>
<error-page>
    <error-code>405</error-code>
    <location>/ErrorServlet?error=405</location>
</error-page>

Here your passing error code by parameter.

You can access parameter though servlet request. Your code for accessing request should look like

Integer statusCode = (Integer) request.getAttribute("error")
        switch(statusCode){
        case 403:
            System.out.println("error 403 --");
            break;
        case 404:
            System.out.println("error 404 ");
             **//SEND REDIRECT**
            break;
        default:
            System.out.println("default error");
        }

You will have error code in statusCode Variable,now you can use that easily.

Thank you

Oomph Fortuity
  • 5,710
  • 10
  • 44
  • 89
0

i dont think so 404 error will come if the url is not correct that means if the requested resource is not founded,also when some scripts missing, but we cant distinguish is it because of missing scripts or jsp...

Lijo
  • 6,498
  • 5
  • 49
  • 60