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 Question

 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm studying SWCD by Charles Lyons (2nd edition) and i'm reviewing some questions about JSP. On page 262, here's the following question:

Which of the following cause the value 'yes' to be written to the response if the state scripting variable is true and 'no' otherwise?

A. <%=
if (state) {
"yes";
} else {
"false";
}
%>

B. <%=state? "yes" : "no"%>
C. <% if (state) %>yes<%else%>no
D. <% if (state) out.write("yes");
else out.write("no");%>
E. <% state? out.write("yes") : out.write("no");%>

Answer is B & D. I do understand the explanation to the answer given. However nothing is mention about why A is incorrect? I don't see why A is incorrect.
Any help is appreciated.
 
Vijitha Kumara
Bartender
Pie
Posts: 3930
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would better sit on the SCWCD forum as it's about a mock exam question.

In JSPs code inside scriptlet expressions (i.e. <%=%>) become the argument to the print method of the JspWriter hence using the option A wouldn't even compile the JSP. You can't use statements inside the print method.
 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I think A will return either "yes" or "no". I don't see any difference between A and B since both are statements returning strings.
 
Vijitha Kumara
Bartender
Pie
Posts: 3930
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think A will return either "yes" or "no". I don't see any difference between A and B since both are statements returning strings.

Code inside the if statement in option A is not a valid Java code. In option B it's an expression which returns either values defined. So the two options are completely different.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kim,
Review the difference between:


and

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic