About Question enthuware.jwpv6.2.1043 :
Moderator: admin
About Question enthuware.jwpv6.2.1043 :
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 <% ... %>
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 <% ... %>
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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.
-
- Posts: 9
- Joined: Fri Oct 09, 2015 3:52 am
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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?
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?
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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.
-
- Posts: 5
- Joined: Thu Nov 02, 2017 1:33 am
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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.
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.
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
Did you try executing the given code?
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Thu Nov 02, 2017 1:33 am
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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.
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.
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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.)
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.
-
- Posts: 5
- Joined: Thu Nov 02, 2017 1:33 am
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
Got it..
Thanks..
Thanks..
-
- Posts: 3
- Joined: Tue Jan 09, 2018 10:02 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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;"?
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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.
-
- Posts: 3
- Joined: Tue Jan 09, 2018 10:02 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
well, like I said, it does not compile. Only after I changed it to int i = 0; int s = 0;
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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.
-
- Posts: 3
- Joined: Tue Jan 09, 2018 10:02 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
"s cannot be resolved to a variable"
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1043 :
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.
Who is online
Users browsing this forum: No registered users and 1 guest