Page 1 of 1

About Question enthuware.jwpv6.2.1043 :

Posted: Mon Aug 15, 2011 1:08 am
by ETS User
Hi,

Scriptlet #1:

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

Line 2 is not inside <% ... %>

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Mon Aug 15, 2011 10:52 am
by admin
The given explanation contains the following:
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.

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Tue Jun 28, 2016 4:43 pm
by kemosabe
Okay, but the question says "Which of the following are correct JSP scriptlet?", and the "System.out.println("starting loop");" occurs outside of the scriptlet tags, so technically it's not a scriptlet. At least, that was my thinking.

Should I interpret this question to say "Which of the following will compile and run?" or is there a clearer definition of scriptlet that I need to know?

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Tue Jun 28, 2016 7:42 pm
by admin
No, the whole thing is a valid scriptlet even if some part of it may not be so individually.

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Thu Nov 02, 2017 1:36 am
by JJeena
Why option 3 is wrong:
3. <% int i = 0, s = 0; System.out.println("starting loop"); for(; i< 10; i++) { s+=i; } %>

AS far as i know for(;i<10;i++) will not create any new variable. It will use the already declared one.
I think Explanation is not correct.

_jspService(...) {    ...    int i = 0;    System.out.println("starting loop");    for(int i=0; i< 10; i++) { out.println(i); } // here i is being declared again, so it will not compile.    ... }

Please help me here.

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Thu Nov 02, 2017 2:06 am
by admin
Did you try executing the given code?

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Thu Nov 02, 2017 6:45 am
by JJeena
Yes infact I did.
Here is the jsp I had

<%@ page import="java.util.*" %>

<html>
<body>
<h1 align="center">Beer Recommendation JSP </h1>

<h2 align ="center"> Check the Loop: </h2>
<% int k = 0, s=0;
out.println("starting loop");
for(; k< 10; k++)
{
out.println("In the loop");
s+=k; }%>


</body>
</html>

And the output I got is :

Beer Recommendation JSP

Check the Loop:

starting loop In the loop In the loop In the loop In the loop In the loop In the loop In the loop In the loop In the loop In the loop.

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Thu Nov 02, 2017 11:16 pm
by admin
Very sorry, I misunderstood your question. Yes, scriptlet 3 is correct and the option that is marked as correct i.e. option 5 indeed says scriptlet 1, 3, and 4 are valid scriptlets.

Option 3 is invalid because it says scriptlets 3 and 4 are valid. It does not list scriptlet 1 as valid.

Since you have to select only 1 option, you need to select the best option, which is option 5.

(The explanation explains why scriptlet 2 is not valid.)

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Fri Nov 03, 2017 1:10 am
by JJeena
Got it..
Thanks..

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Tue Jan 09, 2018 10:06 pm
by mcmovsho
Option 3 is not compiling, complaint that "s cannot be resolved to a variable". Don't you need to declare it as "int i=0;int s =0;"?

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Tue Jan 09, 2018 10:42 pm
by admin
Yes, it is already being declared in the given code: int i = 0, s = 0;

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Thu Jan 11, 2018 9:21 am
by mcmovsho
well, like I said, it does not compile. Only after I changed it to int i = 0; int s = 0;

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Thu Jan 11, 2018 9:58 am
by admin
Not sure why it would not compile for you. There is nothing wrong with that line. What error do you get?

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Thu Jan 11, 2018 12:05 pm
by mcmovsho
"s cannot be resolved to a variable"

Re: About Question enthuware.jwpv6.2.1043 :

Posted: Thu Jan 11, 2018 10:30 pm
by admin
I tried it just now on Tomcat 8.0.27 . It is working fine. The servlet container that you are using might have a bug.