• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

question

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

A. It will print "".
B. It will print "hello"
C. It will not compile.
D. It will throw exception at runtime.

why choose A?
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<jsp:setProperty name="mystring" property="*" />

Tells the container to set the property named "*" (so all) to <null>
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer to the question needs further explanation.

<jsp:setProperty name="mystring" property="*" />

Colin, I don't think that the semantics of jsp:setProperty is what you say:
"Tells the container to set the property named "*" (so all) to <null>"

I think, according to the JSP 2.0 spec (page 143), if property is set to * the tag will:
1) iterate over the current ServletRequest parameters (mystring in this example),
2) match parameter names (mystring) and value type(s) to property names and setter method type(s) (no matches in this example, since in a String there's no mystring property),
3) set each matched property to the value of the matching parameter (nothing to do, since there was no matches in 2))

So, in my opinion, the tag jsp:setProperty leave the bean mystring untouched and <%=mystring%> should print:
a) If jsp:useBean finds the bean mystring in the page scope: whatever the value of mystring is.
b) else: "", because jsp:useBean instantiates a new bean (using the no-args constructor).

So I think that there's no a totally correct answer among the options A to D.
[ February 28, 2005: Message edited by: Jose Esteban ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The jsp:setProperty action in this question tries to set all the properties of the bean from the values present in the request. However, the String class does not have any method named setMystring() and so the page prints "".
 
Jose Esteban
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, what happens if there was an attribute named "mystrig" in the page scope with a value of "Hi".

Then, <%=mystring%> should print "Hi", isn't it?
 
Colin Fletcher
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose, you are right about both.

<jsp:setProperty name="mystring" property="*" /> will match request parameters with the attributes of the mystring object and set them accordingly.

So if we did this:
html page: a.html
<form method=post action=b.jsp>
<input type=text name=myName>
<input type=submit value=' submit '>
</form>

b.jsp:

<jsp:useBean id="myobject" type="com.example.myobject">
<jsp:setProperty name="myobject" property="*" />
</jsp:useBean>

<BR><BR>
<jsp:getProperty name="myobject" property="myName" />


-C
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic