wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:setProperty  question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jsp:setProperty  question" Watch "jsp:setProperty  question" New topic
Author

jsp:setProperty question

Vivek Gupta
Greenhorn

Joined: May 02, 2005
Posts: 23
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.
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
<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 ]
Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
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 ?


It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
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


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
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

Joined: Apr 10, 2006
Posts: 75
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

Joined: Nov 11, 2005
Posts: 127
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

Joined: Dec 04, 2004
Posts: 950
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

Joined: Nov 11, 2005
Posts: 127
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

Joined: Dec 04, 2004
Posts: 950
Hi Aleksander,

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

Thanks
Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
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.
 
subject: jsp:setProperty question