File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes c:forEach code doubt 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 "c:forEach code doubt" Watch "c:forEach code doubt" New topic
Author

c:forEach code doubt

S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
in servlet:

String[] arry ={"additon","subtraction","multiplication","division"};
request.setAttribute("matharry",arry);
/*RequestDispatcher rd=request.getRequestDispatcher("Eltest.jsp");
rd.forward(request,response);*/

in Eltest.jsp
code1:
<c:forEach var="arr" items="${matharry}" >
<c ut value="${arr}"/>
</c:forEach>

or
code 2:
<c:forEach var="arr" items="${requestScope[matharry]}" >
<c ut value="${arr}"/>
</c:forEach>


my output:
blank page

If i uncomment RequestDispatcher then only code 1 works
if i commet RequestDispatcher i know the code 1 wont work
but code 2 is not working in either case

Thanks inadvance
Sravanthi

[ May 02, 2008: Message edited by: Vijaya Sravanthi ]
[ May 02, 2008: Message edited by: Vijaya Sravanthi ]
al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
Is the jsp code you have code for Eltest.jsp?

Also, not sure if this is right, but I think your problem might have to do with the fact that request scope attributes are valid for only one request.
So if this is right (and I am not sure if it is), by the time you get to your jsp code, the attributes are already gone.
[ May 02, 2008: Message edited by: al langley ]
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
thanks langley
you have been helping me
actually when i am reading about EL implicit scopes which are used to get the attributes set in request object of servlet can be retrived using requestScope implicit object but i didnt get expected output
[ May 02, 2008: Message edited by: Vijaya Sravanthi ]
al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
What output did you get in your second code? You may want to try and put single quotes around 'matharry' and see if that works. For example:
"${requestScope['matharry']}"
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
that works finally
i would like to know more differences between [],[''],[""]
Kumar Garlapati
Ranch Hand

Joined: Feb 04, 2008
Posts: 38
http://java.boot.by/wcd-guide/ch07s02.html


Regards,<br />Kumar
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
The link is quite helpful
thanks
Sravanthi
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
i have tried code 2 replacing requestScope[matharry] with requestScope.matharry it is working
Shiraz Khan
Ranch Hand

Joined: Mar 20, 2006
Posts: 51
while using EL, we dont have to worry about the scope of the attribute unless there is a naming conflict between scopes (HFSJ page 385)

I dont get why the first code is not printing the value ??
Can anybody put some light on this !!

I think we will get the results if we use this ..

<c ut value="${matharry[0]}">
<c ut value="${matharry[1]}">
<c ut value="${matharry[2]}">
<c ut value="${matharry[3]}">
[ May 04, 2008: Message edited by: Shiraz Khan ]
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
Khan,

As you can see the attribute is being set in a servlet while the printing is done in a JSP. the RequestDispatcher will pass the request (and its attributes) and the reponse to the JSP. That's will allow the matharry to exist in the JSP, without the request dipatching and forwarding matharry doesn't exist in the JSP and it will be null.

hope that cleared it.


SCBCD - SCWCD - SCJD - SCJP - OCA
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
now i got clear picture thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: c:forEach code doubt