简体   繁体   English

如果用户在java中关闭浏览器,如何清除httpsession

[英]How to clear the httpsession, if the user close the browser in java

I am trying to clear the HttpSession if the consumer close the browser window. 如果消费者关闭浏览器窗口,我试图清除HttpSession。 I dont know how to do it, Please help me anyone 我不知道怎么做,请帮助我

Thanks & Regards Chakri 谢谢和问候Chakri

If you could get the browser to (reliably) notify the server that the user had closed the window, then the server could call session.invalidate() as per the original Answer provided by ejay_francisco. 如果您可以让浏览器(可靠地)通知服务器用户已关闭窗口,那么服务器可以根据ejay_francisco提供的原始答案调用session.invalidate()

The difficulty is getting the notification to happen reliably. 困难在于使通知可靠地发生。 There are various ways to detect the window close; 检测窗口关闭有多种方法; eg as covered by these Questions (and similar): 例如,这些问题所涵盖的(和类似的):

You could then write the (javascript) close event handler to send a specific request to the server. 然后,您可以编写(javascript)关闭事件处理程序以将特定请求发送到服务器。

However, I don't think any scheme is going to be able to deal with cases where the user's browser dies, the user's machine is shut down, and similar scenarios. 但是,我不认为任何方案都能够处理用户浏览器死机,用户机器关闭以及类似情况的情况。 So if you need the session to be cleared 100% of the time, then you are probably out of luck. 因此,如果您需要在100%的时间内清除会话,那么您可能运气不好。 I don't think it can be done. 我不认为可以做到。

It can be archived in a little diffrent way. 它可以以一种不同的方式存档。

  1. use a javascript event known as "window.onbeforeunload" 使用称为“window.onbeforeunload”的javascript事件

    eg window.onbeforeunload=mylogic_function(); 例如window.onbeforeunload = mylogic_function(); function mylogic_function() { window.location.href = "path to the servlet which inavalidate the session"; function mylogic_function(){window.location.href =“使会话无效的servlet的路径”; } }

  2. Have a dedicated servlet which job is only to deactivate the session 有一个专用的servlet,其工作只是停用会话

Try this ..hope this will work 试试这个..希望这会奏效

EDITED : 编辑:

try reading the answer on this Page 尝试阅读本页的答案

"HTTP is not a continuous-conversation protocol where the client connects to the server and they trade information asynchronously until the client disconnects. “HTTP不是连续会话协议,客户端连接到服务器,它们异步交换信息,直到客户端断开连接。

Instead, each HTTP client request acts like it logs in, does one thing, then logs out. 相反,每个HTTP客户端请求都像登录一样,做一件事,然后注销。 This request/response cycle repeats until the client stops sending requests. 此请求/响应周期重复,直到客户端停止发送请求。

At NO time is the server allowed to send a response to the client unless it's in response to a specific actual request and only one response is permitted per request. 在没有时间,服务器允许向客户端发送响应,除非它响应特定的实际请求,并且每个请求只允许一个响应。

So you cannot have a server post a "you timed out" page to the client. 因此,您无法让服务器向客户端发布“您超时”页面。 The closest you can get is to respond with a "you were timed out" response page if/when the client makes a request. 如果/当客户提出请求时,您可以获得的最接近的响应是“您已超时”响应页面。

The other common thing people want to do is to notify the server that the client has closed a browser, window, or tab. 人们想要做的另一件常见事情是通知服务器客户端已关闭浏览器,窗口或选项卡。 There is no protocol defined for the World Wide Web to deal with that. 没有为万维网定义协议来处理这个问题。 If the user closes a server window/tab, the server is not notified by the client. 如果用户关闭服务器窗口/选项卡,则客户端不会通知服务器。 Since there is no ongoing session to be dropped (remember, the connection only lasts long enough for the server to accept a request and return a response), there's no way the server will know that the client went away." 由于没有正在进行的会话被删除(请记住,连接只能持续足够长的时间以使服务器接受请求并返回响应),服务器无法知道客户端是否已经离开。“

SUMMARIZATION : 概述:

YOU CAN'T DO IT 你不能这样做

Possible Workaround : 可能的解决方法:

You simply need to rely upon the session timeout. 您只需要依赖会话超时。 The timeout could happen at any time and you cannot even know if the user is even looking at one of your pages when it occurs. 超时可能在任何时候发生,您甚至无法知道用户是否在发生其中一个页面时查看。 It's likely they're off looking at videos of kittens at the time. 很可能他们当时没看小猫的视频。

Short answer: you can't. 简短的回答:你做不到。 Fortunately, sessions expire automatically after a period of time; 幸运的是,会话会在一段时间后自动失效; check your servlet engine documentation to see how to set this timeout period. 检查servlet引擎文档以了解如何设置此超时期限。

I felt that there should be a way to do it from JavaScript; 我觉得应该有一种方法可以从JavaScript中做到这一点; unfortunately, JavaScript doesn't seem to have an onWindowClose handler. 遗憾的是,JavaScript似乎没有onWindowClose处理程序。

if you close the browser it is already clear the session 如果你关闭浏览器,它已经清除了会话

but if you want to force to clear the session 但是如果你想强制清除会话

use session.invalidate(); 使用session.invalidate(); or if you want to remove specific session use session.removeAttribute("<sessionName>"); 或者如果要删除特定会话,请使用session.removeAttribute("<sessionName>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM