A servlet is defined and mapped in the Web. xml file
The Web. xml file must first define a servlet instance (servlet instance) and then map this instance to one or more URL patterns.
1. Define the servlet instance:
The <servlet> element defines a servlet instance. The <servlet> element must contain <servlet-name> and <servlet-class> two child elements, or it may include other initialization parameters.
The <servlet-name> element defines a unique name for the servlet instance. Each servlet instance must have a unique name that is used only to match the URL mapping for this instance, so it is not necessarily consistent with the URL of the servlet class or servlet.
The <servlet-class> element tells the servlet container how to construct an instance of a servlet class. The <servlet-class> element contains two parts, containing the servlet's package name and the class name of the Servlet class. For example:
<servlet>
<servlet-name>getStatus</servlet-name>
<servlet-class>status.createStatusServlet</servlet-class>
</servlet>
Because the name of the servlet instance does not have to match the class name of the Servlet class. To load another instance of the same servlet class into the container, just a different servlet instance name. For example:
<servlet>
<servlet-name>getStatus</servlet-name>
<servlet-class>status.createStatusServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>fullStatus</servlet-name>
<servlet-class>status.createStatusServlet</servlet-class>
</servlet>
Each <servlet> element also contains any number of <init-param> elements that are optional. The container passes parameters to the respective servlet. As with the command-line arguments passed to the entire program, individual servlets require their respective parameter names and parameter values. For example:
<servlet>
<servlet-name>getStatus</servlet-name>
<servlet-class>status.createStatusServlet</servlet-class>
<init-param>
<param-name>output</param-name>
<param-value>brief</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>fullStatus</servlet-name>
<servlet-class>status.createStatusServlet</servlet-class>
<init-param>
<param-name>output</param-name>
<param-value>verbose</param-value>
</init-param>
</servlet>
A <servlet> element can only define one servlet instance. In order for the container to pass requests to the servlet, a servlet must be mapped to one or more URLs or invoked by the name of the servlet by another servlet or filter.