isNew()如何判断会话是新会话还是已在使用? [英] How does isNew() tell if the session is a new one or is already in use?

查看:236
本文介绍了isNew()如何判断会话是新会话还是已在使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调用 isNew() ,检查会话是新会话还是已在使用?

How does calling isNew() on the session object,check if the session is a new one or is already in use ?

我读到 isNew()如果客户端尚未使用会话ID回复,则返回true。但是这是什么意思 ?请解释

I read that isNew() returns true if the client has not yet responded with the session ID. But what does it mean ? Please explain

推荐答案


我读到如果客户端尚未响应,则isNew()返回true会话ID。但它是什么意思?

I read that isNew() returns true if the client has not yet responded with the session ID. But what does it mean ?

考虑服务器当前正在处理请求。会话处理有两种情况。

Consider that the server is currently processing a request. There are two scenarios with respect to session handling.


  • 在新会话场景中,正在为用户创建一个新会话/客户端由服务器。 (客户端可能在请求中没有提供会话ID,或者它可能提供了服务器认为无效的会话ID。)servlet的应用程序代码决定是否需要会话(例如,因为它有一些想要的信息)存储在那里),并尝试使用如果不存在标志获取它。 servlet基础结构意识到没有当前会话,创建具有新会话ID的新会话,并将其保存在会话存储中。在请求完成时,会话ID返回给客户端;例如作为cookie,或作为附加会话ID的URL。

  • In the new session scenario, a new session is being created for the user / client by the server. (The client may have supplied no session id in the request, or it may have supplied a session id that the server thinks is invalid.) The application code of the servlet decides a session is required (e.g. because it has some information it wants to store there), and attempts to fetch it with the "create if not present" flag. The servlet infrastructure realises that there is no current session, creates a new one with a new session id, and saves it in the session store. At the completion of the request, the session id is returned to the client; e.g. as a cookie, or as a URL with session id attached.

在现有会话场景中,客户端在请求中包含会话ID;例如作为会话cookie,或作为请求URL中的会话ID。 servlet基础结构识别此id,在其会话存储中查找它,并且(如果需要)重新创建包含从会话存储中检索的会话状态的 HttpSession 对象。当servlet的应用程序代码尝试访问会话时,它会获得 HttpSession 对象,而不是新对象。然后,servlet在处理请求时可以使用和更新会话状态。

In the existing session scenario, the client has included a session id in the request; e.g. as a session cookie, or as a session id in the request URL. The servlet infrastructure recognizes this id, looks it up in its session store and (if necessary) recreates the HttpSession object containing the session state retrieved from the session store. When the application code of the servlet attempts to access the session, it gets this HttpSession object, not a new one. The session state can then be used and updated by the servlet as it processes the request.

在第一个场景中,在会话对象上调用 isNew()将返回 true ,因为这是一个新会话。

In the first scenario, calling isNew() on the session object will return true because this is a new session.

在第二种情况下,在会话对象上调用 isNew()将返回 false 因为这不是一个新的会话。

In the second scenario, calling isNew() on the session object will return false because this is NOT a new session.


如何在会话对象上调用isNew(),检查会话是否是新会话或者已经在使用?

How does calling isNew() on the session object,check if the session is a new one or is already in use ?

servlet基础架构知道发生了两个场景中的哪一个,因为它进行了会话创建或会话查找。 isNew()最明显的实现是在 HttpSession中包含 private boolean 字段 object,并返回该字段的值作为 isNew()的结果。该字段将由servlet基础结构根据其获取会话对象的方式进行初始化。

The servlet infrastructure knows which of the two scenarios occurred because it did the session creation or session lookup. The most obvious implementation of isNew() is to include a private boolean field in the HttpSession object, and return the value of that field as the result of isNew(). The field would be initialized by the servlet infrastructure according to how it obtained the session object.

这篇关于isNew()如何判断会话是新会话还是已在使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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