File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes doubts from jsp spec Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSP
Reply 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: 56191
    
  13

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


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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: 56191
    
  13

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: doubts from jsp spec
 
Similar Threads
Scripting with JSTL Core tags.
Scope of jsp:useBean
any hints for creating &/or using existing UNICODE convertor/processor?
param
jsp:useBean tag