The fundamental problem I see with Singleton in Servlets is that
(a) static methods on the Servlet aren't going to provide per-session
information, because Servlets are muti-thread capable (unless flagged with
SingleThreadModel)
(b) you could place the Singleton up into session space, but that means that
(1) you have to pass HttpSession everywhere (and then the value of
Singleton falls out--the HttpSession now
*is* the Singleton) and
(2) any objects placed into session space should be Serializable, since
sessions will be serialized out between
requests/responses (a la JRun), and having a non-Serializable
object up there is recipe for disaster
(c) implementing SingleThreadModel might introduce performance/scalability
problems--it's cheaper to run Threads through a single instance than to have
multiple instances running around.
I'm thinking, though, that you *may* be able to play on a quirky fact of
Servlets to simulate something similar. See if this reasoning holds up:
(1) On entry to the Servlet, before doing anything else, place into a static
Map within the servlet (private static Map singletonSessionMap = new
HashMap(), declared on the Servlet itself) the HttpSession instance (or any
other container that holds the Singleton you're interested in), keyed by
"Thread.currentThread().toString()".
(2) Have the "instance()" method of the Singleton (which I'd place within
the Servlet, for convenience's sake) call into this static Map
(singletonSessionMap) under the key of the current Thread to find the
Singleton instance. Within the lifetime of the servlet request, *I BELIEVE*
you are guaranteed to always be on the same Thread, so even if multiple
Threads come in you're still getting the Singleton for that Thread.
(3) As the request finishes, remove the Singleton from the Map--a servlet
engine could use a Thread pool, so it's possible that the same Thread will
handle a request for a different session the next time around; thus, we
can't assume a Singleton-per-Thread arrangement.
I suppose I could crank out code to demonstrate this, but before I do, does
the logic seem faulty to anyone?
Ted Neward
Java Instructor, DevelopMentor ( http://www.develop.com )
http://www.javageeks.com/~tneward
-----Original Message-----
From: Alok Mehta <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: Monday, December 13, 1999 2:28 PM
Subject: What is the best way to pass singleton/global information within
classes/methods of a servlet?
>A general question about writing servlets...
>
>I would like to have a class or object in my servlet where I can go to find
>out misc. information
>- the HttpServletRequest object
>- authentication information (userid, permissions)
>- database connection information
>- audit management objects, and objects for loggin activity
>etc.
>
>If this was written as an application (instead of a servlet), then I could
>do this using the 'Singleton' design pattern, and create a single instance
>of a class. Or as static methods and variables of a class. But this won't
>work for a servlet, because what's really needed is one instance for each
>'request/session' that is being processed.
>
>I know that I can tie things together with cookies or HttpSession objects.
>But the problem is that I can't figure out a way around the fact that I
>will need a handle to at least one object (for example, a handle to an
>HttpSession object that could lead me to the other objects).
>
>I don't think this is elegant, because I'd still have to pass an instance
>of that object to every object/method that might need to get some
>information. There doesn't seem to be a global way to get it. This also
>means that many of my methods wouldn't even directly use the object, but
>would have to carry that handle around as a parameter anyway, in case the
>lower level methods need it.
>
>Imagine, for example, if every method you write in any Java application
>would need to have "System.out" passed to it, in case there is some method
>that might need to print to the terminal.
>
>Does anyone else feel this way? Is there a preferred solution to this? Or
>do people write simple servlets where this isn't much of an issue?
>
>Thanks
>Alok Mehta, Research Scientist
>Wadsworth Center, NYS Dept. of Health
>
>___________________________________________________________________________
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html