public class Servlet extends HttpServlet {
public void init() throws ServletException{}
public void destroy(){}
public void doGet(HttpServletRequest request, HttpServletResponse response){
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response){
Service service = new Service();
try {
List<Profit> list = service.getProfitList();
request.getSession().setAttribute("ProfitList",list);
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}
}
<%@ page import="java.util.List" %>
<%@ page import="com.beans.Profit" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Java原生态生成报表</title>
</head>
<body>
<!-- js格式待做 -->
<table class="" >
<form action="" method="post">
<tr ><td colspan="7">商品利润表</td></tr>
<tr>
<th>商品ID</th>
<th>商品名</th>
<th>成本价</th>
<th>销售价</th>
<th>商品总利润</th>
<th>交易次数</th>
<th>交易总数</th>
</tr>
<%
List<Profit> list = null;
int temp = 0;
int temp1 = 0;
int temp2 = 0;
int temp3 = 0;
int temp4 = 0;
int temp5 = 0;
if (session.getAttribute("ProfitList")!=null){
if (list==null) {
list = (List<Profit>) session.getAttribute("ProfitList");
}
for (int i=0;i<list.size();i++){
Profit p = list.get(i);
if (p!=null){
temp1 += p.getCostPrice();
temp2 += p.getSellingPrice();
temp3 += p.getPrfit();
temp4 += p.getTimes();
temp5 += p.getTradingNumber();
%>
<tr>
<td><%=temp++%></td>
<td><%=p.getGoodsId()%></td>
<td><%=p.getGoodsName()%></td>
<td><%=p.getCostPrice()%></td>
<td><%=p.getSellingPrice()%></td>
<td><%=p.getPrfit()%></td>
<td><%=p.getTimes()%></td>
<td><%=p.getTradingNumber()%></td>
</tr>
<%
}
%>
<tr>
<td colspan="2">合计</td>
<td><%=temp1%></td>
<td><%=temp2%></td>
<td><%=temp3%></td>
<td><%=temp4%></td>
<td><%=temp5%></td>
</tr>
<%
}
}
%>
</form>
</table>
</body>
</html>