Hi, you forgot a "static" in the getter method, but I still think it's bad advice,
singletons are way overused. OO is all about using the "proper context",
and 99.9% of the time "static" is not that context. In this case the ServletContext
would be the proper one. So the code would look something like this:

public MyClass getMyClassInstance(String name,  ... ) {
    ServletContext context = getServletContext();    // assuming HttpServlet subclass, 
in a JSP page this object would
                                                                            // be 
implictly defined as "application" in the main method...

    synchronized(context) {
        MyClass instance = (MyClass)context.getAttribute(name);
        if(instance == null) {
            instance = new MyClass(...);
            context.setAttribute(name, instance);
        }
        return instance;
    }
}

... singletons are useful as "generic/immutable object factories" though, but it didnt 
sound like that was the case in this post,
this approach also has the benefit of the created objects being usable automatically 
as application-scope beans in JSP-pages.

Hope it helps, have a nice day! :)

/Magnus Stenman
Orion Application Server - http://www.orionserver.com

----- Original Message -----
From: Channing Walton <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, July 21, 1999 10:03 AM
Subject: Re: Inter servlet comm and Session


> > -----Original Message-----
> > From: Ashok MAdhavan [mailto:[EMAIL PROTECTED]]
> > Sent: 20 July 1999 12:43
> > Subject: Inter servlet comm and Session
> >
> >
> > Hi guys,
> >
> > I would like to access an object across servlets in a session. one way
> > would be store the object along with your session and we can retrieve
> > it.But the object is slightly big and there is one object per session
> > and there are many concurrent users. so the session object might become
> > large.this might become a load on the server.
> > is there any other way of accessing of object across servlets in a
> > session.
>
> Hi,
> If you only need a single object shared across lots of sessions then I
> suggest the use of the Singleton design pattern so that only one instance of
> the object exists (although you might have many references to it).
>
> Here is a simple example:
>
> public class MySingleton {
>         private MySingleton singleInstance = new MySingleton();
>
>         /* Note that the constructor is 'private' so that
>          * it cannot be constructed by another class
>          */
>         private MySingleton() {
>                 // constructor stuff
>         }
>
>         /* This is where other classes get a reference
>          * to the single instance
>          */
>         public MySingleton getInstance() {
>                 return singleInstance;
>         }
>
>         file://other methods
> }
>
> (Don't forget that because the single instance of this class could be used
> by lots of other objects, you need to be careful about concurrency.)
>
> Hope this is helpful, (maybe I misunderstood you question ?).
>
> Channing
>
> ===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff JSP-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to