Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts from jsp spec

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<% { // 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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic