Why the instance variable in Servlet is not thread safe

When I read Head First Servlet and JSP, they say that the instance variable is unsafe.

I do not understand this statement. For example: I have a servlet whose name ActionServlet.java. Each time each user request is sent to the server, the container creates a new stream and creates a new instance ActionServlet.

ActionServlet may have the structure:

public class ActionServlet extends HttpServlet {
   // example of instance variable
   Instance variable;
   public void processRequest(HttpServletRequest request, HttpServletResponse response) {   
       // process something relating to instance variable
   }
}

So, since all of these threads create a new instance of the class for ActionServlet, so I don't see any problem here. because the instances of these threads are separate from each other.

Please find out where the problem is when using an instance variable in a multi-threaded environment.

Thank:)

+5
source
3

, , :

, ActionServlet, . .

Servlet . . .

Stripes , . , , Struts 1 Servlet .

, , , , . .

+15

(action.java),

, , , .

, , . , request response? , , , .

, , .

+3

The fact is that your action.java is not always created, but it is taken from the pool of instances, the same applies to request streams, they are taken from the pool of streams, so the servlet instance can be divided by several requests.

0
source

All Articles