title: Accessing HttpSession inside an annotated @WebSocket class on Embedded Jetty 9
body: <p>How can I access a HttpSession object inside an annotated @WebSocket class in Jetty 9?</p>
  
  <p>I found how to do it using @ServerEndpoint annotation, like here: <a href="https://stackoverflow.com/questions/17936440/accessing-httpsession-from-httpservletrequest-in-a-web-socket-socketendpoint">HttpSession from @ServerEndpoint</a></p>
  
  <p>Using the @WebSocket annotation, like in the class bellow, how can I do it?</p>
  
  <pre><code>@WebSocket
  public class AuctionWebSocket {
  
      // NEED TO ACCESS HttpSession OBJECT INSIDE THESE METHODS:
  
      @OnWebSocketConnect
      public void onConnect(Session session) {
          System.out.println("onConnect...");        
      }
  
      @OnWebSocketMessage
      public void onMessage(String message) {        
          System.out.println("Message: " + message);
      }    
  
      @OnWebSocketClose
      public void onClose(int statusCode, String reason) {
          System.out.println("onClose...");        
      }
  
      @OnWebSocketError
      public void onError(Throwable t) {
          System.out.println("onError...");        
      }    
  }
  </code></pre>
  
  <p>Inside the method <code>onConnect(Session session)</code>, I tried to call <code>session.getUpgradeRequest().getSession()</code> which always returns <code>null</code>.</p>
  
  <p>For sake of information, here is how I start embedded Jetty 9:</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) throws Exception {
  
          String webPort = System.getenv("PORT");
          if (webPort == null || webPort.isEmpty()) {
              webPort = "8080";
          }
  
          Server server = new Server(Integer.parseInt(webPort));
  
          ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
          classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
                  "org.eclipse.jetty.annotations.AnnotationConfiguration");
  
          WebAppContext wac = new WebAppContext();
          String webappDirLocation = "./src/main/webapp/";
  
          wac.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/classes/.*");
          wac.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
          wac.setBaseResource(new ResourceCollection(new String[]{webappDirLocation, "./target"}));
          wac.setResourceAlias("/WEB-INF/classes/", "/classes/");
          wac.setContextPath("/");
          wac.setParentLoaderPriority(true);
  
          /*
           * WebSocket handler.
           */
          WebSocketHandler wsh = new WebSocketHandler() {
  
              @Override
              public void configure(WebSocketServletFactory wssf) {
                  wssf.register(AuctionWebSocket.class);
              }
          };
  
          ContextHandler wsc = new ContextHandler();
          wsc.setContextPath("/auction-notifications");
          wsc.setHandler(wsh);
  
          ContextHandlerCollection chc = new ContextHandlerCollection();
          chc.setHandlers(new Handler[]{wac, wsc});
  
          server.setHandler(chc);
  
          server.start();
          server.join();
      }
  
  }
  </code></pre>
  
  <p>Let me know if you need more information.</p>
  
  <p>Any help will be appreciated.</p>
  
  <p>Thanks in advance.</p>