开发者

Spring when is HTTPSession created if all controllers are singleton

开发者 https://www.devze.com 2023-03-02 07:23 出处:网络
I am learning Spring 3 and trying to understand when is HTTPSession created and what is the lifecycle. All my controllers are singleton (which is by default) and I have configured a HttpSessionListene

I am learning Spring 3 and trying to understand when is HTTPSession created and what is the lifecycle. All my controllers are singleton (which is by default) and I have configured a HttpSessionListener. I observed that sessionCreated is never getting invoked.

But when I make my LoginController as Session scoped controller then the sessionCre开发者_如何转开发ated gets invoked. I want to know how is the HttpSession managed by spring controllers. Also, another question is lets say first request from borowser comes to LoinController and creates the HttpSession - then does the subsequent requests from that browser to different controller(which is singleton) would be using the same HTTPSession or woule it be stateless? Can someone please help.


Controllers are singleton and stateless. The dispatcher servlet invokes the desired controller method and passes the session to it. The session is obtained by the dispatcher servlet by calling request.getSession(). Since it is s normal servlet, the container "gives" it the request and response that any servlet obtains on each request.

That said, a servlet is also singleton (it has one instance per context). And multiple threads (= requests) are invoking its methods.

And a final note - session-scoped beans are rarely needed. In my experience you can have a whole application without any session-scoped beans.


A session associates a number of HTTP requests, so once it's created for a user the servlet engine maintains it either using cookies or URL rewriting.

0

精彩评论

暂无评论...
验证码 换一张
取 消