Thread safety is important when you are developing a servlet, or it can lead to unexpected results.
The life cycle of the servlet is the responsibility of the Web container, and when the client requests the servlet for the first time, the container is responsible for initializing the servlet, that is, instantiating the servlet class. This instance will be responsible for client requests and will not instantiate the Servlet class. That is, the servlet instance is shared by multiple threads.
So how can it be servlet security? the answer is not to use instance variables or class variables . Of course you can also use the synchronized synchronization method or use a single-threaded model, but this is inefficient.
Temporary variables do not affect thread safety because they allocate space on the stack, and each thread has its own private stack space.
JSP synchronization is also the same, because the JSP will be compiled into a servlet.
<%! in JSP String Unsafevar; The variable declared by%> is actually an instance variable of the servlet, and the variable <% String Safevar%> declares is a local variable.
Jsp/servlet Thread Safety