Page 1 of 1

About Question com.enthuware.ets.scwcd.v5.2.142 :

Posted: Thu Nov 08, 2012 2:56 pm
by ETS User
Which of the following are correct JSP scriptlet?

1.
<% int i = 10; %>
System.out.println("starting loop");
<%for(i=0; i< 10; i++) { out.println(i); } %>

2.
<% int i = 0; %>
<% System.out.println("starting loop");%>
<% for(int i=0; i< 10; i++) { out.println(i); } %>

3.
<% int i = 0, s = 0;
System.out.println("starting loop");
for(; i< 10; i++) { s+=i; } %>

4.
<%
System.out.println("starting loop");
for(int i=0; i< 10; i++) {%> looping <%} %>


It says 1, 3 and 4. are correct but "System.out.println("starting loop");" of first scriplet it outside <% %> is this correct?

Re: About Question com.enthuware.ets.scwcd.v5.2.142 :

Posted: Sat Nov 10, 2012 11:18 am
by admin
Yes, 1 is valid. As the explanation says:
Although System.out.println("starting loop"); is not HTML but if you don't put it inside <% %>, it will be considered as HTML. It won't cause any compile or runtime problems as the JSP parser will not even look at it. This line will be output as is to the browser. So 1 is valid.
HTH,
Paul.