• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jsp:setProperty question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone explain me the following - Why the answer is option 1?
Q:
Which of the given statements are correct regarding the following JSP page code?

<jsp:useBean id="mystring" class="java.lang.String" />
<jsp:setProperty name="mystring" property="*" />
<%=mystring%>

Assume that the request for this page contains a parameter mystring=hello.

Options
Select 1 correct option.

1.It will print "".
2.It will print "hello"
3.It will not compile.
4.It will throw exception at runtime.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<jsp:useBean> standard action will create a String object in the page scope, the identifier for this object will be "mystring". The wildcard in property attribute of <jsp:setProperty> standard action says something like: take the value of each parameter whose name corresponds to the property name and assign the value to that property, so it will *try* to set the property of our String object, but our object doesn't have a property called "mystring", "mystring" is the identifier for our object. As we know Beans are instantiated using no-arg constructor, therefore our String object, according to API, will be an empty String. Then when we are trying to print the value of the String object using expression, what we'll get is "" - empty String.
[ May 14, 2006: Message edited by: Aleksander Zielinski ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but our object doesn't have a property called "mystring"



Doesn't matter. What this setProperty standard action does is trying to set all the properties of the bean whose id == "name" from the request parameters. And yes, you're right, as String doesn't have any bean property setters, this line won't have any effect.

But when we come to this :


I think we are no longer talking about the same "mystring". As Alexander said, the standard action above declares an attribute in the PageScope, which is a JSP Map of <String, Object>, but this scriptlet is plain old servlet, it refers to a servlet instance object called "mystring". I'm not sure whether the output should be "" or a compilation error as this instance does not seem to have been declared (e.g. with a <%! String mystring = "" %> .

Could anybody confirm that servlet variables have nothing to do with PageScope attributes ?
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When you create a bean instance using <jsp:usBean> , it create a scoped variable as well as scripting variable with the same name. The accessibility of this scripting variable is same as the local variable declared in the method or {} the block in the method.

Thanks
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rodrigo Alvarez:
Doesn't matter. What this setProperty standard action does is trying to set all the properties of the bean whose id == "name" from the request parameters. And yes, you're right, as String doesn't have any bean property setters, this line won't have any effect.



I'm sorry, I don;t uderstand, what doesn't matter? Because later you wrote I'm right.


As for String object it's, like Narendra Dhande said, the same object, here's the generated code for the jsp.

 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

m sorry, I don;t uderstand, what doesn't matter? Because later you wrote I'm right.



Hi Aleksander, sorry , didn't mean to offend you. I just meant that it does not matter that the bean doesn't have a "mystring" attribute, because that was not what the setProperty action was trying to access.
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rodrigo Alvarez:
Hi Aleksander, sorry , didn't mean to offend you.



Hey Rodrigo, and I didn't mean to sound it like I was offended, sorry, it was just a question I should have put a ----> there. Sorry.

Originally posted by Rodrigo Alvarez:
I just meant that it does not matter that the bean doesn't have a "mystring" attribute, because that was not what the setProperty action was trying to access.



I can't agree, <jsp:setProperty> was trying to set the property "mystring" which doesn't exist, because the parameter name was "mystring", as Gupta Vivek wrote.

Originally posted by Gupta Vivek:
Assume that the request for this page contains a parameter mystring=hello.



It just didn't find the corresponding property and the action didn't take place.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In the <jsp:setProperty> the name attribute does not indicate the property to be set, but it indicate the object whose properties is going to set. Therefore when you use setProperty with property="*", it will try to set the properties which are present in the parameter list. In the above example the myString is the valid bean as the String object is the no argument public String. If you want to set the properties of myString, you have to search the String API for the setter method which fit into JavaBean properties. The myString is not property, it is Bean Object.

Hope this help

Thanks
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, #mystring# is an identifier for the bean and I don't say that "name" attribute indicates which property to set, because as we know "name" attribute refers to the bean, so the setProperty action knows what object it is supposed to work with. But we also have a request parameter named *mystring*, the same name as the identifier for the bean, so the point is, setProperty action is trying (maybe wrong word - trying, because the property doesn't exist) to set property named *mystring* of the object identified as #mystring#, but the property does not exits as String doesn't have the setMystring() method.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aleksander,

I perfectly agree with you. You had correctly specified this in your previous post also.

Thanks
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But we also have a request parameter named *mystring*



Oups, yes, you're right, I hadn't seen that part of the question.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic