how to iterate an object in jsp to get the percentage

how to iterate an object in jsp to get the percentage  using -'java,html,jsp,servlets,percentage'

I am working on servlet and jsp project. I am passing an object from servlet to JSP. And currently I am iterating that object and showing them in a table -

Below is my code in jsp -

<TABLE id="tableSMS" BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;">
    <TR style="color:#ffffff;background-color:#787878;">
        <TH>Hash Name</TH>
        <TH>Database Name</TH>
    <c:forEach var="i" begin="0" end="${reportCount.getHash().size() - 1}">

And above code is working fine and I am able to show the data properly. Now what I need to do is -

${reportCount.getDatabaseName().get(i)} will return database name as oracle or mysql only. Now I need to calculate what is the percentage of oracle database. I will get the total number of records from ${reportCount.getHash().size(). So if total number of records is 10 and oracle database is present 5 times, then the percentage should be 50%.

Now I am not sure how would I calculate the above percentage using that object? And after calculating that percentage, I need to show the result in a new table which is shown below -

          <th style="background-color: #E8E8E6;"><b>Oracle Database</b></th>
              <!-- I would like to show the percentage in this row -->

I am thinking I should iterate the reportCount object again in the above new table and extract the percentage here but not sure how would I do that? Can anyone provide an example?


Here is my bean code -

public class Response {
    private List<String> hash = new LinkedList<String>();
    private List<String> databaseName = new LinkedList<String>();
    private List<String> version = new LinkedList<String>();

    // getters and setters here



asked Oct 19, 2015 by bhavin
0 votes

3 Answers

0 votes

There are three things as following:

  1. Add the functions taglib: <%@ taglib prefix="fn" uri="" %>
  2. Add reportsCounts variable to requestScope instead of session if not required on any other page & use as following: ${requestScope.reportCounts.hash[loop.index]} ${reportCount} ${requestScope.reportCounts.version[loop.index]}
  3. Now display percentage as: ${(oracleDBCount / fn:length(requestScope.reportCounts))*100}%
answered Oct 19, 2015 by param.oncemore
0 votes

Based on my experience, doing number-crunching on a JSP page can make it unreadable very quickly. Also you may encounter additional requirements in the future such as:

  • Text matching (Are you guaranteed that the value is always "oracle"? Or can it become "ORACLE" or "Oracle"?
  • What if there are zero reports? Then you will need an if-condition to prevent division by zero.
  • If your client says "We have more report servers like MS, Postgres...can you show the percentage of those?"

Is it possible to do the computation inside the servlet while you are making the reportCount object? Then you can pass the value inside a session attribute

request.getSession(true).setAttribute("oracleReports", "50%")

and then in the JSP output something like

answered Oct 19, 2015 by vibhorsingh
0 votes

Use JavaScript for doing the operation, Here is the code

Read the comments to understand the code.

Add an id to the so that i can read the inner content using javascript. Appended id with the index so that each raw gets different id's


Add Id to the so that i can set values to the raw using JavaScript


call the javascript function to set the values to the raw, as we don't have any button to trigger we open a raw and add the JavaScript call from the JSP so that the script is triggered every time the page loads the raw.

what to do is defined here

answered Oct 19, 2015 by deepak07.s