如果java中的用户不活动,如何使浏览器会话到期?

时间:2022-10-30 01:19:48

i want to expire the my browser session in case of user inactivity for 1 minutes in my web application? I tried doing two things :-

我希望在我的网络应用程序中用户不活动1分钟时使我的浏览器会话到期?我尝试做两件事: -

Approach1:- put the below code in web.xml

方法1: - 将以下代码放在web.xml中

<session-config>
  <session-timeout>1</session-timeout> 
</session-config>

Approach2:-

session.setMaxInactiveInterval(1*60);

Both the aproaches does not expire the session. I am ablke to continue even if user does not do the activity for 3 minutes?

这两个aproach都没有到期。即使用户不进行3分钟的活动,我仍然可以继续吗?

When it is not working:- it does not work when when i go to login screen directly and do the login.

当它不工作时: - 当我直接登录屏幕并进行登录时,它不起作用。

when it works:- Though same approach works when i integrate above web application to with one of mine different web application say webapp2. What happens in this case i provided a link on webapp2 which directly takes me to webapp1 without asking login credentials. Here log out works in case user does not do the activity for 1 minutes?

当它工作时: - 虽然我将上面的Web应用程序集成到我的一个不同的Web应用程序说webapp2时,同样的方法。在这种情况下会发生什么我在webapp2上提供了一个链接,直接将我带到webapp1而不询问登录凭据。如果用户不进行1分钟的活动,这里注销工作?

i am not getting why it is not working in first case when i do the log in manually though web.xml is same and class where i put session.setMaxInactiveInterval(1*60) is executed in both the cases?

虽然web.xml是相同的,并且在两种情况下都执行了session.setMaxInactiveInterval(1 * 60)的类,但我不知道为什么它在第一种情况下不能正常工作?

1 个解决方案

#1


0  

The session is timed out only when the server finds inactivity from the client side for a period of defined in web.xml / session / session-time-out. You should test if it is working or not by sending a request from client and after it gets the response, just close the browser window. Send one more request after session timeout interval. Check if it is a new session (check sessionID). Further help ?enter code here read below -

仅当服务器在web.xml / session / session-time-out中定义的一段时间内从客户端发现不活动时,会话才会超时。您应该通过从客户端发送请求并在获得响应后测试它是否正常工作,只需关闭浏览器窗口即可。在会话超时间隔后再发送一个请求。检查它是否是新会话(检查sessionID)。进一步的帮助?输入代码在这里阅读 -

Well there is a thing called web application listener for java based web applications. These listeners listens to requests, sessions and related things of the clients. For session timeout handling you can use session listeners. Step 1 - Modify web.xml to include a web application listener:

对于基于Java的Web应用程序,有一种叫做Web应用程序监听器的东西。这些侦听器侦听客户端的请求,会话和相关内容。对于会话超时处理,您可以使用会话侦听器。步骤1 - 修改web.xml以包含Web应用程序侦听器:

<listener>
    <listener-class>some.package.name.MySessionListener</listener-class>
</listener>

And, now create the above mentioned class in your source directory:

现在,在源目录中创建上面提到的类:

package some.package.name;

public class SessionListener implements javax.servlet.http.HttpSessionListener {

    public void sessionCreated(HttpSessionEvent arg0) {
        //write your own code for when new session is created.
    }

    public void sessionDestroyed(HttpSessionEvent arg0) {
        //Write your own c`enter code here`ode for when session is destroyed.
    }
}

Hope you find it useful. :) .

希望你觉得它有用。 :)。

#1


0  

The session is timed out only when the server finds inactivity from the client side for a period of defined in web.xml / session / session-time-out. You should test if it is working or not by sending a request from client and after it gets the response, just close the browser window. Send one more request after session timeout interval. Check if it is a new session (check sessionID). Further help ?enter code here read below -

仅当服务器在web.xml / session / session-time-out中定义的一段时间内从客户端发现不活动时,会话才会超时。您应该通过从客户端发送请求并在获得响应后测试它是否正常工作,只需关闭浏览器窗口即可。在会话超时间隔后再发送一个请求。检查它是否是新会话(检查sessionID)。进一步的帮助?输入代码在这里阅读 -

Well there is a thing called web application listener for java based web applications. These listeners listens to requests, sessions and related things of the clients. For session timeout handling you can use session listeners. Step 1 - Modify web.xml to include a web application listener:

对于基于Java的Web应用程序,有一种叫做Web应用程序监听器的东西。这些侦听器侦听客户端的请求,会话和相关内容。对于会话超时处理,您可以使用会话侦听器。步骤1 - 修改web.xml以包含Web应用程序侦听器:

<listener>
    <listener-class>some.package.name.MySessionListener</listener-class>
</listener>

And, now create the above mentioned class in your source directory:

现在,在源目录中创建上面提到的类:

package some.package.name;

public class SessionListener implements javax.servlet.http.HttpSessionListener {

    public void sessionCreated(HttpSessionEvent arg0) {
        //write your own code for when new session is created.
    }

    public void sessionDestroyed(HttpSessionEvent arg0) {
        //Write your own c`enter code here`ode for when session is destroyed.
    }
}

Hope you find it useful. :) .

希望你觉得它有用。 :)。