简体   繁体   English

Servlet会话行为和Session.invalidate

[英]Servlet Session behavior and Session.invalidate

Suppose I have a web app with a servlet defined in web.xml. 假设我有一个Web应用程序,在web.xml中定义了一个servlet。

Then I deploy it on Tomcat. 然后,将其部署在Tomcat上。

Then I open my browser and go to the link to this servlet, it is invoked. 然后,我打开浏览器并转到该servlet的链接,该链接已被调用。

Then I close my browser window. 然后关闭浏览器窗口。

How Session behaves ? 会话如何表现 How is it created, destroyed in this case? 在这种情况下如何创建,销毁它?

if this servlet is "detached" from all the web app, and gets parameters only using post & get, so it does not need Session at all, should one use Session.invalidate at the end of doGet(), doPost() ? 如果此Servlet与所有Web应用程序“分离”,并且仅使用post&get获取参数,那么它根本不需要Session,应该在doGet(),doPost()末尾使用Session.invalidate吗?

The servlet container usually keeps track of session using either (1) a HTTP cookie or (2) adding an extra parameter jsessionid in each URL. Servlet容器通常使用(1)HTTP cookie或(2)在每个URL中添加额外的参数jsessionid跟踪会话。

When a user access this site and no session exist already, a new one is created for him, including the corresponding HttpSession . 当用户访问此站点且尚不存在会话时,将为他创建一个新会话,包括相应的HttpSession If necessary, the user might be redirected to a login page. 如有必要,可以将用户重定向到登录页面。

The effect of Session.invalidate will basically be: "Discard the current session for this user. If he access another page on the site, a new session will be created". Session.invalidate的作用基本上是:“为此用户丢弃当前会话。如果他访问站点上的另一个页面,则将创建一个新会话”。

So far I know, session invalidation is used typically to implement logout feature. 到目前为止,我知道,会话无效通常用于实现注销功能。

I wouldn't call Session.invalidate in your "detached" servlet, it will interfere with the other pages. 我不会在您的“分离的” servlet中调用Session.invalidate ,它将干扰其他页面。 Basically, you don't care about the session in your servlet, you don't use it anyway. 基本上,您不需要关心servlet中的会话,也不会使用它。

Maybe have also a look at this question about disabling the session . 也许还有一个关于禁用会话的问题

Then I close my browser window. 然后关闭浏览器窗口。 How Session behaves ? 会话如何表现? How is it created, destroyed in this case? 在这种情况下如何创建,销毁它?

Are you asking what happens if the browser is closed even before the response is received back at the client? 您是否在询问是否在客户端收到响应之前关闭浏览器会发生什么情况?

In such a case, a Session will still be created on the server. 在这种情况下, Session仍将在服务器上创建。 It will persist for a specified time period and then expire. 它将持续指定的时间段,然后到期。

The next request from your browser will create a new Session . 来自浏览器的下一个请求将创建一个新的Session See more on this here: http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpSession.html 在此处查看更多信息: http : //tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpSession.html

Regarding session.invalidate - ewernli has already answered. 关于session.invalidate-ewernli已经回答。

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

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