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

doubts from jsp spec

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
<% { // introduce a new block %>
...
<jsp:useBean id=”customer” class=”com.myco.Customer” />
<%
/*
* the tag above creates or obtains the Customer Bean
* reference, associates it with the name “customer” in the
* PageContext,
and declares a Java programming language
* variable of the same name initialized to the object reference
* in this block’s scope.
*/
%>
...
<%= customer.getName(); %>
...
<% } // close the block %
<%
// the variable customer is out of scope now but
// the object is still valid (and accessible via pageContext)
%>

associates it with the name “customer” in the
PageContext ..what does this mean? does he say it is available to only that page???

next the last few lines ..what does he mean that obj is still valid...accessible thru pageContext
what i understood was customer cannot be accessed as the use Bean is defined inside scriptlet tag..but what does he mean that obj is still valid...accessible thru pageContext
[ March 14, 2002: Message edited by: shan java ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

What's happening is that the jsp:useBean markup gets converted to a variable declaration in the resulting servlet; in this case with name customer.
Since it's declared within a block, the variable goes out of scope when the block closes -- just like any other declared variable.
But, the object that customer referred to still has a reference from page scope (it could be request scope, session, or application scope as well if the useBean had specified so). So the object still exists and is not a candidate for garbage collection until the page goes out of scope.
You could obtain a new reference to the object by accessing the attribute in the page context.
Taking a look at the java code that a JSP gets converted to is a technique I've found very useful for figuring out mechanisms like this.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You could obtain a new reference to the object by accessing the attribute in the page context.

can u give me a small line of code to access the object which customer was referring thru pageContext
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

For example:
pageContext.getAttribute("customer",PageContext.PAGE_SCOPE);
NOTE: I am not advocating actually doing this -- I'd consider this very bad practice. I'm sure that the original author was pointing this out only to discuss how the useBean mechanism works.
If you need to access the variable outside of that innner block, it'd be best to declare it at the approriate scope in the JSP page.
hth,
bear
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubts from jsp spec