• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to understand the solution on EL

 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am unable to understand the following question:
Given the servlet code below, figure out what would print.
java.util.ArrayList nums = new java.util.ArrayList();
nums.add("1");
nums.add("2");
nums.add("3");
request.setAttribute("numbers",nums);
String[] favoriteMusic = {"zero 7","Tahiti 80","BT","Frou Frou"};
request.setAttribute("musicList",favoriteMusic);

1. ${musicList[numbers[0]]}
2. ${musicList[numbers[0]+1]}
3. ${musicList[numbers["2"]]}
4. ${musicList[numbers[numbers[1]]]}
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should read JSP.2.3.4 Operators "[]" and "." from the JSP specification. Basically, using brackets with an List will cause List.get(int index) to be called. So numbers[0] is the same as nums.get(0);
There are a few coercion rules explained in the spec. It would be easier to refer to it first.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give you a hint, Try evaluating the numbers[whatever] expression first, make the substitution if needed and continue to evaluate if you are not finished
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tell me what you think each of them resolves to and I will help you. If I cannot see that you have tried by yourself then I will wait until I can see that you have tried to reason it out.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks.
With regards,
Padma priya N.G.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic