Mark Fenech
Mark Fenech

Reputation: 1358

Getting ArrayList Size on jsp page

hi I am trying to get the total size of an arraylist but for some reason this is not being shown the code below is used to show the size.

    <%db.DBConnection db = new  db.DBConnection(); 
            ArrayList<User> myUsers =db.getAllUsers();
            %>  

   Total Subscribed Users: <p><% myUsers.size();%></p>

Thanks

Upvotes: 3

Views: 23447

Answers (4)

Kent
Kent

Reputation: 195199

does this work?

<p><%= myUsers.size()%></p>

I suggest that write as less java codes as possible in your jsp. You could consider to use some taglib, jstl. for example. Put all business codes on your server side. specially things like b.DBConnection db = new db.DBConnection();

Upvotes: 5

Rafay
Rafay

Reputation: 6188

You should do this to achieve that purpose:

Total Subscribed Users: <p><%= myUsers.size(); %></p>

Alternatively, you can also do:

Total Subscribed Users: <p><% out.print(String.valueOf(myUsers.size()));%></p>

The block of code you wrote in your question was a "scriplet". By itself, a scriptlet doesn't contribute any HTML.

See this for more details.

Upvotes: 0

kschneid
kschneid

Reputation: 5694

Some of this is clearly ripe for moving out of JSP, but based on how it looks like your classes and methods are defined, this should work (JSP + JSTL):

<jsp:useBean id="db" class="db.DBConnection"/>
<c:set var="myUsers" value="${db.allUsers}"/>
Total Subscribed Users: <p>${fn:length(myUsers)}</p>

Upvotes: 13

Mark Fenech
Mark Fenech

Reputation: 1358

 int count = myUsers.size();
            %>  
            <h1>User Management</h1>
            Total Subscribed Users: <p><%=count%></p>

Upvotes: 0

Related Questions