• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we put EL in a for loop?

 
jyothi sanka
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a jsp in a HF book. There is a String[] named MusicList in the servlet. I am trying to write array elements using EL in a JSP.

<html><body>
Music is: ${musicList[0]}
<% String[] musicList=(String[])request.getAttribute("musicList");
for(int i=0; i< musicList.length;i++){ %>
Music song is : ${musicList[i]}
<% } %>

</body>
</html>

when I run the code above, I am getting this output:
Music is: Zero7 Music song is : Music song is : Music song is : Music song is :

EL is not working in for loop. Can't we use EL in a for loop?
Thanks
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi u can use el in for loop. Now use this code, u can get the desired output. try this

<html><body>
Music is: ${musicList[0]}
<% String[] musicList=(String[])request.getAttribute("musicList");
for(int i=0; i< musicList.length;i++){ %>
Music song is : <%= ${musicList[i]} %>
<% } %>

</body>



</html>


Post ur comments if it works fine. Bcos i m also a beginner
bye for now
sat
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem has nothing to do with the loop, but because you are mixing the EL with scriplets. Particluarly, the loop variable i is a scripting variable which cannot be used with the EL.

Clue: why are you using a for loop in a scriptlet as opposed to the JSTL forEach tag?
 
jyothi sanka
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply. just I want to try is EL works in loop.
I wrote the for loop using JSTL forEach tag, I got the output.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But do you understand why the scriplet for loop did not work and the forEach loop did? It's an important concept.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you're trying weird stuff, you can just do

[ January 24, 2005: Message edited by: Yuriy Zilbergleyt ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good question to ask yourself at this point is:
Why am I using EL? or What benefit am I getting from it at this point?

If the point of EL, JSTL, and jsp:useBean is to help factor the Java scriptlets out of the JSP code, but you're still using Java scriptlets in your JSP code, what is EL doing for you? Why not just use <%=...%>?

From what I can see, it's just adding another layer of indirection and obsfucation.

If you're not going to move the scriptlets out of the JSPs, ditch all the other tools and write your code in Java. At least you'll make it easy for Java developers to work with your code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic