Java 如果用户关闭浏览器,如何自动结束会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16272137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to end sessions automatically if user closes the browser
提问by
I need to end the session automatically when the user is inactive for some particular time say for 10 minutes. We have a method in
当用户在某个特定时间(例如 10 分钟)处于非活动状态时,我需要自动结束会话。我们有一个方法
HttpSession session=request.getSession();
session.setAttribute("User", au);
session.setAttribute("name", firstname);
response.sendRedirect("doLogin.jsp");
session.setMaxInterval();
but this will end the session even if the user is active for 10 minutes. How can I end the session when the user closes the browser?
但这将结束会话,即使用户活动了 10 分钟。当用户关闭浏览器时如何结束会话?
采纳答案by Suresh Atta
Avoid manual code for that.
避免为此手动编码。
simply in web.xml
//this will applied for whole application
简单地在 web.xml
//this will applied for whole application
<session-config>
<session-timeout>10</session-timeout>
</session-config>
Detect browser close event and call invalidatemethod
检测浏览器关闭事件并调用invalidate方法
if(session!=null) {
session.invalidate();
}
回答by Apurv
You can set the session time out as below in the ServletContextListener:
您可以在ServletContextListener 中如下设置会话超时:
session.setMaxInactiveInterval(15*60); //in seconds
This will give you the advantage that you can read the session timeout from any external properties file/ database and change the session timeout without changing the code.
这将为您提供优势,您可以从任何外部属性文件/数据库中读取会话超时并更改会话超时,而无需更改代码。
You can use the unload
event and send the logout request to the server. Or keep sending periodic requests to the server informing the user is still active.
您可以使用该unload
事件并将注销请求发送到服务器。或者继续向服务器发送定期请求,通知用户仍然处于活动状态。
回答by skuntsel
You need to set the session timeout, that is, the time after which current Session
object will be invalidated.
您需要设置会话超时,即当前Session
对象失效的时间。
This can be done either by setting timeout in your web.xml like:
这可以通过在 web.xml 中设置超时来完成,例如:
<session-config>
<session-timeout>20</session-timeout>
</session-config>
or programmatically call on the Session
object
或以编程方式调用Session
对象
session.setMaxInactiveInterval(valueInSeconds);
Keep an eye that session timeout period set up in web.xml is in minutes, and programmatically - in seconds.
请注意在 web.xml 中设置的会话超时时间以分钟为单位,以编程方式 - 以秒为单位。
回答by Kirubhananth Chellam
You can do that from web config file. A sample is here
您可以从 Web 配置文件中执行此操作。样本在这里
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
stateNetworkTimeout="10"
sqlConnectionString="data source=127.0.0.1;Integrated Security=SSPI"
sqlCommandTimeout="30"
customProvider=""
cookieless="UseCookies"
cookieName="ASP.NET_SessionId"
timeout="10"
allowCustomSqlDatabase="false"
regenerateExpiredSessionId="true"
partitionResolverType=""
useHostingIdentity="true">
<providers>
<clear />
</providers>
</sessionState>
The timeout property will specify the timeout period.
timeout 属性将指定超时时间。
回答by Kirubhananth Chellam
There is no way to intimate the server about the user closes the browser. That's why the sessions are having configurable timespan. If you wanna do it then try by creating a onclose javascript event and from there do an ajax call to intimate the session close to the server. In the server you can get the session id from this call as parameter and kill it.
没有办法让服务器知道用户关闭了浏览器。这就是会话具有可配置时间跨度的原因。如果你想这样做,那么尝试创建一个 onclose javascript 事件,然后从那里做一个 ajax 调用来接近服务器的会话。在服务器中,您可以从此调用中获取会话 ID 作为参数并终止它。
I didn't tried it. Don't think it is right to do.
我没试过。不要认为这样做是对的。
回答by Al-Kathiri Khalid
Question: How to end sessions automatically if user closes the browser?
Answer: Set max inactive intervaltime value less than 0.
问题:如果用户关闭浏览器,如何自动结束会话?
答:设置最大非活动间隔时间值小于 0。
Example:
例子:
HttpSession session = request.getSession();
session.setMaxInactiveInterval(-1);
session.setAttribute("User", au);
response.sendRedirect("doLogin.jsp");