Understanding web.xml in Java Servlets

web.xml


Used by the Servlet Container (Ex. Tomcat) to handover user's request to a specified class.


<web-app>
<servlet>
<servlet-name>servlet_name</servlet-name>
<servlet-class>servlet_class</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>servlet_name</servlet-name>
<url-pattern>url_of_actions</url-pattern>
</servlet-mapping>
</web-app>


Explaining Tags


<web-app>: Starting of the web.xml should be done with this tag.

<servlet>: Details regarding servlets, the name, servlet class.

<servlet-name>: Name of the servlet, used within the servlet again.

<servlet-class>: Servlet class file is specified here. Servlet container passes request to this class. If was written in a package, then it must be like package_name.sub_package.class

<servlet-mapping>: Mapping servlet class and the url of the users request.  Servlet container determines user's request and then forwards it to the class file with the help of servlet name.

<url-pattern>: The pattern (the url) to which the user's request is received.