WebSocket获取httpSession空指针异常的解决办法
来源:互联网 发布:大非农数据一定准确吗 编辑:程序博客网 时间:2024/06/16 17:14
小坑:使用requestListner解决不了这个问题!
如何获取HttpSession
在使用webSocket实现p2p或者一对多聊天功能的时候我们经常会有这样的需求:webSocket服务端需要获取到用户使用数据库的用户信息登录后的HttpSession获取个人资料信息。
于是,你会使用这样的代码:
package com.xinyulee.ws;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;/** * Created by zipple on 2017/11/14. * 协助server获取http session */public class HttpSessionWSHelper extends Configurator { @Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { System.out.println("调用modifyHandshake方法..."); HttpSession session = (HttpSession) request.getHttpSession();//session有可能为空 if (session!=null){ System.out.println("获取到session id:"+session.getId()); sec.getUserProperties().put(HttpSession.class.getName(),session); }else{ System.out.println("modifyHandshake 获取到null session"); } }}
然后在服务端这样配置:
@ServerEndpoint(value ="/chatRoom/{username}",configurator=HttpSessionWSHelper.class,encoders = {ServerEncoder.class})//encoders = {ServerEncoder.class}用于指定sendObject()方法调用的解析器
RequestListner解决不了这个问题的原因
在使用上述方法配置完成以后我满怀信心的进行测试,但是意外的发现了Tomcat Localhost Log下报了null pointer exception。
经过调试,发现了modifyHandshake方法并不能获取到HttpSession,在上述HttpSessionWSHelper 类代码中可以看到我对它进行了判空处理。但是这样并不能起到什么实际上的作用。我们需要弄明白为什么HandshakeRequest 获取不到HttpSession。
一开始,我试着去百度了一下,发现有这样的代码:
package com.xinyulee.listener;import com.xinyulee.util.Log;import javax.servlet.ServletRequestEvent;import javax.servlet.ServletRequestListener;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpServletRequest;/** * Created by zipple on 2017/11/14. */@WebListenerpublic class RequestListener implements ServletRequestListener { public void requestInitialized(ServletRequestEvent sre) { //将所有request请求都携带上httpSession----这样会创建新的session!导致重新创建了会话 Log.info("request--:"+sre.getServletRequest().getLocalAddr()); ((HttpServletRequest) sre.getServletRequest()).getSession(); } public RequestListener() { // TODO Auto-generated constructor stub } public void requestDestroyed(ServletRequestEvent arg0) { // TODO Auto-generated method stub }}
网上解释说,是因为进行ws连接的时候并没有HttpSession处于激活状态,于是便不能获取到HttpSession。他们给出的解决办法是对每一个request请求调用sre.getServletRequest()).getSession()方法。
这样的确可以解决空指针异常的问题,但是新问题又出现了。
在没有HttpSession激活状态的时候,使用getSession()方法会新建一个HttpSession,也就是说实际上这个监听器是在没有激活HttpSession的情况下不断新建会话。
这样已经完全偏离了我们程序的需求:获取同时在线的HttpSession中保存的个人信息。
那么怎么办呢?
事实上,这个空指针异常的根源并不是出在我们后端代码上。
localhost和127.0.0.1其实并不是同一个连接
在前端连接WebSocket的时候,我的代码是这样的:
loadWS("ws://127.0.0.1/chatRoom/null");
然而浏览器地址栏是这样的:
http://localhost:8080/
网上解释说如果不使用同一个host,则会创建不同的连接请求。具体细节有兴趣的朋友可以去了解一下。
于是我们可以这样拼接一下:
var host = window.location.host; var url = "ws://"+host+"/chatRoom/null";
这样便可以正常建立ws请求,并且能够使用自定义的HttpSessionWSHelper 类获取到HttpSession了。
- WebSocket获取httpSession空指针异常的解决办法
- spring websocket 获取httpsession
- WebSocket之获取HttpSession
- webSocket获取HttpSession
- webSocket获取httpSession
- WebSocket之获取HttpSession
- java WebSocket 获取httpSession
- Eclipse Plugin 获取图片资源返回空指针异常错误的解决办法。
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- java 空指针异常解决办法
- Tomcat下WebSocket获取HttpSession
- EJB注入后的空指针异常(NullPointerException)解决办法
- Android中关于空指针异常的解决办法
- spring+mybatis+WebService发布webservice接口
- 第六单元作业
- 装载问题
- linux内核结构完整视图
- iOS开发,初探离屏渲染
- WebSocket获取httpSession空指针异常的解决办法
- 在虚拟机中安装CentOS7
- day-22-SQL语句-事务-软件分层
- Java 自动装箱与拆箱
- Unity3D
- C语言实验——判断素数(循环结构)
- BST 二叉搜索树 C++ 算法导论
- python中字母与ascii码的相互转换
- day-23-SQL语句_综合案例