Question - How is an application exception handling is done using a Servlet?
Answer -
The doGet() method and doPost() method throw the ServletException and IOException. The browser understands only HTML. Hence, when an exception os is thrown by an application, then, the servlet container processes the exception and generates an HTML response. Same happens with other errors such as error 404.
Servlet API supports customized Exception and Error Handler servlets that can be configured in the deployment descriptor, the purpose of these servlets is to handle the Exception thrown by application and send HTML response that is useful for the user. We can provide a link to the application home page or the details that let the user know what went wrong.
The Web.XML configuration is as follows:
404
/AppExceptionHandler
javax.servlet.ServletException
/AppExceptionHandler