1

hi I am querying database to load all items based on some criteria and setting this result in session as

data = service.getData();
session.setAttribute("data", data);

now I am trying to access this data via an Ajax call and my Ajax call is served by a different servlet rather then which fetched the data from DB.

Ajax call using jquery

$.ajax({
    type: "POST",
    url: "/com/tp/AjaxXML.jsp",
    data: ({cmd: "report"}),
    dataType: "xml",
    success: function(xml) {
        $(xml).find('site').each(function(){
            var url = $(this).attr('url');                                  
        });
    }
});

in my AjaxXML.jsp

I am doing

if("report".equals(cmd)){
    List<Object> data = (List<Object>)request.getSession().getAttribute("data");

    if(data == null){
        System.out.println("data is null ");
    }
}

every time I am getting the data as null via the Ajax call how ever I try to access the session data normally from my first servlet it works.

could someone let me know if I am doing something wrong?

I noticed one more thing when we do session.getId(); and pageContext.getSession().getId(); both of them are returning different Id's? Is this expected to me they should be same anyone differ on that?

Michaël
  • 3,679
  • 7
  • 39
  • 64
dpsdce
  • 5,290
  • 9
  • 45
  • 58
  • You need to show more codes that surround both lines of codes you have posted. – Bhesh Gurung Dec 14 '11 at 06:06
  • @gurung i think rest of the code is irrelevant i just want to know whether we can access the data set in session via a ajax call? – dpsdce Dec 14 '11 at 06:07
  • Try with `request.getSession(false)` in the second case. – Bhesh Gurung Dec 14 '11 at 06:10
  • You might want to check this out: http://stackoverflow.com/questions/5619277/get-session-by-id-in-ajax-call – Bhesh Gurung Dec 14 '11 at 06:14
  • this is still not working with request.getSession(false), the link refers to Php however m facing this problem in J2EE – dpsdce Dec 14 '11 at 06:19
  • See if the sessionid (should be jsessionid...) is also sent with the request using something like firebug. – Bhesh Gurung Dec 14 '11 at 06:22
  • its set in the cookie JSESSIONID=AAE75FA4060A5789AD22C0E65C60141B and is sent along with the request; – dpsdce Dec 14 '11 at 09:07
  • @gurung: trying `request.getSession(false)` is nonsense. It'll return `null` if session doesn't exist. – BalusC Dec 14 '11 at 15:08
  • @BalusC: Yes, I was just trying to see if in case the session created by the first servlet was somehow destroyed. And that would mean the second servlet was also creating a new session. – Bhesh Gurung Dec 14 '11 at 17:27

1 Answers1

1

I noticed one more thing when we do session.getId(); and pageContext.getSession().getId(); both of them are returning different Id's? Is this expected to me they should be same anyone differ on that?

No, they should definitely not differ. I however assume that you have examined them within the same request. If you examined them in different requests, then the difference can be explained by the absence of the proper JSESSIONID cookie. Cookies are domain and context specific. You're apparently sending the ajax request to a different domain/context. The leading slash / in the ajax URL also confirms this less or more. Make sure that you're sending it to the same domain/context. Use a context-relative URL, something like as url: "AjaxXML.jsp" and move the code to the same domain/context, or turn on session sharing between different contexts at server level.


Unrelated to the concrete problem, doing the Ajax response handling in a JSP is a bad idea. Rather do it in a servlet.

Replace

url: "/com/tp/AjaxXML.jsp",

by

url: "/com/tp/AjaxXML",

and put the code in doPost() method of the servlet which is mapped on that URL pattern.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555