在Endpoint中加入一个configurator,用来配置servlet
@ServerEndpoint(value=”/websocket/chat”,configurator=GetHttpSessionConfigurator.class)
新建一个类叫GetHttpSessionConfigurator,继承Configurator。
HttpSession httpSession=(HttpSession) request.getHttpSession();
sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
在 onStart方法中获取httpsession
public void start(Session session,EndpointConfig config) {
HttpSession httpSession= (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
}