为什么我index.jsp中的session获取不到利润集合呢?

来源:3-6 JSP页面实现

moummm

2017-09-23 12:54

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>


写回答 关注

1回答

  • 夏之泡沫
    2017-10-07 13:00:42
    已采纳

    你的合计在for循环里 ,放外面试试。

    moummm

    非常感谢!

    2017-10-07 13:40:46

    共 1 条回复 >

使用JSP+Servlet技术生成报表

Java Web案例课程,教你如何使用JSP+Servlet技术生成报表

69839 学习 · 84 问题

查看课程

相似问题