How to iterate an object in JSP to get the percentage?

断了今生、忘了曾经 提交于 2019-12-02 13:40:43

There are three things as following:

  1. Add the functions taglib: <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  2. Add reportsCounts variable to requestScope instead of session if not required on any other page & use as following: <c:set var="oracleDBCount" value="0" scope="page" /> <c:forEach var="reportCount" items="${requestScope.reportCounts.databaseName}" varStatus="loop"> <TR> <TD>${requestScope.reportCounts.hash[loop.index]}</TD> <TD>${reportCount} <c:if test="${reportCount == 'ORACLE'}"> <c:set var="oracleDBCount" value="${oracleDBCount + 1}" scope="page"/> </c:if> </TD> <TD>${requestScope.reportCounts.version[loop.index]}</TD> </TR> </c:forEach>
  3. Now display percentage as: ${(oracleDBCount / fn:length(requestScope.reportCounts))*100}%

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

<c:out value="${sessionScope.oracleReports}"/>

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

<c:forEach var="i" begin="0" end="${reportCount.getHash().size() - 1}">
                    <TD id="databaseName<c:out value='${i}' />">
                            ${reportCount.getDatabaseName().get(i)}
                    </TD>

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

<tr>
     <td id="oraclePercentage">%</td>
     <td id="mySQLPercentage">%</td>
</tr>

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.

<TR><TD><Script>setPercentage('${reportCount.getHash().size() - 1}');</script><TD><TR>

what to do is defined here

<Script>
function setPercentage(var index){
  for(var i=0; i<index;i++){
     var documentType=document.getElementById("databaseName"+i).innerHTML;
     var oracleCount=0;
     var mySQLCount=0;
     if(vardocumentType=='Oracle')
       oracleCount+=(Number)1;
     else if(vardocumentType=='MySQL')
       mySQLCount+=(Number)1;
     document.getElementById("oraclePercentage").innerHTML=(oracleCount/index)*100;
     document.getElementById("mySQLPercentage").innerHTML=(mySQLCount/index)*100;
 }
 </script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!