You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jonathan Wilson <jn...@dallas.photronics.com> on 2005/01/17 23:08:15 UTC

Servlet static variable not accessible from init() ??

I'm setting a servlets private static variable from within itself  via 
the contextInitialized(SCE) method but it's not accessible from the 
init() method once that get's called...I would have thought the static 
part would make that variable accessible to all thread of the servlet 
that was started...or is something else going on? Any help is appreciated.

public class controller extends HttpServlet implements 
ServletContextListener {
    private static Logger cblog = null;

    public void init()
    {
        System.out.println("cblog="+cblog);
    }

    public void contextInitialized(ServletContextEvent sce) {
       cblog = Logger.getLogger("cb");
       System.out.println("cblog="+cblog);
    }

<snip>
}

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Servlet static variable not accessible from init() ??

Posted by QM <qm...@brandxdev.net>.
On Mon, Jan 17, 2005 at 05:41:48PM -0600, Jonathan Wilson wrote:
: Thanks for responding.

Not a problem.  You're quite welcome.


: (Note to self: weird behaviour == restart IDE)

You've stumbled onto the primary reason I'm not a fan of IDE/container
integration.  =)  Too much weirdness.  I suppose that makes me very
old-school.


: By "store in application scope" you mean: 
: getServletContext().setAttribute(string, object) right??

Correct!

: This is then accessible via 
: (object)getServletContext().getAttribute(string)

Ditto.


: had static fields set they would still be set and accessible via 
: objectclassname.<i>public static string object.getSetting()</i>

Once you fetch the object from the ServletContext, it's like any other
object.  You downcast it to the proper type and go to work.

What makes the ServletContext just a little better than a singleton
(which is another way of making an object globally accessible) is that
there's now loose coupling between the object's class and its access:
client code (other objects) use the ServletContext as the single point
of contact.  If the type of object changes, client code doesn't have to
know...

-QM

-- 

software  -- http://www.brandxdev.net
tech news -- http://www.RoarNetworX.com


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Servlet static variable not accessible from init() ??

Posted by Jonathan Wilson <jn...@dallas.photronics.com>.
Thanks for responding. I modified the init() method of the servlet to 
redo everything that was being done in the contextInitialized() method 
to workaround the problem. The Listener was getting fired. A while after 
that NetBeans 'ran out of memory' so I restarted it. Now, after 
commenting out the workaround, I am unable to duplicate this problem. 
cblog is always set in the init() method now (like I was expecting). 
I'll leave the workaround code commented out just to see if my problem 
returns later. (Note to self: weird behaviour == restart IDE)

By "store in application scope" you mean: 
getServletContext().setAttribute(string, object) right??
This is then accessible via 
(object)getServletContext().getAttribute(string) and/or if the object 
had static fields set they would still be set and accessible via 
objectclassname.<i>public static string object.getSetting()</i>

Thanks again for responding,
    JW

QM wrote:

>On Mon, Jan 17, 2005 at 04:08:15PM -0600, Jonathan Wilson wrote:
>: I'm setting a servlets private static variable from within itself  via 
>: the contextInitialized(SCE) method but it's not accessible from the 
>: init() method once that get's called...I would have thought the static 
>: part would make that variable accessible to all thread of the servlet 
>: that was started...or is something else going on? Any help is appreciated.
>
>I didn't look all that closely for the bug itself, but this line caught
>my ele:
>
>: public class controller extends HttpServlet implements 
>: ServletContextListener {
>
>Any reason you're putting both in the same class?  You could have one
>ContextListener class instantiate a variable and store it in Application
>scope; then, the servlet could retrieve it in its init() method.  (Be
>sure to use the init( ServletConfig ) overload in that case.)
>
>Are you sure the ContextListener is being fired? (i.e. that it's been
>properly registered?)
>
>Another question: I don't have the JavaDoc in front of me, but does
>ContextListener (or any of its parent classes) have an init() method?
>
>-QM
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Servlet static variable not accessible from init() ??

Posted by QM <qm...@brandxdev.net>.
On Mon, Jan 17, 2005 at 04:08:15PM -0600, Jonathan Wilson wrote:
: I'm setting a servlets private static variable from within itself  via 
: the contextInitialized(SCE) method but it's not accessible from the 
: init() method once that get's called...I would have thought the static 
: part would make that variable accessible to all thread of the servlet 
: that was started...or is something else going on? Any help is appreciated.

I didn't look all that closely for the bug itself, but this line caught
my ele:

: public class controller extends HttpServlet implements 
: ServletContextListener {

Any reason you're putting both in the same class?  You could have one
ContextListener class instantiate a variable and store it in Application
scope; then, the servlet could retrieve it in its init() method.  (Be
sure to use the init( ServletConfig ) overload in that case.)

Are you sure the ContextListener is being fired? (i.e. that it's been
properly registered?)

Another question: I don't have the JavaDoc in front of me, but does
ContextListener (or any of its parent classes) have an init() method?

-QM

-- 

software  -- http://www.brandxdev.net
tech news -- http://www.RoarNetworX.com


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org