Learn the details of 2_Day09_servlet, 2_day09_servlet

Source: Internet
Author: User

Learn the details of 2_Day09_servlet, 2_day09_servlet
Servlet details

 

 

1 Servlet and thread security

Because a type Servlet has only one instance object, it is possible that a Servlet can process multiple requests at the same time. Is the Servlet thread safe? The answer is "Not thread-safe ". This shows that Servlet is very efficient, but there are also thread security problems!

Therefore, we should not create a member variable cheaply in the Servlet, because one thread may write this member variable, and the other thread may read this member variable.

2. Let the server create a Servlet at startup

By default, the server creates a Servlet when it receives the request for the first time. You can also configure the Servlet in web. xml so that the Servlet is created when the server is started.

<Servlet>

<Servlet-name> hello1 </servlet-name>

<Servlet-class> cn. itcast. servlet. Hello1Servlet </servlet-class>

<Load-on-startup> 0 </load-on-startup> [c1]

</Servlet>

<Servlet-mapping>

<Servlet-name> hello1 </servlet-name>

<Url-pattern>/hello1 </url-pattern>

</Servlet-mapping>

<Servlet>

<Servlet-name> hello2 </servlet-name>

<Servlet-class> cn. itcast. servlet. Hello2Servlet </servlet-class>

<Load-on-startup> 1 </load-on-startup>

</Servlet>

<Servlet-mapping>

<Servlet-name> hello2 </servlet-name>

<Url-pattern>/hello2 </url-pattern>

</Servlet-mapping>

<Servlet>

<Servlet-name> hello3 </servlet-name>

<Servlet-class> cn. itcast. servlet. Hello3Servlet </servlet-class>

<Load-on-startup> 2 </load-on-startup>

</Servlet>

<Servlet-mapping>

<Servlet-name> hello3 </servlet-name>

<Url-pattern>/hello3 </url-pattern>

</Servlet-mapping>

[C1] configure <load-on-startup> in <servlet>. A non-negative integer is given!

Configure the <load-on-startup> element in the <servlet> element so that the server can create the Servlet at startup, the value of the <load-on-startup> element must be an integer greater than or equal to that of the Servlet created at server startup. In the preceding example, the server creates Servlet in the order of Hello1Servlet, Hello2Servlet, and Hello3Servlet Based on the <load-on-startup> value.

3 <url-pattern>

<Url-pattern> is a child element of <servlet-mapping>. It is used to specify the Servlet access path, that is, the URL. It must start!

1)You can provide multiple <url-pattern> in <servlet-mapping>, for example:

<Servlet-mapping>

<Servlet-name> AServlet </servlet-name>

<Url-pattern>/AServlet </url-pattern>

<Url-pattern>/BServlet </url-pattern>

</Servlet-mapping>

 

This indicates that a Servlet is bound to two URLs. No matter whether it is accessing/AServlet or/BServlet, all access is AServlet.

 

2)You can also use a wildcard in <url-pattern>. The wildcard is the asterisk (*). The asterisk can match any URL prefix or suffix. You can use a wildcard to name a Servlet and bind a group of URLs, for example:

Please note that,The wildcard is either a prefix or a suffix, and cannot appear in the middle of the URL or only a wildcard. For example,/*. do is incorrect.Because the asterisk appears in the middle of the URL. *. * This is also incorrect because only one wildcard can appear in a URL.

 

Note that Wildcards are a fuzzy match of URLs. If more specific <URL-pattern> exists, the access path matches the specific <url-pattern>. For example:

<Servlet>

<Servlet-name> hello1 </servlet-name>

<Servlet-class> cn. itcast. servlet. Hello1Servlet </servlet-class>

</Servlet>

<Servlet-mapping>

<Servlet-name> hello1 </servlet-name>

<Url-pattern>/servlet/hello1 </url-pattern>

</Servlet-mapping>

<Servlet>

<Servlet-name> hello2 </servlet-name>

<Servlet-class> cn. itcast. servlet. Hello2Servlet </servlet-class>

</Servlet>

<Servlet-mapping>

<Servlet-name> hello2 </servlet-name>

<Url-pattern>/servlet/* </url-pattern>

</Servlet-mapping>

 

When the access path is http: // localhost: 8080/hello/servlet/hello1, because the access path matches the <url-pattern> of hello1, matches <url-pattern> of hello2, but because no wildcard exists in <url-pattern> of hello1, hello1 is set first.

 

 

 

 

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.