• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dout on EL question in Enthuware test Kit?

 
Aaron Raja
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Given the following JSP code:

<%
ArrayList cities = new ArrayList();
cities.add("NYC");
cities.add("SFO");
%>

Which of the following correctly creates an HTML select box on the page?


A)<select name="Cities">
<%for(int i=0; i<cities.size(); i++) {%>
<option>${cities[<%=i%>]}</option>
<%}%>
</select>


B)<select name="Cities">
<%for(int i=0; i<cities.size(); i++) {%>
<option>${cities[i]}</option>
<%}%>
</select>



C)<select name="Cities">
<%for(int i=0; i<cities.size(); i++) {
<option>${cities[i]}</option>
}%>
</select>



D)<select name="Cities">
<%for(int i=0; i<cities.size(); i++) {%>
<option><%=cities.get(i)%></option>
<%}%>
</select>

According to Enthuware right Answer is D

But per my understanding It should be A, because i will be evaluated in expression and it will pick all (cities[ 0],cities[1]) am I missing something!!!
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D is the correct answer. A is wrong. ${cities} will not evaluate to the cities list declared in the page. You can't access declarations made in the jsp page using the ${} syntax. So ${cities} will evaluate to nothing...
 
Aaron Raja
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${cities[<%=i%>]}
will be translated into
${cities[0]} ----> first loop
and
${cities[1]} -----> next loop

Is that right ? If that is correct then IT should be right. I might be wrong please explain!

In D
<option><%=cities.get(i)%></option>
will be translated into
cities.get[0]----> first loop
and
cities.get[1] ----> next loop

Thanks

AR
 
Kamal Tripathi
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron Raja wrote:${cities[<%=i%>]}
will be translated into
${cities[0]} ----> first loop
and
${cities[1]} -----> next loop

Is that right ? If that is correct then IT should be right. I might be wrong please explain!


I have a couple of points to make

Scripting is not allowed inside EL expressions. Infact EL was developed in order to have scriptless JSPs.

Also EL is about storing and accessing attributes bound to some particular scope, its not a way to access a variable declare in a scriptlet. Please refer HFSJ to see how EL expression is translated into servlet. They are not translated like impliciti objects where you would be able to access local variables.

{citites[i]} ==> pageContext.findAttribute("cities[i]") // doesn't make any sense.

So as such D is the only feasible answer. Since entire code is thru scriptlets, they will be put in _jspService() method where they can create and access local variables.

To make the point abt EL more clearer, I am making some changes in your original code and I am picking option B and making some changes thr as well. In this case u'll get the desired output

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic