aspose file tools*
The moose likes JSP and the fly likes Can we put EL in a for loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Can we put EL in a for loop?" Watch "Can we put EL in a for loop?" New topic
Author

Can we put EL in a for loop?

jyothi sanka
Ranch Hand

Joined: Aug 27, 2001
Posts: 39
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

Joined: May 03, 2004
Posts: 334
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

Joined: Jan 10, 2002
Posts: 61648
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jyothi sanka
Ranch Hand

Joined: Aug 27, 2001
Posts: 39
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

Joined: Jan 10, 2002
Posts: 61648
    
  67

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

Joined: Dec 13, 2004
Posts: 429
As long as you're trying weird stuff, you can just do

[ January 24, 2005: Message edited by: Yuriy Zilbergleyt ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we put EL in a for loop?