WebSocket之获取HttpSession
2018-03-25 11:53
323 查看
由于WebSocket与Http协议的不同,故在使用常用的HttpSession方面就存在了一些问题。通过google翻阅到了在onOpen方法下使用HttpSession的方法。新建一个GetHttpSessionConfigurator类并继承Configurator类
然后在@ServerEndpoint注解里面添加configurator属性
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); } }
然后在@ServerEndpoint注解里面添加configurator属性
@ServerEndpoint(value="/socketTest",configurator=GetHttpSessionConfigurator.class)在onOpen方法里加入参数EndpointConfig config即可获取HttpSession
@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); }来自:http://www.cnblogs.com/zhaoww/p/5119706.html?utm_source=tuicool&utm_medium=referral
相关文章推荐
- WebSocket之获取HttpSession
- java使用Websocket获取HttpSession出现的问题与解决
- webSocket获取httpSession
- java使用websocket,并且获取HttpSession 源码分析(推荐)
- java使用websocket,并且获取HttpSession,源码分析
- WebSocket获取httpSession空指针异常的解决办法
- webSocket获取HttpSession
- WebSocket之获取HttpSession
- WebSocket之获取HttpSession
- Tomcat下WebSocket获取HttpSession
- spring websocket 获取httpsession
- websocket中获取httpsession
- websocket获取httpsession报NullPointerException解决办法
- 在spring3.2.0+hibernate3.3.2+struts2 2.2.3中使用websocket,运行环境是tomcat8.0.36,获取HttpSession
- Spring MVC中如何通过RequestContextHolder类获取HttpServletRequest和HttpSession
- WebSocket入门教程(四)-- WebSocket实例:实时获取服务器内存使用情况(优化一)
- 火币网行情获取的websocket客户端
- springMVC(spring)+WebSocket案例(获取请求参数)
- jsp获取websocket的远程服务器地址
- JSF(Java Server Face) 框架中如何获取传统的HttpSession或HttpServletRequest对象