aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getting the no of parameter from c:forEach  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 "getting the no of parameter from c:forEach  " Watch "getting the no of parameter from c:forEach  " New topic
Author

getting the no of parameter from c:forEach

salil verma
Greenhorn

Joined: Jun 03, 2006
Posts: 22
I am new to jstl
While trying the following code,i am gettting the items in the string printed but not the no of parameters in the array

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Main extends HttpServlet implements Servlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] names={"salil verma","amit tripathi","amit pandey","amit singh"};
request.setAttribute("names",names);
RequestDispatcher rd = request.getRequestDispatcher("Main.jsp");
rd.forward(request,response);
}
}



The code for Main.jsp is as follows-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<HTML>
<HEAD>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM Software Development Platform">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE>Main.jsp</TITLE>
</HEAD>
<BODY>
<p> The attributes in the request are as follows <br>
<c:forEach var="name" items="${names}" varStatus="Count">
<br> ${name}
</c:forEach>
<p> The no of elements in the string array are = ${Count.count}
</BODY>
</HTML>

I get the following as output--

The attributes in the request are as follows

salil verma
amit tripathi
amit pandey
amit singh

The no of elements in the string array are =


But the no of elements are not printed. So can any one help me
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

varStatus only exists within the forEach. You are referencing it after the forEach where it has become out of scope.

If you move the ${Count.count} to inside the forEach you should see it work. Over and over again.
[ June 20, 2006: Message edited by: Marc Peabody ]

A good workman is known by his tools.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Instead you can try fn:length to get the number of items


[My Blog]
All roads lead to JavaRanch
salil verma
Greenhorn

Joined: Jun 03, 2006
Posts: 22
Marc, thanks for reply. got the mistake that i had done.

Satou,thanks for your reply too. Can you send the whole syntax to me as i am unable understand the syntax.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting the no of parameter from c:forEach