title: How can I manually load a Java session using a JSESSIONID?
body: <p>I have a servlet which handles a multipart form post. The post is actually being made by a Flash file upload component embedded in the page. In some browsers, the Flash-generated POST doesn't include the JSESSIONID which is making it impossible for me to load certain information from the session during the post. </p>
  
  <p>The flash upload component does include cookie and session information within a special form field. Using this form field, I can actually retrieve the JSESSIONID value. The problem is, I don't know how to use this JSESSIONID value to manually load that specific session.</p>
  
  <p><strong>Edit -</strong> Based on ChssPly76's solution, I created the following <em>HttpSessionListener</em> implementation:</p>
  
  <pre><code>    @Override
      public void sessionCreated(final HttpSessionEvent se) {
          final HttpSession session = se.getSession();
          final ServletContext context = session.getServletContext();
          context.setAttribute(session.getId(), session);
      }
  
      @Override
      public void sessionDestroyed(final HttpSessionEvent se) {
          final HttpSession session = se.getSession();
          final ServletContext context = session.getServletContext();
          context.removeAttribute(session.getId());
      }
  </code></pre>
  
  <p>Which adds all sessions to the <strong>ServletContext</strong> as attributes mapped by their unique ids. I could put a Map of sessions in the context instead, but it seems redundant. Please post any thoughts on this decision. Next, I add the following method to my servlet to resolve the session by id:</p>
  
  <pre><code>    private HttpSession getSession(final String sessionId) {
          final ServletContext context = getServletContext();
          final HttpSession session = (HttpSession) context.getAttribute(sessionId);
          return session;
      }
  </code></pre>