File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Is Servlet implementation is example of Singleton pattern? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is Servlet implementation is example of Singleton pattern?" Watch "Is Servlet implementation is example of Singleton pattern?" New topic
Author

Is Servlet implementation is example of Singleton pattern?

Surender Singh
Greenhorn

Joined: Aug 11, 2006
Posts: 5
Hi ALL
I just starting to learn JSP/Servlet. Can anybody tell me how single servlet instance is use to handle multiple requests? Is servlet implementation is example of Singleton pattern?
Thank You
Surender Singh
[ April 28, 2008: Message edited by: Surender Singh ]
chandrakant karale
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
Hi,
Welcome onboard.
As far as comparison of servlet to singleton pattern is concerned, yes it is true, servlet is initialized only once when it is loaded by the servlet container. But there are differences-
Further requests to the servlets are executed in a seperate thread where service() method is executed.

But the servlet may be loaded again by servlet container during life cycle of servlet.

According to the specification, a servlet is destroyed when "the servlet container determines that a servlet should be removed from service" (Servlet 2.3, section 2.3.4). Essentially, the Web container can potentially destroy and then reload and reinitialize the servlet at another time (as needed). In effect, startup code placed in an init() method can potentially be run multiple times during the life of an application.

-Chandrakant Karale
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, servlet instances are managed by the container.
There is no Java code in a servlet that enforces its singularity (private constructors, etc..).

You can have several servlet instances from the same class file running simultaneously by creating separate servlet entries in your deployment descriptor.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Servlet implementation is example of Singleton pattern?
 
Similar Threads
Static reference for object
private constructors
No Singletons in TDD?
singleton class
static - old and new