One, the servlet is a Java class for other Java program calls, such as the Tomcat server, it can not run alone, it is run by the servlet engine to control and dispatch.
Second, servlet is a single case, multithreading
In the case of multiple clients accessing a servlet, the servlet is instantiated only once, that is, its init () method runs only once.
For each request, the servlet engine creates a new ServletRequest and servletresponse corresponding object, which is then passed as a parameter to the service () method, and the service calls Doget () or Dopost () on request.
Servlets are multi-threaded.
Three, multi-threaded security issues
For global variables, if high concurrency is the same variable, one thread request is not finished, and the next thread request comes.
This creates an error in the operation of the variable.
For example, for global variables I
When the first thread is taken out, the i++ operation is performed, the i++ is not executed, the next thread arrives, and the i++ operation is performed, in fact i++ executed two times, but the final result is only 1.
To solve this problem, or use a synchronous approach, use the synchronized
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Servlet Basic Concepts