Title: RE: Help on application scope

See notes below

> -----Original Message-----
> From: Michael Wentzel [mailto:[EMAIL PROTECTED]]
> Sent: Saturday, July 07, 2001 5:24 PM
> To: 'ericdev'
> Cc: '[EMAIL PROTECTED]'
> Subject: RE: Help on application scope
>
>
> >   I tried to do a Singleton Instance of a class containing a
> > Hashmap of
> > entried mapping IPs to session objects to be used later for session
> > invalidation. I instantiate the singleton instance with a
> > generic servlet,
> > but I dont think it worked when I started the server because
> > I didnt see my
> > println message when I started the server, and I can when I
> > call the servlet
> > explicitly. How do I know if I correctly setup the servlet to
> > startup when
> > the server starts?
>
> If you don't already you can put debug output into your Servlet's
> init() method.  The method gets called whenever an instance of a
> Servlet is created.
>
> > Also, having a singleton instance, do I still need to import
> > the class in
> > the jsp page I use it in? Should it find it anyways if I dont?
>
> Yes, even though you have an instance classes(jsp's are classes in
> the end) have to know where to look for the definition of
> other classes.
> Therefore you can either
> a) import the class
> b) refer to the fully qualified class name(including package)
> everytime
>    you refer to the class.
>
> > Also, when I do put it in the import, for some reason the
> > singleton instance
> > is intantiated twice in different JSP pages, thus furthering
> > my belief that
> > this class was created possibly in the first JSP page and
> > discarded, then
> > recreated in the second JSP page. Somebody help, This is
> > getting ridiculous
> > I originally tried this with application scope but it
> couldnt find the
> > instance in another jsp page though it was in the same application.
>
> The second instance is more than likely because of your
> servlet mapping.
> One instance is created for every mapping in you webapp plus
> one for the
> default /servlet/ServletName mapping.  You don't really need
> any mappings
> because you don't really need an HttpServlet if I understand
> you application
> appropriately.
>
> My idea went more along these lines....
> <code>
> public class MyServlet extends javax.servlet.GenericServlet {
>     public void init(ServletConfig config) throws ServletException {
>       this.config = config;
>       log("init");
>       this.init();
>     }
>
>     public void init() throws ServletException {
>       MyClass.initialize();
>     }
> }
>
> public class MyClass {
>     public static java.util.Map myMap = null;
>     public static void initialize() {
>       if (myMap == null) { myMap = new HashMap(); }
>       /* any other init goes here */
>     }
>
>     /* static accessor/modifier methods go here */
> }
> </code>
>
> Then your jsp's would import MyClass and reference the static
> accessor/
> modifier methods as necessary.  The static map will ensure
> that there is
> only one instance of the session mapping per JVM(note that this is
> significant since you can ONLY be assured that there is one
> instance of
> the map for each JVM, hence the importance of only having one servlet
> mapping for your servlet.)

Within Tomcat, static only guarntees one instance per CONTEXT, not jvm. This is due to the classloader(s) used to load your classes. For more info, see the following from the archives:

Classloader Explaination
http://www.mail-archive.com/[email protected]/msg13260.html

Discussion and testing of static members in different contexts:
http://www.mail-archive.com/[email protected]/msg23986.html
http://www.mail-archive.com/[email protected]/msg23998.html


>
> I don't know exactly what you're trying to accomplish in your
> app but if
> you are trying to manager HttpSession invalidation you might want to
> check out javax.servlet.http.HttpSessionBindingListener and
> the valueUnbound
> method in this class.
>
>
> ---
> Michael Wentzel
> Software Developer
> Software As We Think - http://www.aswethink.com
>

Charlie

Reply via email to