About Question enthuware.jwpv6.2.1043 :

Moderator: admin

ETS User

About Question enthuware.jwpv6.2.1043 :

Post by ETS User » Mon Aug 15, 2011 1:08 am

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 <% ... %>

admin
Site Admin
Posts: 7181
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1043 :

Post by admin » Mon Aug 15, 2011 10:52 am

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.

kemosabe
Posts: 9
Joined: Fri Oct 09, 2015 3:52 am
Contact:

Re: About Question enthuware.jwpv6.2.1043 :

Post by kemosabe » Tue Jun 28, 2016 4:43 pm

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?

admin
Site Admin
Posts: 7181
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1043 :

Post by admin » Tue Jun 28, 2016 7:42 pm

No, the whole thing is a valid scriptlet even if some part of it may not be so individually.

JJeena
Posts: 5
Joined: Thu Nov 02, 2017 1:33 am
Contact:

Re: About Question enthuware.jwpv6.2.1043 :

Post by JJeena » Thu Nov 02, 2017 1:36 am

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.

admin
Site Admin
Posts: 7181
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1043 :

Post by admin » Thu Nov 02, 2017 2:06 am

Did you try executing the given code?

JJeena
Posts: 5
Joined: Thu Nov 02, 2017 1:33 am
Contact:

Re: About Question enthuware.jwpv6.2.1043 :

Post by JJeena » Thu Nov 02, 2017 6:45 am

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.

admin
Site Admin
Posts: 7181
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1043 :

Post by admin » Thu Nov 02, 2017 11:16 pm

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.)

JJeena
Posts: 5
Joined: Thu Nov 02, 2017 1:33 am
Contact:

Re: About Question enthuware.jwpv6.2.1043 :

Post by JJeena » Fri Nov 03, 2017 1:10 am

Got it..
Thanks..

mcmovsho
Posts: 3
Joined: Tue Jan 09, 2018 10:02 pm
Contact:

Re: About Question enthuware.jwpv6.2.1043 :

Post by mcmovsho » Tue Jan 09, 2018 10:06 pm

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;"?

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests