the thread security of the servlet. Not sure
A servlet is a single instance of multithreading to handle requests, which should be the main cause of thread safety. We know that the servlet itself is stateless, which means that the servlet itself is thread-safe, but why the web says that the servlet is thread-safe. May be based on a sentence that multiple threads will simultaneously access a servlet instance to judge.
And the servlet is not thread-safe, it is mainly determined by implementation, if a servlet implementation has instance variables, and will be multithreaded changes, this time is not thread-safe, and if there are instance variables, but the variable is read-only, this time does not involve variable changes, is thread-safe And if a servlet implementation has no instance variables and is a local variable, it is also thread-safe. HttpServlet
HttpServlet is an implementation of the servlet, inherited from Genericservlet, and is also a class that we customize the servlet to inherit, HttpServlet is not thread-safe, nor is it easy to say, depending on how it is used in the process. In addition, the use of attributes in the servlet can also have an impact on thread safety, see below. Custom Servlet
Usually we develop our own servlet to inherit HttpServlet, and then rewrite the relevant methods, when thread safety and restlessness are all determined by ourselves, without instance variables, which are thread-safe, and when there are instance variables, and they are changed. This is not thread-safe, and other means are needed to ensure thread safety. In addition, the use of attributes in the servlet can also have an impact on thread safety, see below. How to control the thread security of a servlet thread safety for variables
We know that when there are no instance variables, there is basically no thread-unsafe problem, so it is a method to not use instance variables. thread safety for properties ServletContext, not thread-safe, multithreading can read and write at the same time. Be careful when you use it. HttpSession, not thread-safe, for example, when a user opens multiple browser windows, it produces multiple requests for the same session. Be careful when you use it. ServletRequest, thread-safe, corresponds to a request, so it is thread-safe. Singlethreadmodel
We can also use this interface to create our own implementation, which ensures thread safety, and at the same time only one thread can execute the service method of the servlet instance, which is a single-threaded, which has been discarded. thread security for common frames
SPRINGMVC, we know that spring's IOC container Default-managed bean is single-instance, and is single-instance for SPRINGMVC's controller, so thread safety is needed for development.
The action in Struts1 is also a single instance of the thread security problem when used.
The action in STRUTS2 generates an instance for each request, so there is no thread safety problem.
Note: When using spring to manage Struts2 's action, you need to set the scope of the action to prototype, because the bean default in the Spring IOC container is a single example. thread security for Dispatcherservlet
When the application is started, initialization is initiated based on the configuration of spring and Springmvc configured in Web.xml, Dispatcherservlet for SPRINGMVC initialization, only once for servlet initialization, and There is only one instance , so there is only one dispatcherservlet.
However, it is thread-safe when multithreading accesses Dispatcherservlet at the same time because the internal properties in Dispatcherservlet do not affect thread safety . So dispatcherservlet can ignore thread-safe issues.
Although dispatcherservlet can be considered thread-safe, the controller in SPRINGMVC is not. Controller is also a singleton , each request corresponds to a controller method, which can be considered thread-safe without using an instance variable, but is considered thread-safe If there is an instance variable.
SPRINGMVC Processing Process:
Dispatcherservlet class Diagram: