关闭浏览器时会话丢失 [英] Session Lost when closing the browser

查看:150
本文介绍了关闭浏览器时会话丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了会话时间。

 < session-config> 
< session-timeout> 11520< / session-timeout>

< / session-config> / p>

每次关闭浏览器并通过调用servlet再次打开浏览器时,我看到创建了新的会话。



我在tomcat / Java中是新的,但是如果我在ASP.NET环境中工作的话,就可以从HttpSessionListener中执行SessionCreated方法,我会努力设置cookie与会话名称相同的名称。



在Tomcat中解决它的最佳实践是什么?



预先感谢您。



Danny。

$ b $

b

这符合指定的行为。会话cookie没有年龄,所以只要客户端打开了webbrowser实例,或者直到客户端没有访问过网站,只要在 session-timeout 在服务器端设置。



你基本上需要一个比会话cookie长的cookie。您可以使用 <$创建一个新的长效cookie c $ c> Cookie API,使用 Cookie#setMaxAge() ,使用 HttpServletResponse#addCookie() 。在后续的HTTP请求中,您可以使用 HttpServletRequest#getCookies()



方式不是Tomcat具体的。您可以对每个其他servlet容器执行相同操作。


I setup my Session time out.

 <session-config>
<session-timeout>11520</session-timeout>

</session-config>

Each time when I close the browser and open it again by calling the servlet, I see that new session is created. It can be seen from SessionCreated method executed in HttpSessionListener each time when browser reopened.

I'm new in tomcat/Java, but if I were working in ASP.NET environment, I would work around it be setting cookie with the same name as session name.

What is the best practice to work around it in Tomcat?

thank you in advance.

Danny.

解决方案

Each time when I close the browser and open it again by calling the servlet, I see that new session is created.

That's conform the specified behaviour. The session cookie doesn't have an age, so it lives as long as the client has the webbrowser instance open or until the client hasn't visited the website for long as specified in the session-timeout setting in the server side.

You basically want a cookie which lives longer than the session cookie. You can create a new long-living cookie using Cookie API, set its age using Cookie#setMaxAge(), add it to the HTTP response using HttpServletResponse#addCookie(). On the subsequent HTTP requests you can determine the presence of the cookie using HttpServletRequest#getCookies().

This is by the way not Tomcat specific. You can do the same on every other servletcontainer.

这篇关于关闭浏览器时会话丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