• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

page scope for beans declared in a JSP

 
Aruna Raghavan
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
This is a question on page #262 and 263 in the Manning book. It says that the default scope for a userbBean is the page (which, in my understanding is the servlet scope itself.)
Now, if I define something like this-
<%
if (true)
{
%>
<jsp:useBean id="address" class="AddressBean"/>
<%
}
%>
shouldn't the scope of the userBean "address" be the entire page, rather than the {} scope it is defined in?
The book says unless the scope is explicitly defined as the application or session or request, its scope is just {}.
Thanks!
Aruna.
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aruna
My understanding of this is that the scope of the variable ('address' in this case) depends on where you define it in the code, as is the case with any other Java variable declarations.
But the default scope that the bean (ie. the instance of the 'AddressBean' class) resides in is 'page'.
So...
Even though the variable 'address' is defined (and limited to) the {} scope, the bean itself can be referenced outside the {} scope by using PageContext.getAttribute("address").
Hope this helps
 
boyet silverio
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it might also help if you think of the 'scope object' and the 'declaration scope'.
the 'scope' attribute in the <jsp:useBean id="address" class="AddressBean" /> line could be interpreted to refer to the 'scope' object by which the 'address' object is set and retrieved. The 'scope objects' could be 'page' (pageContext) which is in your case (default being 'page'), 'session', 'application' (servletContext) or 'request'. e.g. based on the code snippet you provided,
// in your case, to set
AddressBean address = new AddressBean();
pageContext.setAttribute("address", address);
...
// and to retrieve as pointed out by Mark is
... (AddressBean)pageContext.getAttribute("address");
You might be confusing this 'scope' with the 'declaration scope', or the scope where your AddressBean address has been declared i.e. {}.
hope i didn' blur matters.

[ January 30, 2003: Message edited by: boyet silverio ]
 
Dharmin Desai
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaration scope {}, is for variable adress of type Adress and not for bean.
Bean is available even through out PAGE. Just use another variable to retrive the same object of bean class out side the braces{}.
Best Regards, Dharmin
 
boyet silverio
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Declaration scope {}, is for variable adress of type Adress and not for bean.

Dharmin, you probably meant
"Declaration scope{}, is for variable address of AddressBean and not for bean."
anyway, it is the same (the 'address' which is also the bean). Try looking at the servlet that the servlet container translated from the jsp ( i created one containing Aruna's code snippet) which used <jsp:useBean id="address" class="AddressBean" />.
and also you can access the same 'bean' using the same variable outside the {} (containing <jsp:useBean ...> using the same variable using pageContext.getAttribute("address")and assigning return value to AddressBean address = .... because it is outside the scope of the {} which contained the <jsp:useBean...>
hope i didn't further blur matters. cheers.
[ February 01, 2003: Message edited by: boyet silverio ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic