Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to collect them all in a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Map.html" rel="nofollow noreferrer"><code>Map</code></a> using a <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionListener.html" rel="nofollow noreferrer"><code>HttpSessionListener</code></a> yourself.</p> <pre><code>public class HttpSessionCollector implements HttpSessionListener { private static final Map&lt;String, HttpSession&gt; sessions = new HashMap&lt;String, HttpSession&gt;(); @Override public void sessionCreated(HttpSessionEvent event) { HttpSession session = event.getSession(); sessions.put(session.getId(), session); } @Override public void sessionDestroyed(HttpSessionEvent event) { sessions.remove(event.getSession().getId()); } public static HttpSession find(String sessionId) { return sessions.get(sessionId); } } </code></pre> <p>Just register it in <code>web.xml</code> as follows to run it:</p> <pre><code>&lt;listener&gt; &lt;listener-class&gt;com.example.HttpSessionCollector&lt;/listener-class&gt; &lt;/listener&gt; </code></pre> <p>Then, anywhere you want just do <code>HttpSessionCollector.find(sessionId)</code> to get the <code>HttpSession</code> in question.</p> <hr> <p>That said, this is a <strong>huge</strong> smell. There are certainly better ways to solve the <em>actual</em> functional requirement than this ;) As I commented in your <a href="https://stackoverflow.com/questions/3092917/how-can-i-get-the-tomcat-jsessionid-on-the-client-side">follow-up question</a>:</p> <blockquote> <p>This is the 2nd time that you asked a question which in real world should never be practiced. Honestly said, this all smells. What is it, the problem for which you think that getting the <code>HttpSession</code> associated with JSESSONID in server side and getting the JSESSIONID value in client side is "the" solution? Elaborate about this in a new question, you'll get answers how to do it the right way. </p> </blockquote> <p>Take it serious. We're not teasing you, we're just trying to help you in the right direction to avoid that your project/webapp will break due to security holes and bad practices and/or that you will get fired.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COMaybe I can explain why I found myself asking the same question and you can explain why this isn't what I should want: I assume that my server should store details in the HTTPSession, and return its sessionID (HttpSession.getId()) to the client, so the client can store it in a cookie. Then the client can provide the cookie to the server, a few days later, to be using that same session again. But that would need the server to be able to retrieve the HTTPSession based on that session ID, and/or somehow make that the active session. But I'm still exploring the whole idea. (I'm using GWT.)
      singulars
    2. CO@murrayc: by default, the session is timed out after 30 minutes of inactivity. See also http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909 and http://stackoverflow.com/questions/2185951/java-how-do-i-keep-a-user-logged-into-my-site-for-months/2186072#2186072
      singulars
    3. COAh, I now understand that HTTPSession (or its implementation in Tomcat, or something) automatically sets the JSESSIONID cookie, and later (but before the user has closed his browser) makes sure that the HTTPSession has that JSESSIONID from the cookie. So I don't need to do that. I also now understand that I wouldn't want to use the regular JSESSIONID cookie to persist a login across browser sessions, because a) that's not what it's for and b) that would not be efficient. So I'll use a separate cookie.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload