This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session/scope

 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely not beginners material. Moving to servlets.
 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic