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 Scriplet doubt

 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the two is correct syntax

A <% if(true)%>
I am fine
<%else%>
You are fine

B<%! int i; %>
<% if(i==0) %>
<% i++; %>
<% else %>
<%= "i==1" %>

Kindly help.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think the newline after if give you problem. Safer side use {} brackets for the if.

Thanks
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right...

But these were the options given in the mock.
I am aware of the fact that newline can create problem in such scenerio.

Could you please give the correct answer from these two options.
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, Sandeep, but these two JSP chunks are very poor examples and a cert question written like this would be dropped.

Here is the code generated by Tomcat v5.0.24 for the first item:


As you can see this is invalid Java code.

You can safely ignore bad mock questions such as this. You will not see anything this poorly concieved on the real exam.

Cheers,
Bryan
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I just read your question again. The answer is both of these JSP code chunks are valid JSP syntax. Unfortunately, they are translated into invalid Java syntax. Does that answer your question?

-Bryan
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bryan,

I am sure we won't face such questions in the actual exam, but incase if encounter one, is it fine to go the by syntax and select the answer true ?

Thanks,
Reshma
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right...

Atleast the syntax for both of these code is correct.
so in case we get such question in exam, I think we should go for both of them.

Thanks Bryan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic