This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Can we put EL in a for loop?

 
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
 
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
 
Marshal
Posts: 67273
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67273
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!