I'm using a context listener to instantiate an object and set it as an attribute of the context. In a jsp, I want to use the jspInit method to retrieve that object and hold it in a field for requests to use. (Using Tomcat 5.0, via Eclipse). But, the object is not there when jspInit runs.
I printed the enumeration of attribute names from my listener, after adding the object, and confirmed that it is there at that point (plus a few other items). When I do the same in jspInit, the enumeration does not include my object, but does include a few new items that weren't there for the context listener.
So, I went a step further, and printed the hashCode of the context object in each, and got different values.*
Why am I getting a different context object in the event listener than I get in jspInit?*
I am accessing the context object through the event object's getServletContext method, and in the jspInit via this.getServletConfig().getServletContext() (also tried this.getServletContext(), with the same result)
* I am amending this post to note that the hashCode comparison is not a valid test for object identity if the class has overridden the hashCode method.
perhaps the container is initializing the servlet before calling the context listener lifecycle method.
Joined: Dec 04, 2008
I took another look at the output - the stuff I printed from context listener appears when I restart the server, and then nothing else happens until I request the page. At that time, the printout from jspInit appears. So the sequence of events seems correct.
I had noticed before, when I got null pointer exceptions from the unfound object, that it actually seemed to run jspInit three times. I wonder if that is due to it first attempting to deserialize and init a stored instance, then attempting to instantiate a new one when the exception occurred. But that doesn't explain the third attempt.
first of all , why you are using a jsp for this?, use servlet .
Joined: Dec 04, 2008
seetharaman venkatasamy wrote:first of all , why you are using a jsp for this?, use servlet .
I hadn't yet said what "this" is, so that question really isn't valid. The task I'm eventually going to perform is pretty much just presentation. The object I'm trying to access from the context has global data that I'm using to help accomplish that. I figured it would be more efficient to store the reference once rather than have each call retrieve it.
I tried moving the code to retrieve the object to the service method as a scriptlet, and suddenly it started working, even when I then moved the code back to the jspInit method. I hate when that happens.
Also, I realized that my hashCode check wasn't necessarily valid - I was assuming that the default (inherited from Object) hashCode algorithm was in place. But, if ServletContext overrides that, then it could change. In my now working version I get a different hashCode from my context listener than from the jsp, but my enumerations show the same contents, at least by name.