3

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

Mark Fenech
  • 1,358
  • 6
  • 26
  • 36

4 Answers4

13

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>
kschneid
  • 5,626
  • 23
  • 31
5

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();

Kent
  • 189,393
  • 32
  • 233
  • 301
  • As an addition, this would be a good read: http://stackoverflow.com/a/3180202/814702 – informatik01 Jan 20 '13 at 22:48
  • And please remove semicolon. Quote from "Head First Servlets and JSP": **Expressions become the argument to an out.print()**. In other words, the Container takes everything you type between the `<%=` and `%>` and puts it in as the argument to a statement that prints to the implicit response `PrintWriter out`. – informatik01 Jan 20 '13 at 22:58
  • The valid expression is `<%= myUsers.size() %>`. Semicolon is invalid inside an expression element in a JSP page. – informatik01 Jan 20 '13 at 23:17
  • 1
    @informatik01 thank you for pointing it out. I just copy/paste his codes and added an "=". – Kent Jan 20 '13 at 23:30
0
 int count = myUsers.size();
            %>  
            <h1>User Management</h1>
            Total Subscribed Users: <p><%=count%></p>
Mark Fenech
  • 1,358
  • 6
  • 26
  • 36
0

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.

Rafay
  • 6,108
  • 11
  • 51
  • 71