Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Asking mock Q

 
Wai Yip Lau
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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.


Options

Select 1 correct option.

a. It will print "".

b. It will print "hello"

c. It will not compile.

d. It will throw exception at runtime.

----------------------
A is correct.
It first creates a bean object,then the second line is irrelevant. There is a bean object in the page scope, printing it out should at least print something.
 
Sebastien Col
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 "".

Sebastien
 
chris noe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Chris.
 
Selva Prasad Rajendran
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi chris...

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

-Selva
SCJP 1.4
 
Selva Prasad Rajendran
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris..

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
 
chris noe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Selva,

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 ?

Grtz,
Chris.
 
Selva Prasad Rajendran
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

Iam very sorry. I confuse u too. Sorry for that. Wat u said is correct.The setpropery will be always executed in this case.

-Selva
 
yegappan ramaiah
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to my knowledge it should execute the toString method of the class. People correct me if I am wrong
 
Carlos Rincon
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
-----------------
********************************

I think you should say setMystring() method.

This is because the user enter the parameter "mystring=hello" in the request line!!
 
Narayan R
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I totally agree with carlos..
 
Alexandre Cervieri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

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 ("").

Right?
 
Pawan Ramchandani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct
 
S.R.K.Vivek Raju
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a good question
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic