Java 中的会话管理是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3067383/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:10:48  来源:igfitidea点击:

What is session management in Java?

javajspsessionservletssession-management

提问by

I have faced this question in my Interview as well. I do have many confusion with Session Scope & it management in java.

我在面试中也遇到过这个问题。我对 Session Scope 和 java 中的 it 管理有很多困惑。

In web.xml we do have the entry :

在 web.xml 中,我们确实有条目:

<session-config>
        <session-timeout>
            30
        </session-timeout>
</session-config>

What does it indicate actually ? Is it scope of whole project ?

它实际上表明了什么?是整个项目的范围吗?

Another point confusing me is how can we separate the session scope of multiple request in the same project? Means if I am logging in from a PC & at the same time I am logging in from another PC, does it differentiate it ?

另一点让我困惑的是,我们如何在同一个项目中分离多个请求的会话范围?意思是如果我从一台 PC 登录 & 同时我从另一台 PC 登录,它会区分吗?

Also, another confusing thing is the browser difference. Why does the different Gmails possible to open in different browsers ? And Gmail can prevent a session from Login to Logout. How is it maintained with our personal web ?

此外,另一个令人困惑的事情是浏览器的差异。为什么不同的 Gmail 可以在不同的浏览器中打开?Gmail 可以阻止从登录到注销的会话。它是如何通过我们的个人网络维护的?

采纳答案by Bozho

Session management is not something limited to Java and servlets. Here's roughly how it happens:

会话管理不仅限于 Java 和 servlet。大致是这样发生的:

  1. The HTTP protocol is stateless, so the server and the browser should have a way of storing the identity of the user through multiple requests
  2. The browsers sends the first request to the server
  3. The server checks whether the browser has identified with the session cookie (see below)

    3.1. if the server doesn't 'know' the client:

    • the server creates a new unique identifier, and puts it in a Map (roughly), as a key, whose value is the newly created Session. It also sends a cookie response containing the unique identifier.

    • the browser stores the session cookie (with lifetime = the lifetime of the browser instance), containing the unique identifier, and uses it for each subsequent request to identify itself uniquely.

    3.2. if the server already knows the client - the server obtains the Session corresponding to the passed unique identifier found in the session cookie

  1. HTTP 协议是无状态的,因此服务器和浏览器应该有一种通过多次请求存储用户身份的方式
  2. 浏览器向服务器发送第一个请求
  3. 服务器检查浏览器是否已识别会话 cookie(见下文)

    3.1. 如果服务器不“知道”客户端:

    • 服务器创建一个新的唯一标识符,并将其放入一个 Map(大致)中,作为键,其值是新创建的 Session。它还发送包含唯一标识符的 cookie 响应。

    • 浏览器存储会话 cookie(生命周期 = 浏览器实例的生命周期),包含唯一标识符,并在每个后续请求中使用它来唯一标识自己。

    3.2. 如果服务器已经知道客户端 - 服务器获取与在会话 cookie 中找到的传递的唯一标识符相对应的会话

Now onto some the questions you have:

现在回答你的一些问题:

  • the session timeout is the time to live for each session map entry without being accessed. In other words, if a client does not send a request for 30 minutes (from your example), the session map will drop this entry, and even if the client identifies itself with the unique key in the session cookie, no data will be present on the server.

  • different gmails (and whatever site) can be opened in different browsers because the session cookie is per-browser. I.e. each browser identifies itself uniquely by either not sending the unique session id, or by sending one the server has generated for it.

  • logging from different PCs is the same actually - you don't share a session id

  • logging-out is actually removing the entry for the session id on the server.

  • 会话超时是每个会话映射条目未被访问的生存时间。换句话说,如果客户端在 30 分钟内(根据您的示例)未发送请求,会话映射将删除此条目,即使客户端使用会话 cookie 中的唯一键标识自己,也不会出现任何数据在服务器上。

  • 可以在不同的浏览器中打开不同的 gmail(以及任何站点),因为会话 cookie 是针对每个浏览器的。即,每个浏览器通过不发送唯一会话 ID 或发送服务器为其生成的会话 ID 来唯一标识自己。

  • 从不同的 PC 登录实际上是相同的 - 您不共享会话 ID

  • 注销实际上是删除服务器上会话 ID 的条目。

