wood burning stoves 2.0*
The moose likes Servlets and the fly likes Session/scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session/scope" Watch "Session/scope" New topic
Author

Session/scope

Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
Hi everyone,
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?

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Definitely not beginners material. Moving to servlets.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
Thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
must be associated with sessions


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.

Bill
Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
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?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
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.

Bill
Gina vernon
Ranch Hand

Joined: Jan 16, 2009
Posts: 108
Thanks everyone.
 
 
subject: Session/scope