众所周知,函数createWebSocketInbound(String subprotocol,HttpServletRequest request)有2个参数。
但是当我使用这个功能时,我无法获得与从参数“request”登录时输入的用户信息的会话。相反,当我使用request.getSession()时,我得到一个没有任何内容的新会话...如何在请求中获取用户登录信息或如何将我的登录信息传递给此函数?
我的代码:
@WebServlet(urlPatterns = { "/websocket"})
public class WebSocketWebSocketServlet extends WebSocketServlet {
private static final long serialVersionUID = 1L;
private AdminBean chatter; //Login information
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Object obj = request.getAttribute("admin"); //obj turns out to be null ...
super.doGet(request, response);
}
@Override
protected StreamInbound createWebSocketInbound(String subProtocol,HttpServletRequest request) {
System.out.println("Create a new Instance of WebSocketServlet");
HttpSession session = request.getSession();
this.chatter = (AdminBean) session.getAttribute("LoginUser"); //chatter turns out to be null ...
Object obj = request.getAttribute("admin"); //obj turns out to be null ...
return new WebSocketMessageInbound(chatter);
}
}