jsp页面没有显示arraylist值

时间:2016-07-28 07:00:46

标签: java jsp servlets

我编写了数组列表的代码,并在JSP页面中显示了数据库值。 但它只显示JSP页面中的表标题值。我不知道我在哪里犯了这个错误。任何人帮助我

这是我的Servlet代码

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        PrintWriter out=response.getWriter();
            try
            {
                String date1=request.getParameter("startdte");
                String date2=request.getParameter("enddte"); 
                SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                Date date3 = df.parse(date1);
                SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
                Date date4 = format1.parse(date2);
                java.sql.Date sqdate1 = new java.sql.Date(date3.getTime());
                java.sql.Date sqdate2 = new java.sql.Date(date4.getTime());
                Connection conn=null;
                PreparedStatement pst=null;
                ResultSet rst=null;
                ArrayList<Product> listProducts = new ArrayList<Product>();
                conn=mysqlconnect.ConnecrDb();
                String sql="select bill_master.bill_no,DATE_FORMAT(bill_master.bill_date, '%Y/%m/%d') AS 'formatted_date',transaction.product_id,transaction.tax_amount,transaction.amount,transaction.amount-transaction.tax_amount as 'without_tax ',product_master.Product_name,product_master.vat from bill_master inner join transaction on bill_master.bill_no=transaction.bill_no inner join product_master on transaction.product_id=product_master.product_id where vat='14.50' and DATE_FORMAT(bill_master.bill_date, '%Y/%m/%d')between ? and ?";
                pst= conn.prepareStatement(sql);
                pst.setDate(1, sqdate1);
                pst.setDate(2, sqdate2);
                rst= pst.executeQuery();          
                while(rst.next())
                {
                    Product product=new Product();
                    product.Bill_no= rst.getInt("bill_no");
                    product.Formatted_date=rst.getDate("formatted_date");
                    product.Product_id=rst.getInt("product_id");
                    product.Tax_amount=rst.getDouble("tax_amount");
                    product.Amount=rst.getDouble("amount");
                    product.Without_tax=rst.getDouble("without_tax"); 
                    product.Product_name=rst.getString("product_name");
                    product.Vat=rst.getDouble("vat");
                    listProducts.add(product);

                }
                    request.setAttribute("listProducts",listProducts);
                    conn.close();
                    RequestDispatcher rd=request.getRequestDispatcher("/report1.jsp");
                    rd.forward(request,response);
            }
           catch(Exception e)
           {
              e.printStackTrace();
           }
    }

这是我的产品类代码

package report;
import java.sql.Date;
public class Product {
 public int Bill_no;
   public  Date Formatted_date;
   public int Product_id;
   public double Tax_amount;
   public double Amount;
   public double Without_tax;
   public String Product_name;
   public double Vat;   
}

这是我的report1 JSP页面代码

<%@ page import="java.util.*"%>
<%@ page import="report.Product"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>
    <body>          

          <div align="center">
        <table border="1" cellpadding="5">/
            <caption><h2>List of products</h2></caption>
            <tr>
                <th>Bill NO</th>
                <th>Product Id</th>
                <th>Date</th>
                <th>Tax</th>
                <th>Without TAX</th>
                <th>Including TAX</th>
                <th>Product Name</th>
                <th>VAT</th>
            </tr>
             <%
                 ArrayList<Product> dproducts=(ArrayList)request.getAttribute("listProducts");
                   Iterator it=dproducts.iterator();
                  while(it.hasNext())
                 {
                  Product product=(Product)it.next();
           %>

             <tr>
                  <td><%=product.Bill_no %></td>
                 <td><%=product.Formatted_date %></td> 
                 <td><%=product.Product_id %></td>
                 <td><%=product.Tax_amount %> </td>
                 <td><%=product.Without_tax %> </td>
                 <td><%=product.Amount %> </td>
                 <td><%=product.Product_name %> </td>
                 <td><%=product.Vat %> </td>
            </tr>              
            <%
                 }
             %>
        </table>           
    </div>  
  </body>
</html>

0 个答案:

没有答案