java - 检查 session 在 servlet 中是否有效

标签 java session servlets

/****注意****/ 在 WEB.XML 文件中,我正在设置 session 超时属性, 所以可能是 session 已过期。 /************/

我正在使用 HttpSession 对象来管理 session

HttpSesion session = myPersonalMethodThatReturnHttpSessionObject();

//我正在使用 Eclipse,它在调试 View 中为我提供了以下详细信息,我将图像放在下面

In this Image session has field isValid

//所以我怎样才能得到 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/

相关文章:

java - Accept-Ranges 是否可以与 GWT 中的 RequestBuilder 一起使用?

java - 在图像中寻找模式

session - 如何保护经典 ASP ASPSESSIONID cookie 的安全?

laravel - Laravel 存储用户 session 的位置

java - 从 Maven 资源文件夹获取正确的文件路径 - iText

java - Spring 网 : Getting file from web context using Resource?

java - java.lang.String 的值无法转换为 double

java - Netbeans 中 JFilechooser 的问题

java - 我在哪里安装 ubuntu 上的 jdbc 驱动程序?

php - 什么是/dev/arandom?