1

First,I set attribute in servlet before websocket.

I'm sure the attribute is not null and print it on console.

session.setAttribute("user",request.getParameter("name"));

Then I establish websocket from HTML5 and get httpsession in modifyHandshake. But I get null.

public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator
{
    @Override
    public void modifyHandshake(ServerEndpointConfig config, 
                                HandshakeRequest request, 
                                HandshakeResponse response)
    {
        HttpSession httpSession = (HttpSession)request.getHttpSession();
            config.getUserProperties().put(HttpSession.class.getName(),httpSession);
        System.out.println("username:"+httpSession.getAttribute("user"));
    }
}

It print 'null' on console.

Why?

Jonny C
  • 1,943
  • 3
  • 20
  • 36
Jarviswu
  • 41
  • 1
  • 5
  • 1
    This might be a helpful read for you: [http://stackoverflow.com/questions/17936440/accessing-httpsession-from-httpservletrequest-in-a-web-socket-socketendpoint](http://stackoverflow.com/questions/17936440/accessing-httpsession-from-httpservletrequest-in-a-web-socket-socketendpoint) – jHilscher Jan 17 '15 at 16:13

0 Answers0