Williams, Allen wrote:
Thanks, Dave & Frank.I've followed both your suggestions. The very first lines in the servlet print out the various parameter, attributes, & cookies. Here's what I get:
Have you used a standard JS framework of some sort, or is it in-house code? Testing with Firebug is, as has been stated, the way to find out what's being sent by your browser & therefore what the JS code you're using is actually doing.
I concur with the chaps who've already posted - in that your AJAX is probably not sending the cookie data.
You could manually attach the session id in your JS. We use the following snippet to provide access to that data in JS, for some parts of one of our apps.
<script type="text/javascript"> var Session = { id : '${pageContext.session.id}', user : '${pageContext.request.remoteUser}' } </script> var url = path + ";jsessionid=" + Session.id + "?" + queryParams; p
-- QUOTE -- ****************** doPost entering May 21 16:16:23: Session Attributes Session Attributes May 21 16:16:23: Session isNew()= true May 21 16:16:23: No attributes in this scope May 21 16:16:23: Request Attributes Request Attributes May 21 16:16:23: No attributes in this scope May 21 16:16:23: Parameters Request Parameters owner = 6 ajaxreq = getfora type = 0 May 21 16:16:23: Cookies May 21 16:16:23: 'JSESSIONID'='97E2CA20966390CCBC851738E71F3053' May 21 16:16:23: Found 1 cookies -- END QUOTE -- As I said in my earlier post, the session being returned by getSession() is a new one; when I call it like getSession(false) it return a null session. Therefore, it contains no attributes. The parameters are, of course, exactly what I expected. As you can see, there is the JSESSIONID cookie, but, for some reason, the servlet isn't using it, or it is not valid. Again, according to Sun, everyone one of the API functions to manipulate session IDs are deprecated, so I can't even kludge it up. I have no idea if that is a valid session ID or not. Also, I used Firebug to examine the XMLHttpRequest. I'm not too sure what I'm looking for. There was no reference to cookies, and all the stuff that I set was OK. Anyway, it seems to be getting the cookie. Any ideas as to why the session wouldn't use this cookie, or why this cookie might be invalid? In the process of this debugging, I have seen two JSESSIONIDs come over, one valid, one not. Thanks again for the help.-----Original Message-----From: Frank W. Zammetti [mailto:[EMAIL PROTECTED] Sent: Monday, May 21, 2007 3:29 PMTo: Tomcat Users List Cc: users@tomcat.apache.org; [EMAIL PROTECTED] Subject: Re: Session IDs & XMLHttpRequestsI can say with 100% certainty that a servlet invoked with XMLHttpRequest**DOES** have the same access to server-side objects as a non-AJAXrequest. I say this based on two applications in production that do this all day long, one Struts-based, one not. I also say it based on a numberof other applications, some using other frameworks, some using plain servlets, all that do this as well, with no problems. Now, the two production apps, which are very much AJAX-based, not just using it here and there, are running on Websphere, so that leaves the possibility that there's something going on with Tomcat. However, I generally develop under Tomcat, including most of those other apps I mentioned, and never observed this problem.This isn't to say what your seeing isn't truly an issue, I have no doubtit is... but, the only difference I can conceive of, based on all thisexperience, between an AJAX request and a normal POST/GET, is the session cookie not being passed in with the AJAX request. I could believe that might happen, and I could also believe it may be different from browser to browser (don't misunderstand, I have no knowledge of this being the case,but it wouldn't shock me). As another poster suggested, I would begin by monitoring the requestsgoing across in Firefox with Firebug, and perhaps TamperData... you shouldbe able to see every detail of the request and response with those...compare an AJAX request with a plain form sumission or link click and see if you notice any difference... I'd bet dollars to donuts you'll find someheader missing, or something along those lines.But, unless there's some peculiarity to your server setup or environment, I can tell you for sure there's no fundamental difference to the serverbetween the two types of requests, and by extension, to the servlets/filters that execute to service the request. Frank -- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com AIM/Yahoo: fzammetti MSN: [EMAIL PROTECTED] Author of "Practical Ajax Projects With Java Technology" (2006, Apress, ISBN 1-59059-695-1) and "JavaScript, DOM Scripting and Ajax Projects" (2007, Apress, ISBN 1-59059-816-4) Java Web Parts - http://javawebparts.sourceforge.net Supplying the wheel, so you don't have to reinvent it! On Mon, May 21, 2007 2:33 pm, Williams, Allen wrote:I had posted this question to four different Java fora overfour daysand gotten zero replies, when it occurred to me how stupidnot to askthe community that wrote Tomcat. I was just going to postthis, whichis a summary that describes what I've found so far: -- QUOTE --In the interest of informing the community, I'm publishingthe resultsof four days of testing and debugging of XMLHttpRequestsand attributes.This has led me to the conclusion that servlets invoked with an XMLHttpRequest do not have the same access to server-side objects(actually, attributes) as those invoked via the normal URLmechanism. Idon't know why, because if I insert a filter, the filtergets executed,albeit the first time with the wrong session ID.I began this odyssey when a filter in place to check if auser's sessionhad timed out would fail the first time when invoked with anXMLHttpRequest, but would work each time thereafter. What Idiscoveredthere was that there were two JSESSIONID cookies stored andbeing sentin the browser and the jsp and other servlets were requesting the correct one. The xml request was not, it was requesting the (old? Idon't know) invalid JSESSIONID. One would think, "OK, I'lljust read thecookies in my servlet, check each ID withrequest.isRequestedSessionIdValid(), and force the rightone". Wrong.All of the http session APIs that allow one to manipulatethe session IDand force a good one are deprecated, according to Sun's website, so theprogrammer isn't allowed to find & use a good session ID.In order to progress while I waited vainly for a reply, Ijust removedthe filter from the servlet's path so it didn't invoke it.I want thefilter to check, but decided to move on in the meantime.That's when Idiscovered that, evidently, the servlet does not get avalid session IDeither. I had the following line in my XMLHttpRequest servlet: [code] HttpSession sess= req.getSession(); [/code] This seemed to execute and work fine, until I needed to accesssession-scoped attributes I had defined in other pages orservlets. Thewere repeatedly null. When I changed the above line to this: [code] HttpSession sess= req.getSession(false); [/code]the reason was apparent: the servlet was generating a brandnew sessionfor me. So, for some reason, XMLHttpRequests don't get the sametreatment that normal servlets get. I'm going to have to goand modify alot of code to pass stuff around as query parameters inURLs, which Ireally don't want to do for both aesthetic & securityreasons, but seeno alternative. Hopefully, there really is someone outthere that knowsmore about this than I do and can explain to the community& me what'sgoing on. -- END QUOTE --1. So the last paragraph has my main question in it: whycan't I accessattributes in a servlet invoked via an XMLHttpRequest?However, I havea another important question and a couple of ancillary ones as well:2. What is the difference in the servlet invocation betweena regularURL invocation & an XMLHttpInvocation?3. How can I get my filter to work? I. e., get it thecorrect sessionID?4. WHY are all the session manipulation APIs deprecated,and, at leastaccording to Sun's web site, not being replaced? It seemsunusual to beremoving control from the programmer/software engineerinstead of tryingto give him more control over his environment. With thoseAPIs I couldhave fixed this (well, kludged it up, anyway).If you need me to post any code, I'll be glad to, but it'sreally prettystraightforward. In fact, when I started this adventure,the servletwas empty except for print statements, and the filter hasbeen in place& working for months. Thanks!! anw---------------------------------------------------------------------To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]--------------------------------------------------------------------- To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]--------------------------------------------------------------------- To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
smime.p7s
Description: S/MIME Cryptographic Signature