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

jsp:useBean

 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a question on <jsp:useBean>
Q. Which of the given statements are correct regarding the following JSP code?

Select one correct option.
a. It'll print "".
b. It'll print "hello".
c. It'll not compile.
d. It'll throw exception at runtime.
The answer given is (a).
According to me, the JSP code is translated in to the following servlet code
1 java.lang.String mystring = (java.lang.String) pageContext.getAttribute("mystring");
2 mystring.setMystring(mystring);
3 out.println(mystring);
The code # 1 must initialize mystring to "hello" and the same is being printed out on the 3rd line of code so the answer must be (b) and not (a).
Thanks
-- Ravi
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does ur "hello" string come from? Is it from the parameter in the address field of the browser? Since nothing is set to the mystring variable, it will print nothing(a blank string)...
[ December 10, 2003: Message edited by: Ko Ko Naing ]
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The question has to be rephrased
Q. Which of the given statements are correct regarding the following JSP code?

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.
Select one correct option.
a. It'll print "".
b. It'll print "hello".
c. It'll not compile.
d. It'll throw exception at runtime.
I regret the mistake.
Thanks
-- Ravi
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the request for this page contains a parameter mystring=hello
then the code
1 java.lang.String mystring = (java.lang.String) pageContext.getAttribute("mystring"); // here mystring will be initialized to "hello"
2 mystring.setMystring(mystring);
3 out.println(mystring);
so the code at runtime is evaluated as
java.lang.String mystring = "hello";
mystring.setMystring("hello");
out.println("hello");
so 'hello' must be printed in the output. Please confirm the answer.
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well; I just tested the code on tomcat passing a parameter myString=hello and I got an empty String. I think this only takes effect when the parameter is being passed from a previous page. Some others may also test the program to ensure the correctness of the answer to this question.
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi janapareddy,

1.java.lang.String mystring = (java.lang.String) pageContext.getAttribute("mystring"); // here mystring will be initialized to "hello"
2 mystring.setMystring(mystring);

You're wrong on these two points.
First one: no javabean mystring won't be initialized. If you would like your javabean to be initialized, then you should have written

instead of

POint 2:
you've made again two errors:
passing a parameter mystring=hello in the URL would be tranlated into:


equivalent here to

but it is not the case. If it would, do you think the compilator would find a setMystring method?? So, if you don't have any error, that is because this code is not present in the translation unit.
indeed, when using <jsp:setProperty name"xxx" property="*">, the corresponding code generated looks like:

and this form of code is not aware about existing method or not. If the method invoked on the bean doesn't exist, he doesn't harm.
HTH,
Cyril.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic