Hi, The scope attribute of the jsp:useBean tag defaults to page. Here no scope is specified so the jsp:useBean looks into the page scope before creating the bean. The assumed parameter "mystring" is in the request, not in the page scope. So the bean is actually an empty string. That why it prints "".
I do not agree on this one. using the property='*' means that all the parameters are taken and mapped on the setters of the object. As, in this case the String object has no setHello() method, nothing happens, and we remain with an empty string.
The <jsp:useBean> tag is ended in the first line it self. so the second line does not makes sense in this question. As defaulf scope is page it sees for the mystring in the page scope and it print nothing.
If the setProperty tag is defined inside the body of the jsp:useBean tag then wat u say is correct
Wat u said is correct. I got bit confused over this. This question the setproperty will be executed always. So wat u said is correct. Sorry was confusing others and u also. Being a learner iam getting into these confusion. sorry
-selva SCJP 1.4
posted 13 years ago
Indeed, the useBean is ended, with />, but that only means that the setProperty is always executed. If it was in the useBean tag, then it only be executed if the class was not found in pageScope and was created as a result of that.
Or are you saying that the setProperty can only be used in the body of the useBean ?
Selva Prasad Rajendran
posted 13 years ago
Iam very sorry. I confuse u too. Sorry for that. Wat u said is correct.The setpropery will be always executed in this case.
Hi Cris, I agree with your idea, but I think you are wrong in your explanation ******************************** As, in this case the String object has no setHello() method, nothing happens, ----------------- ********************************
Puting it all togheter, let me see if I understood this question:
* <jsp:useBean id="mystring" class="java.lang.String" /> Container will search for a "mystring" bean in the page context. If no bean with this name is found then it will create one.
* <jsp:setProperty name="mystring" property="*" /> Container will try to set all attributes that contains standard setters (setXXX) with the given property=value pairs available in the request. Since that the pair mystring=hello is in the request, the container will try to call the method mystring.setMystring(String), however this method dos not exists in the "mystring" object, then nothing happens.
* <%=mystring%> Since the string value was not updated by the previous line, it will simple print an empty string ("").