> -----Original Message-----
> From: Magnus Stenman [mailto:[EMAIL PROTECTED]]
> Sent: 21 July 1999 10:18
> To: Channing Walton; [EMAIL PROTECTED]
> Subject: SV: Re: Inter servlet comm and Session
>
>
> Hi, you forgot a "static" in the getter method,

Yup

> but I still think
> it's bad advice,

OK

> 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;
>     }
> }
>

So this ensures that there is a single instance of 'MyClass' being used
without actually making MyClass a singleton...much better.

Chang

===========================================================================
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