Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is servlet always singleton class?

 
Makesh Kumar Ramakrishnan
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is servlet always singleton class? or Does it depend on Application Server?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65542
110
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are not singletons by definition. Just because a single instance is created is not enough to consider a class a singleton.

But to answer the question that I think you meant to ask: a container will usually create one instance of a servlet for each servlet declaration in the deployment descriptor.
 
Makesh Kumar Ramakrishnan
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I got it.
 
Sahul Yasin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Could you please elaborate why Servlet is not considered as Singleton?. Thanks.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.

Wiki on Singleton

The Servlet API does not restrict the creation of a particular Servlet class to a single instance, therefore it is not a Singleton.
 
Sahul Yasin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,

Since there is always only one Servlet instance in the address space at a particular time, was bit confused. Thanks.

Well, is there any way to make the Servlet singleton?. This was asked in a Interview.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65542
110
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on what the interviewer was thinking.

The real answer is no. But he or she may have been mistakenly fishing for another answer.
 
Sahul Yasin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All.
 
it's a teeny, tiny, wafer thin ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!