我正在研究servlet和jsp项目。我正在将一个对象从servlet传递到JSP。目前,我正在迭代该对象并将其显示在表格中-
以下是我在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> <TH>Version</TH> </TR> <c:forEach var="i" begin="0" end="${reportCount.getHash().size() - 1}"> <TR> <TD> ${reportCount.getHash().get(i)} </TD> <TD> ${reportCount.getDatabaseName().get(i)} </TD> <TD> ${reportCount.getVersion().get(i)} </TD> </TR> </c:forEach>
上面的代码工作正常,我能够正确显示数据。现在我需要做的是-
${reportCount.getDatabaseName().get(i)}将以oracle或mysql仅返回数据库名称。现在,我需要计算oracle数据库的百分比。我将从中获取记录总数${reportCount.getHash().size()。因此,如果记录总数为10且oracle数据库存在的5次数为,则百分比应为50%。
${reportCount.getDatabaseName().get(i)}
oracle
mysql
${reportCount.getHash().size()
10
5
50%
现在我不确定如何使用该对象计算上述百分比?在计算完该百分比之后,我需要在新表格中显示结果,如下所示-
<table> <tr> <th style="background-color: #E8E8E6;"><b>Oracle Database</b></th> </tr> <tr> <!-- I would like to show the percentage in this row --> <td>%</td> </tr> </table>
我在想我应该reportCount在上面的新表中再次迭代该对象,并在此处提取百分比,但是不确定该怎么做?任何人都可以提供示例吗?
reportCount
更新:-
这是我的bean代码-
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 }
有以下三件事:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<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>
${(oracleDBCount / fn:length(requestScope.reportCounts))*100}%