The moose likes Servlets and the fly likes Is servlet always singleton class? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Is servlet always singleton class?" Watch "Is servlet always singleton class?" New topic
Author

Is servlet always singleton class?

Makesh Kumar Ramakrishnan
Ranch Hand

Joined: Jan 07, 2006
Posts: 88
Is servlet always singleton class? or Does it depend on Application Server?


Makesh<br /> <br />SCJP | SCWCD | SCBCD | SCEA
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56153
    
  13

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.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Makesh Kumar Ramakrishnan
Ranch Hand

Joined: Jan 07, 2006
Posts: 88
Thanks, I got it.
Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
Hi, Could you please elaborate why Servlet is not considered as Singleton?. Thanks.


Regards,<br />Yasin<br />SCJP,SCWCD,SCBCD
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8259

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
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

Joined: Jan 10, 2002
Posts: 56153
    
  13

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

Joined: Aug 28, 2002
Posts: 48
Thanks All.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is servlet always singleton class?
 
Similar Threads
Servlet container will create only one instance of the class?
Singleton Question
Does it make any need to have static variables inside a Servlet
Servlets
Providing sessionbeans through init()