userimage

How to handle exceptions thrown by application with another servlet?

From my servlet web application continue getting exceptions because of different different reasons.I google about this and got some suggestion too but unable to implement in my application. So need some understanding on this, means how can I handle if any exception will throw from the servlet.
userimage

If you notice, doGet() and doPost() methods throw ServletException and IOException. Since browser understand only HTML, when our application throw exception, servlet container processes the exception and generate a HTML response. Same goes with other error codes like 404, 403 etc.

Servlet API provides support for custom Exception and Error Handler servlets that we can configure in deployment descriptor, the whole purpose of these servlets are to handle the Exception or Error raised by application and send HTML response that is useful for the user. We can provide link to application home page or some details to let user know what went wrong.


<error-page>
    <error-code>404</error-code>
    <location>/AppExceptionHandler</location>
</error-page>
    
<error-page>
    <exception-type>javax.servlet.ServletException</exception-type>
    <location>/AppExceptionHandler</location>
</error-page>


Place the above snippet into your web.xml file.


Answer is