1

众所周知,函数 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);
}

}

4

0 回答 0