Exception handling in Servlet

Exception Handling is one of important activity for any web application. If we do not implemented proper exception handling in the web application then application can crash and end user may be confused. 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 useful HTML response to user.

Exception Handling Example :-

servlet-throwing-custom-exception

Exception Generated by container :-

When user access the servlet by using url-pattern as shown in below image address bar them he will see the error message as shown below.
servlet-exception

Servlet Exception and Error Handling :-

Servlet API have support for custom Exception and Error Handler servlets that we can configure in deployment descriptor(web.xml). Here we want to show meaning full massage to end user instead of showing default error message coming from servlet container. exception-view-render


global-custom-exception-handler

showException.jsp Code

exception-view-render