The Web. xml file defines and maps the servlet (1/3) __web

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.