File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Getting the List size in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Getting the List size in JSP" Watch "Getting the List size in JSP" New topic

Getting the List size in JSP

Elizabath Lima
Ranch Hand

Joined: Nov 23, 2005
Posts: 32

I would like to get the size of the list in jsp. I need the total size of the list to be displayed in the page. I couldn't yet find ot a solution for this. Is it possible at jsp to get the list size using jstl? or shall i use <% %> to get the size like jsp 1.0?
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Is this a List interface ? It does not follow the javabeans convention, so I don't think that you can get the size of the list. A quick way would be to use a scriptlet <%= mylist.size() %>

If you want to avoid sciptlets, a little wrapper around your list, with a getSize() method could do the trick.
Or you could even put the list size in a request atribute.

If you're looping through the list before displaying the total number of records using itertors like c:forEach, you could keep the total count in a page-scoped variable.
[ April 11, 2006: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Elizabath Lima
Ranch Hand

Joined: Nov 23, 2005
Posts: 32

Thanks for your reply.

I'm searching a jstl implementation for my jsp code. I'm trying for a jstl and jsp 2.0 implementation for the following code


for(int i=offset.intValue(), l = Math.min(i + maxPageItems,arrResult.size()); i < l ;i++) {
MyBean result = (MyBean)arrResult.get(i);

if that for loop is omitted, then rest is working fine for me..

<c:forEach items="${searchResults}" var="searchResult" varStatus="searchIndex"> .....

while using the Math.min in the <c ut> its throwing an exception like this

/WEB-INF/pages/Search.jsp(329,14) The function min must be used with a prefix when a default namespace is not specified

How i can convert the above code to jstl and jsp 2.0? and is it possible to assign a scriplet as the value in a jstl tag?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873


You should have a copy of the JSTL Spec on your desk.
[ April 11, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Stefan Evans

Joined: Jul 06, 2005
Posts: 1580
This assumes that the values "offset" and "maxPageItems" are available to JSTL, and are not just scriptlet variables.
The call to Math.min has been replaced with a <c:if>

Elizabath Lima
Ranch Hand

Joined: Nov 23, 2005
Posts: 32
Thanks a lot Stefan Evans

You have done a great work for me.

And everything working fine for me except calling a jsp from another jsp in the spring framework.
I agree. Here's the link:
subject: Getting the List size in JSP
It's not a secret anymore!