HttpSession getAttribute() 返回 null
Posted
技术标签:
【中文标题】HttpSession getAttribute() 返回 null【英文标题】:HttpSession getAttribute() returning null 【发布时间】:2015-12-19 01:50:01 【问题描述】:我正在尝试与 eclipse ide 进行 websocket 通信,当我运行我的代码时,我得到了 NullPointerException。我已经检查过了,getAttribute中的名称与bean中的名称相同
package ws;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.websocket.server.ServerEndpoint;
import fundstarter.model.ConnectToRMIBean;
import javax.servlet.http.HttpSession;
import javax.websocket.*;
@ServerEndpoint(value="/ws", configurator = HandShake.class)
public class WebSocketAnnotation
private Session session;
private ConnectToRMIBean sessionUser;
private HttpSession httpSession;
private static final Set<WebSocketAnnotation> myConnections = new CopyOnWriteArraySet<WebSocketAnnotation>();
public WebSocketAnnotation()
@OnOpen
public void start(Session session, EndpointConfig config)
this.session = session;
this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
myConnections.add(this);
this.sessionUser = (ConnectToRMIBean) httpSession.getAttribute("RMIBean");
//sendMessage("New message");
@OnClose
public void end()
// clean up once the WebSocket connection is closed
myConnections.remove(this);
@OnMessage
public void receiveMessage(String message)
sendMessage(message);
@OnError
public void handleError(Throwable t)
t.printStackTrace();
private void sendMessage(String text)
try
System.out.println("[WebSocketAnnot]RMIBean User Id -> " + this.sessionUser.getUserID());
this.session.getBasicRemote().sendText(text);
catch (IOException e)
try
this.session.close();
catch (IOException e1)
e1.printStackTrace();
谁能告诉我我的错误是什么?
【问题讨论】:
【参考方案1】:我发现了错误。这是 HandShake 上 modifyHandshake 方法的拼写错误
【讨论】:
以上是关于HttpSession getAttribute() 返回 null的主要内容,如果未能解决你的问题,请参考以下文章
Servlet第七课:ServletContext HttpSession 以及HttpServletRequest之间的关系
Servlet第七课:ServletContext HttpSession 以及HttpServletRequest之间的关系