JSESSIONID 是在什么条件下创建的? [英] Under what conditions is a JSESSIONID created?

查看:52
本文介绍了JSESSIONID 是在什么条件下创建的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建JSESSIONID的时间/条件是什么?

When / what are the conditions when a JSESSIONID is created?

是按域吗?例如,如果我有一个 Tomcat 应用程序服务器,并且我部署了多个 Web 应用程序,是否会为每个上下文(Web 应用程序)创建不同的 JSESSIONID,或者它是否在 Web 应用程序之间共享,只要它们是同一个域?

Is it per a domain? For instance, if I have a Tomcat app server, and I deploy multiple web applications, will a different JSESSIONID be created per context (web application), or is it shared across web applications as long as they are the same domain?

推荐答案

JSESSIONID cookie 在创建会话时创建/发送.当您的代码第一次调用 request.getSession()request.getSession(true) 时,会创建会话.如果您只想获取会话,但如果它不存在则不创建它,请使用 request.getSession(false) -- 这将返回一个会话或 null.在这种情况下,不会创建新会话,也不会发送 JSESSIONID cookie.(这也意味着会话不一定在第一次请求时创建...您和您的代码可以控制何时创建会话)

JSESSIONID cookie is created/sent when session is created. Session is created when your code calls request.getSession() or request.getSession(true) for the first time. If you just want to get the session, but not create it if it doesn't exist, use request.getSession(false) -- this will return you a session or null. In this case, new session is not created, and JSESSIONID cookie is not sent. (This also means that session isn't necessarily created on first request... you and your code are in control when the session is created)

会话是基于上下文的:

SRV.7.3 会话范围

SRV.7.3 Session Scope

HttpSession 对象的范围必须在应用程序(或 servlet 上下文)等级.底层机制,例如作为用于建立session,对于不同的可以是相同的上下文,但引用的对象,包括其中的属性对象,绝不能在它们之间共享容器的上下文.

HttpSession objects must be scoped at the application (or servlet context) level. The underlying mechanism, such as the cookie used to establish the session, can be the same for different contexts, but the object referenced, including the attributes in that object, must never be shared between contexts by the container.

(Servlet 2.4 规范)

更新:如果还没有会话,每次调用 JSP 页面都会隐式地创建一个新会话.这可以使用 session='false' 页面指令关闭,在这种情况下,会话变量在 JSP 页面上根本不可用.

Update: Every call to JSP page implicitly creates a new session if there is no session yet. This can be turned off with the session='false' page directive, in which case session variable is not available on JSP page at all.

这篇关于JSESSIONID 是在什么条件下创建的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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