Note: the unique session id can alternatively be stored:

注意:唯一的会话 ID 也可以存储:

回答by Dean Povey

Servlets in Java have an HttpSession object which you can use to store state information for a user. The session is managed on the client by a cookie (JSESSIONID) or can be done using URL rewrites. The session timeout describes how long the server will wait after the last request before deleting the state information stored in a HttpSession.

Java 中的 Servlet 有一个 HttpSession 对象,您可以使用它来存储用户的状态信息。会话由 cookie (JSESSIONID) 在客户端管理,或者可以使用 URL 重写来完成。会话超时描述了在删除存储在 HttpSession 中的状态信息之前,服务器将在最后一次请求之后等待多长时间。

The scope is per browser instance, so in the example you give logging in from two different pcs will result in two session objects.

范围是每个浏览器实例,因此在示例中,您从两台不同的 PC 登录将导致两个会话对象。

回答by bdhar

What does it indicate actually ?

它实际上表明了什么?

The lifetime of a session. The session expires if there is no transaction between the client and the server for 30 minutes (per the code segment)

会话的生命周期。如果客户端和服务器之间 30 分钟(根据代码段)没有事务,则会话过期

Is is scope of whole project ?

是整个项目的范围吗?

It has application scope. Defined for each web application

它有应用范围。为每个 Web 应用程序定义

Another point confusing me is how can we separate the session scope of multiple request in the same project? Means if I am logging in from a PC & at the same time I am logging in from another PC, does it differentiate it ?

另一点让我困惑的是,我们如何在同一个项目中分离多个请求的会话范围?意思是如果我从一台 PC 登录 & 同时我从另一台 PC 登录,它会区分吗?

Yes. The session ids (JSESSIONID for Apache Tomcat) will be different.

是的。会话 ID(Apache Tomcat 的 JSESSIONID)会有所不同。

Also, another confusing thing is the browser difference. Why does the different Gmails possible to open in different browsers ?

此外,另一个令人困惑的事情是浏览器的差异。为什么不同的 Gmail 可以在不同的浏览器中打开?

Each login by the same user from a different browser is a different session altogether. And the cookies set in one browser will not affect in another. So different Gmail instances are possible in different browsers.

同一用户从不同浏览器进行的每次登录都是完全不同的会话。并且在一个浏览器中设置的 cookie 不会影响另一个浏览器。因此,不同的浏览器中可能有不同的 Gmail 实例。

And Gmail can prevent a session from Login to Logout. How is it maintained with our personal web ?

Gmail 可以阻止从登录到注销的会话。它是如何通过我们的个人网络维护的?

Persistent cookies

持久性 cookie

回答by Lalchand

if you open the same application in different window i mean multiple instance of a browser it will create different session for every instance.

如果您在不同的窗口中打开同一个应用程序,我的意思是浏览器的多个实例,它将为每个实例创建不同的会话。

回答by Yaniv

I recommand Apache Shirofor session management,Authentication and authorization.

我推荐使用Apache Shiro进行会话管理、身份验证和授权。

I take it back.

我把它收回。

As @BalusC commeneted below, only servlet container is in charge of managing the http session. Shiro is just using that. It will hook to HttpSession via a filter you explicitly define.

正如@BalusC 在下面评论的那样,只有 servlet 容器负责管理 http 会话。Shiro 正在使用它。它将通过您明确定义的过滤器连接到 HttpSession。

回答by user2191576

we have 4 ways to manage a session.

我们有 4 种方法来管理会话。

1.Cookies 2.URL rewriting 3.Hidden form fields 4.HTTP session

1.Cookies 2.URL 重写 3.隐藏的表单域 4.HTTP 会话

the fourth one is powerful and mostly used now-a-days.

第四个功能强大,现在主要使用。