About Question enthuware.jwpv6.2.1043 :

Moderator: admin

Post Reply
ETS User

About Question enthuware.jwpv6.2.1043 :

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

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

Re: About Question enthuware.jwpv6.2.1043 :

Post 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.
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.jwpv6.2.1043 :

Post 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?

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

Re: About Question enthuware.jwpv6.2.1043 :

Post by admin »

No, the whole thing is a valid scriptlet even if some part of it may not be so individually.
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.jwpv6.2.1043 :

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

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

Re: About Question enthuware.jwpv6.2.1043 :

Post by admin »

Did you try executing the given code?
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.jwpv6.2.1043 :

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

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

Re: About Question enthuware.jwpv6.2.1043 :

Post 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.)
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.jwpv6.2.1043 :

Post by JJeena »

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 »

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

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

Re: About Question enthuware.jwpv6.2.1043 :

Post by admin »

Yes, it is already being declared in the given code: int i = 0, s = 0;
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.jwpv6.2.1043 :

Post by mcmovsho »

well, like I said, it does not compile. Only after I changed it to int i = 0; int s = 0;

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

Re: About Question enthuware.jwpv6.2.1043 :

Post by admin »

Not sure why it would not compile for you. There is nothing wrong with that line. What error do you get?
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.jwpv6.2.1043 :

Post by mcmovsho »

"s cannot be resolved to a variable"

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

Re: About Question enthuware.jwpv6.2.1043 :

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest