Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL c:forEach tag iteration variable

 
padma jyotsna
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I was trying out the following code.




here is the test.jsp:



and the code inside the servlet



Now I am getting a compiler error at line 06 of test.jsp i.e
<%=dept.getName()%>

why cant the scripting code access the iteration variable "dept"
whereas EL can access the iteration variable in the body of the tag?

Thanks

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scripting variables and scoped variables are two separate things. They were not designed to work together, and will not easily work together.

Use one or the other; mixing them is just problematic and will make a big mess.
 
padma jyotsna
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply...

do you mean that scripting code should not be used at all inside the body of jstl tag?
did i miss some basic point here?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignoring the certification exam (which I know nothing about), scripting shouldn't be used anywhere in a JSP page. It's over 8 years out of date and discredited.

That said, I repeat: scripting has no direct access to scoped variables. You should use JSTL and EL exclusively. If you are going to use poor-practice scriptlets (not recommended), then use those exclusively. Mixing scriptlets with JSTL/EL is just a big old mess.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you mean that scripting code should not be used at all inside the body of jstl tag?
did i miss some basic point here?

Not using scripting code at all is a good point, but that's not what Bear was telling you here. The variable "dept" used by the c:forEach tag is a scoped variable. It is accessible via EL ${dept}. The "dept" variable you are using in the scriptlet <%=dept.getName()%> is not a scoped variable, but a scripting variable.

Check how your JSP is being translated into a servlet, and you'll understand why they are not the same, and why a compilation error will happen.
 
padma jyotsna
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

checked the generated servlet code..got that clear

Thanks guys...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic