Hi All Everyone is enjoyed Christmas festival still some part of the world celebration going up to New Year. Any have spent some valuable time to see my blog also for this thanks lot. I am trying to give some information regarding Servlet Exception Handling. In Every Application Development you must raise some un excepted error may occurred. Sometimes you need to handle Exception for end user point of you, so in order to use servlet you need to handle exception in the following different ways. Any one of the technique you can implement some times more than one technique also allows.  Now the time to see the different Techniques.

1.       When a servlet throws an exception, the web container searches the configurations in web.xml that use the exception-type element for a match with the thrown exception type.

You would have to use the error-page element in web.xml to specify the invocation of servlets in response to certain exceptions or HTTP status codes.

Consider, you have an ErrorHandler servelt which would be called whenever there is any defined exception or error. Following would be the entry created in web.xml.

<!-- servlet definition -->
<servlet>
        <servlet-name>ErrorHandler</servlet-name>
        <servlet-class>ErrorHandler</servlet-class>
</servlet>
<!-- servlet mappings -->
<servlet-mapping>
        <servlet-name>ErrorHandler</servlet-name>
        <url-pattern>/ErrorHandler</url-pattern>
</servlet-mapping>

<!-- error-code related error pages -->
<error-page>
    <error-code>404</error-code>
    <location>/ErrorHandler</location>
</error-page>
<error-page>
    <error-code>403</error-code>
    <location>/ErrorHandler</location>
</error-page>

<!-- exception-type related error pages -->
<error-page>
    <exception-type>
          javax.servlet.ServletException
    </exception-type >
    <location>/ErrorHandler</location>
</error-page>

<error-page>
    <exception-type>java.io.IOException</exception-type >
    <location>/ErrorHandler</location>
</error-page>


2.      If you want to have a generic Error Handler for all the exceptions then you should define following error-page instead of defining separate error-page elements for every exception:

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

Following are the points to be noted about above web.xml for Exception Handling:
  • The servelt ErrorHandler is defined in usual way as any other servlet and configured in web.xml.
  • If there is any error with status code either 404 ( Not Found) or 403 ( Forbidden ), then ErrorHandler servlet would be called.
  • If the web application throws either ServletException or IOException, then the web container invokes the /ErrorHandler servlet.
  • You can define different Error Handlers to handle different type of errors or exceptions. Above example is very much generic and hope it serve the purpose to explain you the basic concept.
3.      Request Attributes - Errors/Exceptions:
Following is the list of request attributes that an error-handling servlet can access to analyses the nature of error/exception.
Hey common I am not forced to read entire thing here you know my motive right I am stopping here to give much please see the following video you can understand better way .


        

Post a Comment

 
Top