This way wouldn't it be more optimized?

public class Test extends HttpServlet
{
 private static MySingleton theInstance = new MySingleton();
 
 public static synchronized MySingleton getInstance() {
   return theInstance;
 }
}

  Anyway, hwo tomcat creates a new instance of a Servlet? My question
started because what I want is a Singleton Servlet. So if tomcat
instantiates it using the following code, and exception will be thrown,
wouldn't it?

  Class test = Class.forName("Test");
  HttpServlet servletInstance = test.newInstance();


On Tue, 2003-01-28 at 19:23, Shapira, Yoav wrote:
> Howdy,
> Tomcat won't touch your singleton at all.  All your servlets will call getInstance() 
>on the singleton.  The constructor for the singleton is private, and the 
>getInstance() method looks like:
> 
> private static MySingleton theInstance;
> 
> public static synchronized MySingleton getInstance() {
>   if(theInstance == null) {
>      theInstance = new MySingleton();
>   }
> 
>   return theInstance;
> }
> 
> That way no matter how many servlet instances the container spawns, and no matter 
>how many of them call getInstance() at the same time, you will only have one instance 
>of your singleton.
> 
> Yoav Shapira
> Millennium ChemInformatics
> 
> 
> >-----Original Message-----
> >From: Felipe Schnack [mailto:[EMAIL PROTECTED]]
> >Sent: Tuesday, January 28, 2003 4:19 PM
> >To: Tomcat Users List
> >Subject: RE: servlets
> >
> >  yes, I know how to do it (private constructor, etc), but how tomcat
> >will call an getInstance() method instead of create a new instance of
> >it?
> >
> >On Tue, 2003-01-28 at 19:13, Shapira, Yoav wrote:
> >> Howdy,
> >>
> >> >  If I write an servlet and DO NOT implement the SingleThreadModel, I
> >> >have a guarantee that I'll have only one instance of it?
> >>
> >> No.
> >>
> >> >If not, here is
> >> >a way to do this?
> >>
> >> No.
> >>
> >> If you need only one instance of something, put in in a singleton.  Have
> >> your servlets use the singleton.  A google search for "the singleton
> >> design pattern java" will yield many explanations and examples.
> >>
> >> Yoav Shapira
> >> Millennium ChemInformatics
> >>
> >> --
> >> To unsubscribe, e-mail:   <mailto:tomcat-user-
> >[EMAIL PROTECTED]>
> >> For additional commands, e-mail: <mailto:tomcat-user-
> >[EMAIL PROTECTED]>
> >>
> >--
> >
> >Felipe Schnack
> >Analista de Sistemas
> >[EMAIL PROTECTED]
> >Cel.: (51)91287530
> >Linux Counter #281893
> >
> >Centro Universitário Ritter dos Reis
> >http://www.ritterdosreis.br
> >[EMAIL PROTECTED]
> >Fone/Fax.: (51)32303341
> >
> >
> >--
> >To unsubscribe, e-mail:   <mailto:tomcat-user-
> >[EMAIL PROTECTED]>
> >For additional commands, e-mail: <mailto:tomcat-user-
> >[EMAIL PROTECTED]>
> 
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> 
-- 

Felipe Schnack
Analista de Sistemas
[EMAIL PROTECTED]
Cel.: (51)91287530
Linux Counter #281893

Centro Universitário Ritter dos Reis
http://www.ritterdosreis.br
[EMAIL PROTECTED]
Fone/Fax.: (51)32303341


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to