Web.xml is important file for any web application and all container specific information's defined here. We can handle errors and exceptions scenarios as well in web.xml. Lets see how to do that
Handling Status Codes:
Web based applications we may get error codes like 500,501,502,404,403,400 or any other http status codes we may receive from the server. If we get this status code we don't want to show the server error message to the customer, specifically in the error case.We wanted to give generic error message to the customers, Here is an example
In the above example we provided location as error.html but if we want to handle this in servlet we can give the URL of the servlet. In the servlet we can implement specific messages to the customer and then redirect to relevant pages.
Handling Exceptions:
Similarly like error codes we can handle exception also in web.xml
Handling Status Codes:
Web based applications we may get error codes like 500,501,502,404,403,400 or any other http status codes we may receive from the server. If we get this status code we don't want to show the server error message to the customer, specifically in the error case.We wanted to give generic error message to the customers, Here is an example
<!-- Handling Error Codes --> <error-page> <error-code>500</error-code> <location>/error.html</location> </error-page> <error-page> <error-code>501</error-code> <location>/error.html</location> </error-page> <error-page> <error-code>404</error-code> <location>/error.html</location> </error-page> <error-page> <error-code>403</error-code> <location>/error.html</location> </error-page>
In the above example we provided location as error.html but if we want to handle this in servlet we can give the URL of the servlet. In the servlet we can implement specific messages to the customer and then redirect to relevant pages.
Handling Exceptions:
Similarly like error codes we can handle exception also in web.xml
<!-- Handling Exceptions in web.xml --> <error-page> <exception-type> java.io.FileNotFoundException </exception-type > <location>/error.html</location> </error-page> <error-page> <exception-type>java.lang.ClassNotFoundException</exception-type > <location>/error.html</location> </error-page>
0 Comments
Post a Comment