Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

accessing object declared in useBean

 
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
 
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 ]
 
Author
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.
 
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
 
Everybody! Do the Funky Monkey! Like this tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic