如果用户在java中关闭浏览器,如何清除httpsession
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24260271/
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 clear the httpsession, if the user close the browser in java
提问by chakri
I am trying to clear the HttpSession if the consumer close the browser window. I dont know how to do it, Please help me anyone
如果消费者关闭浏览器窗口,我会尝试清除 HttpSession。我不知道该怎么做,请帮助我
Thanks & Regards Chakri
感谢和问候 查克里
回答by Yaje
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.
Instead, each HTTP client request acts like it logs in, does one thing, then logs out. 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."
“HTTP 不是客户端连接到服务器并且它们异步交换信息直到客户端断开连接的连续对话协议。
相反,每个 HTTP 客户端请求就像它登录一样,做一件事,然后注销。这个请求/响应循环重复,直到客户端停止发送请求。
服务器在任何时候都不允许向客户端发送响应,除非它响应特定的实际请求,并且每个请求只允许一个响应。
因此,您不能让服务器向客户端发布“您超时”页面。如果/当客户端发出请求时,您可以获得的最接近的是响应“您已超时”响应页面。
人们想做的另一件事是通知服务器客户端已关闭浏览器、窗口或选项卡。没有为万维网定义的协议来处理这个问题。如果用户关闭服务器窗口/选项卡,则客户端不会通知服务器。由于没有正在进行的会话被丢弃(请记住,连接只持续足够长的时间让服务器接受请求并返回响应),服务器无法知道客户端已经离开。”
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.
您只需要依赖会话超时。超时可能随时发生,您甚至无法知道用户在发生时是否正在查看您的页面之一。很可能他们当时不看小猫的视频。
回答by Stephen C
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.
如果您可以让浏览器(可靠地)通知服务器用户已关闭窗口,那么服务器可以session.invalidate()
根据 ejay_francisco 提供的原始答案进行调用。
The difficulty is getting the notification to happen reliably. There are various ways to detect the window close; e.g. as covered by these Questions (and similar):
困难在于让通知可靠地发生。检测窗口关闭的方法有多种;例如,这些问题(和类似问题)所涵盖的内容:
- Trying to detect browser close event
- javascript to check when the browser window is close
- javascript detect browser close tab/close browser
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. I don't think it can be done.
但是,我认为没有任何方案能够处理用户浏览器死机、用户机器关闭等类似情况。因此,如果您需要 100% 的时间清除会话,那么您可能不走运。我不认为这是可以做到的。
回答by wrecklez
if you close the browser it is already clear the session
如果您关闭浏览器,它已经清除了会话
but if you want to force to clear the session
但如果你想强制清除会话
usesession.invalidate();
or if you want to remove specific session use session.removeAttribute("<sessionName>");
使用session.invalidate();
或者如果你想删除特定的会话使用session.removeAttribute("<sessionName>");
回答by Girdhar Singh Rathore
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; unfortunately, JavaScript doesn't seem to have an onWindowClose handler.
我觉得应该有一种方法可以从 JavaScript 中做到这一点;不幸的是,JavaScript 似乎没有 onWindowClose 处理程序。
回答by Diptiranjan Sahoo
It can be archived in a little diffrent way.
它可以以稍微不同的方式存档。
use a javascript event known as "window.onbeforeunload"
e.g window.onbeforeunload=mylogic_function(); function mylogic_function() { window.location.href = "path to the servlet which inavalidate the session"; }
- Have a dedicated servlet which job is only to deactivate the session
使用名为“window.onbeforeunload”的 javascript 事件
例如 window.onbeforeunload=mylogic_function(); function mylogic_function() { window.location.href = "使会话无效的 servlet 的路径"; }
- 有一个专门的 servlet,它的工作只是停用会话
Try this ..hope this will work
试试这个..希望这会奏效