• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing object declared in useBean

 
Anoopriya Muthuswaami
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:useBean id="strName" class="java.lang.String"/>

<%= strName %>

What is the output
1) It won't compile.
2) It is a valid jsp line and it will print the variable called name.
3) It will compile but it will always produce null as the output.
4) It will work if you create a javabean class with only one variable of type java.lang.String.

Given answer: 2

I feel it is incorrect. Correct answer is 1

Explanation:
Assume there is no attribute 'strName' in any scope. <jsp:useBean> will create a new String object and place it in page scope. Scoped objects cant be accessed like <%= objectName %>.
This gives compilation error that 'strName' cannot be resolved.

Please confirm if my understanding is correct
 
cesar valencia
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as far as i can recall, you do can access page scoped attributes in an expression. The code will compile and it will print whatever the strName variable holds.
[ December 09, 2008: Message edited by: cesar valencia ]
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume there is no attribute 'strName' in any scope. <jsp:useBean> will create a new String object and place it in page scope. Scoped objects cant be accessed like <%= objectName %>.
This gives compilation error that 'strName' cannot be resolved.
All correct, except <jsp:useBean /> also creates a scripting variable in the current JSP page, which can be accessed via the scripting expressions. So in this case, option (2) is correct.
as far as i can recall, you do can access page scoped attributes in an expression. The code will compile and it will print whatever the strName variable holds.
No---you cannot access scoped variables in scripting elements by just quoting their name. You should use EL instead, else you have to lookup the scoped variable for yourself, like this:
 
Anoopriya Muthuswaami
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks... I got the concept.
 
liao Yang
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the declaration of

<jsp:useBean id="strName" class="java.lang.String"/>

I can't figure out a way to get the value of strName by using <jsp:getProperty>. Is it possible?

Thanks
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!