/****注意****/ 在 WEB.XML 文件中,我正在设置 session 超时属性, 所以可能是 session 已过期。 /************/
我正在使用 HttpSession 对象来管理 session
HttpSesion session = myPersonalMethodThatReturnHttpSessionObject();
//我正在使用 Eclipse,它在调试 View 中为我提供了以下详细信息,我将图像放在下面
//所以我怎样才能得到 isValid 字段或方法的值,所以我可以在这里输入 if 条件
if(session != null)
{
//removing attributes from session
}
/******************************************更多说明****** ************************************/
我的问题是... 注意 1 --> session 超时为 30 分钟。
Step1 some one login my web apllication
Step2 session is created.
Step3 if user close web application without signout
Step4 all session attribute is there
Step5 if another user try to login.
Step6 I try to remove all session attribute and store new attribute value.
Step7 Above functionality work properly but, while session is invalidate can't remove session attribute so i need to put condition in if session is valid that remove attribute else do nothing so I need to check session is valod or not.
最佳答案
由于您看到了 isValid=false
,我猜您的 session 已变得无效/超时。
您应该调用 HttpSession session = request.getSession(true);
或 HttpSession session = request.getSession();
以始终获得有效 session 。
request.getSession(true)
方法将确保在当前 session 无效时创建一个新 session 。如果当前 session 有效,它将返回相同的内容。
方法 request.getSession();
默认调用 request.getSession(true);
。
关于java - 检查 session 在 servlet 中是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25419004/