is sessions variable and pageContext variable thread safe?

Discussions

Web tier: servlets, JSP, Web frameworks: is sessions variable and pageContext variable thread safe?

  1. I'm using JSPs and a custom tag.
    in the tag, there are 2 things
    1.get sessions variables and put them into a Map.
    2.get a remote interface of ejb

    the session map and ejb instance are put in pageContext by using pageContext.setAttribut()

    also,I defined the class extends TagExtraInfo to get these variables.

    in all of jsps ,we use this tag to get sessions variable(from map) and ejb instance to call ejb method.


    I just wondering if this is thread safe?I don't want sessions variables shared on web application.thank you
  2. Session Objects are created for each user seperately. How can one user access another user's session object? If you keep it in Application scope, it may be possible. But I think not with session objects.

    Senthil.
  3. thank you.session is not shared by others.but my point is that:
    in the tag,the session are put in a map,and map is put in pageContext,so that they can be accessed from jsp,my question is if pageContext could be shared by others?
    thank you
  4. Hi,
    If you are using a "Hashtable" as your Map your objects are already
    thread-safe. If you wanna make the entire JSP page threadsafe, do this at the start of the page :-

    <%@ page isThreadSafe="false" %>

    Could the others confirm this approach ?

    -MK.
  5. Hi zhang zhang,

    if your "session map" is not an instance variable of the tag, there should be no problem. I'm assuming you do something like get attributes from the session in the doEndTag() method of your tag like this:

    public int doEndtag()
    {
       ...
       Map sessionMap = new Hashtable();
       sessionMap.put("key1", session.getAttribute("differentKey1");
       ...
       pageContext.setAttribute("sessionMap", sessionMap);
       ...
    }

    Cheers,

    René
  6. Thank you.
    you almost get what I did!
    only diffrence is I use HashMap instead of HashTable,is this OK?