且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSP Session.getAttribute()值返回null

更新时间:2023-10-23 22:55:34

我不知道为什么我的session.getAttribute("user")返回的不是空值,而是属性值在几分钟后重新调整为空值

好,因此属性"user"为空.

Ok, so attribute "user" becomes null.

这可能是由于会话超时导致的,但是为什么只有属性值返回null,而其他会话仍然存在,即使session.getAttribute("user")也不返回null.

Que?所以属性用户"不为空?

Que?? So attribute "user" is not null??

更改会话属性只有两种可能性:

There are only two possibilities for changing session attributes:

  1. 如果会话超时:容器会同时清除所有属性.
  2. 如果您的会话没有超时,并且仅清除了一个属性:您的程序清除了一个属性.

附加说明A:来自javadoc的HttpServletResponse.sendRedirect:使用此方法后,应将响应视为已提交且不应写入.您上面的代码违反了此要求.

Extra Note A: from javadoc for HttpServletResponse.sendRedirect: After using this method, the response should be considered to be committed and should not be written to. Your code above violates this.

附加说明B:

   User user = new User();

您刚刚用一个新的,空的用户对象填充了它.避免这种情况-这是一个等待发生的错误.仅将变量设置为有用/有效值. :)

You just populated it with a rubbish new, empty user object. Avoid that - it's a bug waiting to happen. Only set variables to useful/valid values. :)