This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Servlet implementation is example of Singleton pattern?

 
Surender Singh
Greenhorn
Posts: 5
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic