File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Servlet implementation is example of Singleton pattern?

 
Surender Singh
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic