Servlet: Singleton, Singlethread or Multi Instance Multithread

This question was asked earlier and discussed earlier, but I want to ask it further.

  • Are Servlets Singleton or Not? According to me, they are initialized only by the container, but are they still not single? why??

  • Servlets Single Thread or multi Threaded (Forget the javax.servlet.SingleThreadModel class ) i.e. What happens when there are multiple requests for a single servlet? If they are executed conncurrently, does that mean multithreading ?? and if it is multithreading, then each thread will have a servlet instance, which contradicts the 1st point !!

What I think Whenever a new request is requested, the container creates a new thread for the incoming say Req1 request, in which it calls or sends the control to the servlet service method. Now this execution is happening concurrenlty .. I think so ...

Is my work the same in MVV envirionment ?? (say Struts 1/2, Springs)

+6
source share
4 answers

The fact that there is only one instance does not mean that it is not multithreaded. More than a thread can simultaneously call the same method of the same instance. Servlets are absolutely multithreaded.

+5
source

Multi-threaded servlets are the foundation of their efficiency. You can use "implements SingleThreadModel" to make the servlet single-threaded, so a new object will be created for each request.

Recently, this SingleThreadModel interface is deprecated

+3
source

It depends on the implementation. The servlet interface is not specified. http://docs.oracle.com/javaee/5/api/javax/servlet/Servlet.html

However, if you see the HttpServlet, you will see that it indicates that it should be synchronized. http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServlet.html

+2
source

Perhaps the diagram in the link below illustrates everything ... http://www.tutorialspoint.com/servlets/servlets-life-cycle.htm

+1
source

Source: https://habr.com/ru/post/946683/


All Articles