java使用Websocket获取HttpSession出现的问题与解决
2017-12-19 09:20
381 查看
websocket的写法就不多说了,主要记一记其中出现的问题
1.获取不到httpSession
解决办法:先重写握手方法,将httpsession放入ServerEndpointConfig.getUserProperties()方法返回的map中
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; /** * 继承websocket配置类,将httpsession放入ServerEndpointConfig的map中 * 从而达到使websocket对象可以访问到httpsession中的对象 */ public class GetHttpSessionConfigurator extends Configurator{ /** * 重写修改握手方法 * @param sec * @param request * @param response */ @Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { HttpSession httpSession = (HttpSession)request.getHttpSession(); sec.getUserProperties().put(HttpSession.class.getName(),httpSession); } }
然后在注解@ServerEndpoint加入configurator的配置
@ServerEndpoint(value="/websocket",configurator=GetHttpSessionConfigurator.class)
此时已经可以获取到httpsession了。
然而如果我没有登录,反而需要临时登录这种情况呢?根据上述demo,会发现根本没办法连接上,一直是close状态
下面是对这个问题的解决
2、未登录用户连接WebSocket,一直close状态的情况解决:
解决方法是建立个请求监听器
@WebListener public class RequestListener implements ServletRequestListener { public void requestInitialized(ServletRequestEvent sre) { //将所有request请求都携带上httpSession ((HttpServletRequest) sre.getServletRequest()).getSession(); } public RequestListener() { // TODO Auto-generated constructor stub } public void requestDestroyed(ServletRequestEvent arg0) { // TODO Auto-generated method stub } }
问题解决。
相关文章推荐
- 使用android OpenGL时出现的java.lang.IllegalArgumentException: Must use a native order direct Buffer错误问题解决
- 使用myeclipse开发java,解决java中继承JFrame类出现The type JFrame is not accessible due to restriction的问题
- 解决使用maven的java web项目导入后出现的有关问题 -cannot be read or is not a valid ZIP file
- 基于CDH5.x 下面使用eclipse 操作hive 。使用java通过jdbc连接HIVESERVICE 创建表出现的问题及解决
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 解决2008系统中使用java的Date获取时间总是比实际时间差了8个小时的问题
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 解决使用maven的java web项目导入后出现的有关问题, -cannot be read or is not a valid zip file
- 使用eclipse创建多文件java工程出现找不到或无法加载主类问题解决
- 解决java使用sigar获取linux系统内存使用信息报错的问题
- JavaWeb学习(5.5)---在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- Java 简单解决springmvc获取properties文件里面中文内容出现论码问题(我遇到的问题是数据提交检验时返回的错误提示出现乱码)
- ubuntu下使用eclipse调试jni无法获取环境变量,本地库(java.library.path,LD_LIBRARY_PATH)等问题的解决。
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 解决Java获取文件路径出现乱码的问题
- 解决问题: 使用hadoop时出现:java.io.IOException: Bad connect ack with firstBadLink ...
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法