• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean scope question

 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still confused on the distinction between this two scopes

<jsp:useBean id="person" class="com.models.Person" scope="request" >
<jsp:useBean id="person" class="com.models.Person" scope="session" >

What would be the difference between the two?
 
P. Dunn
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This Bean lasts for the request:
<jsp:useBean id="person" class="com.models.Person" scope="request" >

This survives for the whole session:
<jsp:useBean id="person" class="com.models.Person" scope="session" >
[ February 24, 2005: Message edited by: P. Dunn ]
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This survives for the whole session:
<jsp:useBean id="person" class="com.models.Person" scope="session" >


Do you mean that another jsp can get this person object right? While the other one cant?
 
Francois Roland
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right.

In fact, if the user calls the same page a second time, the page will not be able to retrieve the request attribute either (unless you gives it as a parameter).

The request scope is opened when a client sends a HTTP request to the application server. And it is closed just after the server has responded to the client. So, whenever the client makes a request to the server, a new request scope (that obviously doesn't contain the objects stored in the previous) is opened and closed.

The session scope is kept opened by the server until the session times out or your applications calls the HttpSession.invalidate() methods. So you can store there objects that can be retrieved during the next requests of the same user in the same session. All the pages and/or servlets of the same web application have access to the objects store in the user session.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks but to clarify another confusion

If I have this on jsp1.jsp
<jsp:useBean id="person" class="com.models.Person" scope="session" >

and this on jsp2.jsp
<jsp:getProperty name="person" property="firstName">

How come this doesn't work? My only thought is because I need the standard action useBean to grab it again from session. Am I right?
 
Colin Fletcher
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you will need another jsp:useBean in jsp2.jsp. I would specify the scope to ensure you are picking up the correct object. You don't have to because the scope is searched from page to application, but I would for clarity.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic