Define and map servlet in the web. xml file
In the Web. xml file, you must first define a servlet instance and then map the instance to one or more URL patterns.
1. Define the servlet instance:
<Servlet> The element defines a servlet instance. The <servlet> element must contain <servlet-Name> and <servlet-class> child elements, and may also contain other initialization parameters.
The <servlet-Name> element defines the unique name of the servlet instance. Each servlet instance must have a unique name. This name is only used for URL ing of this instance, so it does not have to be consistent with the servlet class or servlet URL.
The <servlet-class> element tells the servlet container how to build a servlet class instance. The <servlet-class> element consists of the servlet package name and Servlet class name. For example:
<Servlet>
<Servlet-Name> getstatus </servlet-Name>
<Servlet-class> status. createstatusservlet </servlet-class>
</Servlet>
The servlet Instance name does not need to be the same as the servlet class name. To load another instance of the same servlet class to the container, you only need to create another 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 may also include any number of <init-param> elements. The container sends the parameters to the respective servlet. Just as the command line parameters are passed to the entire program, the individual servlet requires 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>
One <servlet> element can only define one servlet instance. In order for the container to send requests to the servlet, a servlet must be mapped to one or more URLs or called by other servlets or filters using the servlet name.