JavaWeb study notes 16--Servlet mapping matching problem, thread safety problem

Welcome to reprint, but please keep the original source of the article →_→

Article source: http://www.cnblogs.com/smyhvae/p/4140529.html

 

1. Servlet mapping matching problem:

This knowledge has already been covered in the fourth paragraph of the first article ( MyEclipse and Tomcat configuration ), now let's refine it:

Since the client accesses the resources in the web server through the URL address, if the servlet program wants to be accessed by the outside world, it must map the servlet program to a URL address. This work uses the <servlet> element and the <servlet> element in the web.xml file. -mapping > element complete.

The <servlet> element is used to register the servlet, and it contains two main sub-elements: <servlet-name> and <servlet-class>, which are used to set the registered name of the servlet and the full class name of the servlet, respectively.

A <servlet-mapping> element is used to map an external access path of a registered servlet. It contains two sub-elements: <servlet-name> and <url-pattern>, which are used to specify the registered name of the servlet and the servlet name respectively. external access path. E.g:

        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.vae.servlet.MyServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/servlet/MyServlet</url-pattern>
    </servlet-mapping>
 

have to be aware of is:

1. A <servlet> can correspond to multiple <serlvet-mapping>s, so that a servlet can have multiple paths to access

2. The path in url-partten can also use the * wildcard , but there can only be two fixed formats : one format is "*. extension", the other format starts with a forward slash (/) and ends with "/*"end.

Due to the introduction of *, it is possible that a path is matched by multiple urlpartten, which is the priority judgment condition as follows:

  • which one is more accurate
  • *. The format of the suffix always matches the lowest level

【Example】

For some of the following mapping relationships:

  • Servlet1 maps to /abc/*
  • Servlet2 maps to /* (meaning any path will match)
  • Servlet3 maps to /abc
  • Servlet4 maps to *.do

question:

  • When the request URL is "/abc/a.html", both "/abc/*" and "/*" match, which servlet responds? The servlet engine will call Servlet1.
  • When the request URL is "/abc", both "/abc/*" and "/abc" match, which servlet responds? The servlet engine will call servlet3.
  • When the request URL is "/abc/a.do", both "/abc/*" and "*.do" match, which servlet responds? The servlet engine will call Servlet1.
  • When the request URL is "/a.do", both "/*" and "*.do" match, which servlet responds? The servlet engine will call servlet2.
  • When the request URL is "/xxx/yyy/a.do", both "/*" and "*.do" match, which servlet responds? The servlet engine will call servlet2.

 

3. You can configure <load-on-startup> in the <serlvet> tag to specify the startup sequence. By default, the servlet is created when it is accessed for the first time. If this tag is configured, it will be created with the startup of the web application . Example:

    <servlet>
        <servlet-name>Servlet2</servlet-name>
        <servlet-class>Servlet2</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

The int value on line 4 indicates the startup order of multiple serlvets. 

 

4. Default Servlet:

If there is a servlet whose url-partten is configured as a forward slash "/", this servlet becomes the default servlet. Requests that other servlets do not handle are handled by the default servlet .

In fact, the access to static resources is performed by the default Servlet; the prompt pages such as setting 404 pages and 500 pages are also performed by the default Servlet. Usually we don't configure the default servlet by ourselves

 

2. Thread safety issues

The servlet engine operates in a multi-threaded mode, which uses a separate thread for each concurrent access request to respond.

Since servlet has only one object in memory by default , it may cause thread safety problem when multiple browsers access servlet concurrently.

Note for interview questions: Servlet thread is not safe, and only one instance is maintained from beginning to end.

Note: The essence of thread safety is that the same resource is operated by multiple threads at the same time, which may interfere with each other.  

solution:

1. SingleThreadModel interface (marker interface, single- threaded model interface ): cannot really prevent thread safety issues (obsolete)

The servlet implements the SingleThreadModel interface, then the servlet engine will call the service method of the servlet in a single thread mode. For a servlet that implements the SingleThreadModel interface, the servlet engine still supports multi-threaded concurrent access to the servlet by generating multiple servlet instance objects, and each concurrent thread calls an independent servlet instance object .

Note: This interface is outdated in API 2.4. Although it solves the problem of thread safety, it consumes a lot of performance (different clients will create different Servlet instances when accessing at the same time), so this method is not recommended and is not used in actual development. .

2. Use synchronized code blocks: reduced efficiency

The following issues need to be considered for thread safety: (when multiple threads access data at the same time)

  • When accessing member variables (so, when defining member variables in servlets, thread safety issues need to be considered) 
  • When accessing shared resources

Note: Try not to use member variables in Servlet.

Code example: When accessing member variables, consider thread safety issues

 
1 public class MyServlet1 extends HttpServlet {
 2     private static final long serialVersionUID = 1L;
 3     
 4 public int count = 10;// Define member variables in Servlet, thread safety issues need to be considered     
 5        
 6     public MyServlet1() {
 7         super();
 8     }
 9  
10     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
11         doPost(request, response);
12     }
13  
14     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
15     
16         synchronized (this) {
17             count ++;
18         }
19     }
20  
21 }
 

 

Final solution : Use class variables (member variables) as little as possible in Servlet. If you must use class variables, use locks to prevent thread safety problems, but pay attention to locking the content should be the core code that causes thread safety problems. Less lock main content, reduce waiting time and improve servlet response speed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325419396&siteId=291194637