Deployment descriptor is used in Java Web applications for Url Hiding, Error handling, Welcome page and many more.
1] Url Hiding
Importance: 1] URL are the exact address of a specific website. Hence URL hiding increase security since it does not reveal original name of the page.
2] URL can be customized which can be easily read by users.
Url-hiding.jsp
index.jsp
web.xml
2] Exceptional Handling using web.xml
We can customize what the server sends to the user when an error occurs, using the deployment descriptor.
Example: If a user clicks on a link that does not exist then customized error page can be displayed.
index.jsp
error.jsp
web.xml
Note: 1] abc.jsp file mentioned in index.jsp does not exist . There are only 2 jsp files index.jsp and error.jsp. Hence when user clicks on the link error is thrown and redirected to error.jsp through web.xml
2] java.lang.Exception is used to handle any exception . If we want to handle for a particular error code then replace <exception-type> with <error-code> .It can be done in the following way
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
1] Url Hiding
Importance: 1] URL are the exact address of a specific website. Hence URL hiding increase security since it does not reveal original name of the page.
2] URL can be customized which can be easily read by users.
Url-hiding.jsp
1 2 3 4 5 6 7 8 | <html> <head> <title>Passionate codes</title> </head> <body> <a href="homepage"> index page</a> <!--mention the url same as mentioned in web.xml as shown below--> </body> </html> |
index.jsp
1 2 3 4 5 6 7 8 | <html> <head> <title>Passionate codes</title> </head> <body> index.jsp is written as homepage in url </body> </html> |
web.xml
1 2 3 4 5 6 7 8 9 10 | <web-app> <servlet> <servlet-name>index.jsp</servlet-name> <jsp-file>/index.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>index.jsp</servlet-name> <url-pattern>/homepage</url-pattern> <!-- give the url that u want to display here --> </servlet-mapping> </web-app> |
2] Exceptional Handling using web.xml
We can customize what the server sends to the user when an error occurs, using the deployment descriptor.
Example: If a user clicks on a link that does not exist then customized error page can be displayed.
index.jsp
1 2 3 4 5 6 7 8 | <html> <head> <title>Passionate codes</title> </head> <body> <a href="abc.jsp"> passionate codes </a> </body> </html> |
error.jsp
1 2 3 4 5 6 7 | <html> <head> <title>Passionate codes</title> <h3>Sorry an exception occurred !</h3> The page you are looking for might have been moved or no longer exist. Please contact system admin </body> </html> |
web.xml
1 2 3 4 5 6 | <web-app> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/error.jsp</location> </error-page> </web-app> |
Note: 1] abc.jsp file mentioned in index.jsp does not exist . There are only 2 jsp files index.jsp and error.jsp. Hence when user clicks on the link error is thrown and redirected to error.jsp through web.xml
2] java.lang.Exception is used to handle any exception . If we want to handle for a particular error code then replace <exception-type> with <error-code> .It can be done in the following way
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>