views:

51

answers:

3

Normally Servlets are initiated just once and web container simple spawns a new thread for every user request. Let's say if I create my own web container from scratch and instead of Threads, I simply create Servlets as Singleton. Will I be missing anything here? I guess, in this case, the singleton can only service one user request at a time and not multiple.

A: 

That's basically it.

I would question the motivations for creating your own container, with so many available for a wide range of purposes.

Will Hartung
well, I just took that as an example. ofcourse wouldn't really reinvent the wheel.
Rohit
A: 

JavaEE used to have a mechanism for this - a marker interface called SingleThreadModel that your servlet could implement:

Ensures that servlets handle only one request at a time. 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 recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. This interface is deprecated in Servlet API version 2.4.

Containers could use this to instantiate a new servlet for each request, or maintain a pool of them, if they chose to.

This was deprecated in Servlet 2.4, for the reasons documented above. Those same reasons still apply to your question.

skaffman
+1  A: 

Normally Servlets are initiated just once and web container simple spawns a new thread for every user request.

The first statement is true, but the second actually not. Normally, threads are been created once during applications startup and kept in a thread pool. When a thread has finished its request-response processing job, it will be returned to the pool. That's also why using ThreadLocal in a servletcontainer must be taken with high care.

Let's say if I create my own web container from scratch and instead of Threads, I simply create Servlets as Singleton. Will I be missing anything here?

They does not necessarily need to follow the singleton pattern. Just create only one instance of them during application's startup and keep them in memory throughout application's lifetime and just let all threads access the same instance.

I guess, in this case, the singleton can only service one user request at a time and not multiple.

This is not true. This will only happen when you synchronize the access to the singleton's methods on an application-wide lock. For example by adding the synchronized modifier to the method of your servlet or a synchronized(this) in the manager's method who is delegating the requests to the servlets.

BalusC