aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JSTL  c:forEach tag iteration variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JSTL  c:forEach tag iteration variable" Watch "JSTL  c:forEach tag iteration variable" New topic
Author

JSTL c:forEach tag iteration variable

padma jyotsna
Greenhorn

Joined: Jun 07, 2010
Posts: 17
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

Joined: Jan 10, 2002
Posts: 61201
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
padma jyotsna
Greenhorn

Joined: Jun 07, 2010
Posts: 17
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

Joined: Jan 10, 2002
Posts: 61201
    
  66

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
padma jyotsna
Greenhorn

Joined: Jun 07, 2010
Posts: 17

checked the generated servlet code..got that clear

Thanks guys...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL c:forEach tag iteration variable