I am bit a ambiguous about scope/session management in web applications. Correct me if I am wrong, this is what I understand.
Two seperate components in a web application must be associated with sessions: servlets and beans (that access the data tier). Servlet sessions correspond to requests sent from the same browser window thus they are scoped, ususally, at session level. Beans correspond to database sessions when querying data. If I wanted to call a bean's method or variable from a JSP (which is usually scoped at sesssion level), would the bean variable/method be automatically assigned the request scope?
Not necessarily, that just what the API provides. You can write your own using cookies or hidden form variables.
There are a variety of ways to handle creating Bean instances and their scope - up to the programmer.
Joined: Jan 16, 2009
Thanks William, but I am still unclear.
Here's a simple scenario that I might follow. Lets say I am using spring and injecting my beans with dependencies. The sub bean element in the config file has a scope attribute which can be set to request/session/application. So if I were to scope a bean called A to a session and then call one of this bean A's variables or methods from a servlet that has been scoped at request level, would there be an error?
Gina vernon wrote:a servlet that has been scoped at request level
A servlet doesn't have a scope. There is usually only one instance thoroughout the application's lifetime.
In the servlet you have the HttpServletRequest to work with. You can get the associated session as HttpSession using HttpServletRequest#getSession(). You can find session scoped beans as an attribute of the HttpSession.
Author and all-around good cowpoke
Joined: Mar 22, 2000
I can't really address your question because I don't use Spring or any other "framework" - however, you have the tools at hand to answer your own question. Look at the actual Java code your framework writes - all questions of where the bean reference come from should become obvious.