I am attempting to use a session object to store user-entered preferences in an application. I get a reference to a session object in the first of 3 servlets utilized in the app, no problem. I successfully setAttribute(). In a subsequent servlet (#2), I similarly get a reference to the session object created in servlet #1 (verified by getting and displaying session id). I can successfully set and getAttribute(). My problem is in calling servlet #3 (from the HTML form returned to client in servlet #2). When I attempt to get a reference to the existing session object, a new session object is created instead (again, as verified by getting and displaying session id). Subsequently, every attribute value is null when I attempt to retrieve it. I coded another, separate application servlet that I can run at this point (post creation of the new session object) which can successfull get a reference to the original session object, and get the attribute values which servlet #3 cannot. I've simplified servlet #3 so that all it does is simply get the session obj and return it in HTML to the client... it alway gets a new session id, attribute values are null. I'm a student and have tried everything I can to resolve the problem, but I think I may be out of my league. What really concerns me is that none of my instructors have any real insight into the problem. If I don't sound desperate, trust me, I am. Any insight/guidence would be most appreciated. A box of delicious girl scout cookies (without headers!) to anyone who might be able to shed some light!!! Running Tomcat 3.2.1, servlet API 2.2, java 1.3.1 on W2K. Others in the class have no problem. Server config? Well, thanks... Steve
If servlet 1 and 2 can see the session but session 3 can't, I would say that the servlet container considers that servlet 3 is not in the same "web application" as 1 and 2. Are all 3 defined in your web.xml for the application? Are all 3 addressed with the same basic URL? Are you using the "/servlet/" format for any URL? Bill
William's suggestion seems the most likely. The other possibility is if the session was invalidated for some reason. Look for calls in your code to session.invalidate() to see if you're doing it yourself. Other possibilities might include setting the session expiry time or inactive interval too short, thus allowing the servlet engine to invalidate the session before your request to servlet #3.
"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Thanks to you guys and the many others who have taken the time to post responses. After looking at the URL I coded to access each servlet (several times), I found what seems to have been the problem... a slight difference in the path. Namely, in the first two servlets: "ACTION=http://localhost:8080/....", Whereas in the third "ACTION=http://127.0.0.7:8080/...". Problem solved upon making them all consistant(agh!). I didn't 'see' the problem until Bill prompted me "...Are all 3 addressed with the same basic URL?". As I understand it, localhost is simply an alias for the loopback addr, so if anyone could explain why it doesn't work as I expected, that would be appreciated, but thanks again for all the comments and input. Now, we have Thin Mints, Peanut Butter, Suggared Shortbread...