Servlet 中的请求和会话 [英] Request and session in Servlet

查看:61
本文介绍了Servlet 中的请求和会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的关于 web 请求和会话的问题.当我从具有不同选项卡的同一浏览器或通过新窗口多次请求同一页面时,会话 ID 和会话创建时间相同.

I have very simple question with request and session in web. When I requested a same page page for multiple time from same browser with different tabs or through new window, session ID and session creation time was same.

这是我通过 Internet Explorer 完成的.但是当使用不同的浏览器(如谷歌浏览器)并访问同一页面时,就会出现不同的会话 ID 和会话创建时间.据我了解,http 请求是无状态的.

This I have done from internet explorer. But when in use a different browser like google chrome and access the same page then different session id and session creation time was there. As far as my understanding says http request is stateless.

因此,在我的情况下,它在同一浏览器中似乎不是无状态的,因为对于不同的 http 请求,通过容器创建新的 servlet 来创建新线程.所以我得出以下结论:

So, in my case it does not seem to be stateless within same browser as for different http request new thread is created by creating new servlet by container. So I have come to following conclusion:

如果请求是从同一浏览器发送的,同时打开了不同的选项卡或通过另一个新窗口,请求总是使用相同的线程进行具有相同会话 ID 的 servlet 操作.如果请求是从不同的浏览器发送的,那么新的 http 请求将使用新的会话 ID 发送.所以,我的问题是什么时候它是无状态的?如果请求是从不同的浏览器并发发送的?如果我在 spring 中声明 scope="request">scope="session"> 那么它也遵循相同的情况?如果我的理解有误,请指正.

If request is send from same browser with different tabs opened or through another new window at that time, the request always use the same thread for servlet operation with same session Id. If request is send from different browser then new http request is sent with new session ID.So,my question is then when it is stateless? If the request is send concurrently from different browser? If i declare scope="request"> and scope="session"> in spring then it also follows the same case ? If I am wrong in my understanding please correct me.

推荐答案

当您的服务器应用程序开始一个新会话时,servlet 容器会发送一个带有 JSESSIONIDSet-Cookie 的标头代码>返回浏览器.浏览器保存该 cookie,并在每次请求时将其发送回服务器,而不管您从哪个选项卡发出请求.很明显,其他浏览器无法访问该 cookie,因此它们会从服务器接收另一个.

When your server application starts a new session, the servlet container sends a Set-Cookie header with a JSESSIONID back to the browser. The browser saves that cookie, and sends it back to the server with each request regardless of what tab you are making the request from. Obvoiusly other browsers don't have access to that cookie, so they will receive another one from the server.

当您的服务器收到带有 JSESSIONID cookie 的请求时,它可以将该请求与之前发出的具有相同 ID 的请求相关联.serlvet 容器能够将不同的属性与该 id 相关联,并在请求之间保留这些属性.http 会话对象基本上是这些属性的容器,您的服务器应用程序对其具有读/写访问权限.基本上,这就是在无状态 http 协议之上使用 http 会话实现有状态的方式.

When your server receives a request with a JSESSIONID cookie, it can correlate that request with requests with the same id made earlier. The serlvet container is able to associate different attributes with that id, and persist these attributes between requests. The http session object is basically a container for these attributes, to which your server application has a read/write access. Basically this is how statefullness is implemented with http sessions on top of the otherwise stateless http protocol.

至于线程:每个请求都可以由任何随机线程处理,因为会话数据不绑定到特定线程.servlet 容器维护从会话 id 到包含不同属性的会话对象的映射.因此,任何随机线程都可以根据其会话 ID 访问属于当前请求的会话对象.

As for the threads: each request can be processed by any random thread, because the session data is not bound to a particular thread. It is the servlet container that maintains the mapping from session id to the session object containing the different attributes. Consequently any random thread can access the session object belonging to the current request based on its session id.

在 Spring 中,request 范围意味着为每个请求新创建一个 bean 实例,而 session 范围内的 bean 的生命周期绑定到 http session 的生命周期.

In Spring, request scope means that a bean instance gets newly created for each request, while the lifecycle of the session scoped beans is bound to that of the http session.

这篇关于Servlet 中的请求和会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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