package per.zww.web;

import javax.servlet.http.HttpSession;

import javax.websocket.HandshakeResponse;

import javax.websocket.server.HandshakeRequest;

import javax.websocket.server.ServerEndpointConfig;

import javax.websocket.server.ServerEndpointConfig.Configurator;

/*

* 获取HttpSession

*

*/

public class GetHttpSessionConfigurator extends Configurator {

@Override

public void modifyHandshake(ServerEndpointConfig sec,

HandshakeRequest request, HandshakeResponse response) {

// TODO Auto-generated method stub

HttpSession httpSession=(HttpSession) request.getHttpSession();

sec.getUserProperties().put(HttpSession.class.getName(),httpSession);

}

}

第一步:新建一个GetHttpSessionConfigurator类并继承Configurator类@ServerEndpoint(value="/socketTest",configurator=GetHttpSessionConfigurator.class)

第二步:然后在@ServerEndpoint注解里面添加configurator属性@OnOpen

public void onOpen(Session session,EndpointConfig config) {

HttpSession httpSession= (HttpSession) config.getUserProperties().get(HttpSession.class.getName());

System.out.println( httpSession.getAttribute("name"));

sessionMap.put(session.getId(), session);

}

第三步:在onOpen方法里加入参数EndpointConfig config即可获取HttpSession

转自:http://www.cnblogs.com/zhaoww/p/5119706.html?utm_source=tuicool&utm_medium=referral

非常感谢!

更多推荐