Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Dout on EL question in Enthuware test Kit? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Dout on EL question in Enthuware test Kit?" Watch "Dout on EL question in Enthuware test Kit?" New topic
Author

Dout on EL question in Enthuware test Kit?

Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206


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!!!


Thanks, AR
SCWCD 5, SCJP 1.4, OCA (PL/SQL)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
${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

Joined: Oct 02, 2008
Posts: 86
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



Kamal Tripathi
SCJP 1.4 90%, SCWCD5 94%, Next SCDJWS--> In Naescent stage. Researching abt exam and material itself.
 
jQuery in Action, 2nd edition
 
subject: Dout on EL question in Enthuware test Kit?
 
Similar Threads
Any Suggestion about a small project?
scripting varibale unavailable in EL expression
switch statement
Toughest Questions for Oracle JSP Servlet (SCWCD 5.0) Doubts
another doubt