Here we are going to discuss about SingleThradModel in Servlets. As we know that servlets are server side components that help us for developing web application on server side. Servlets are used to process multiple requests simultaneously. When we instantiate servlet then instance variable created, as a result it will be used over multiple client request,hence same instance is shared across multiple clients.
SingleThreadModel in Servlet
A Simple Servlet Example Tutorial using Tomcat and Eclipse
Servlet technology is used to create a web application resides at server side and generates a dynamic web page. Servlet technology is robust and scalable because of java language. However, there were many disadvantages to this technology. We have discussed these disadvantages below.
What is SingleThreadModel in Servlets?
This interface has no methods. If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet. Note that SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used.
It is an interface from javax. It is marker interface having no methods. Few servlets may require this to implement. Are Servlets multithreaded? First time when a request comes to a Web server for a Servlet, the Web container loads the Servlet, creates a Servlet object, executes the callback service method.