码迷,mamicode.com
首页 > Web开发 > 详细

【jsp】案例:显示商品列表 & 问题:List内添加元素,为什么值都变成一样的了

时间:2019-07-27 12:35:06      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:use   jdb   pat   hid   height   oat   java   display   header   

技术图片

 

 

代码:

1.Product:

 1 package domain;
 2 
 3 public class Product {
 4     
 5 /*    `pid` varchar(50) NOT NULL,
 6       `pname` varchar(150) DEFAULT NULL,
 7       `market_price` double DEFAULT NULL,
 8       `shop_price` double DEFAULT NULL,
 9       `pimage` varchar(200) DEFAULT NULL,
10       `pdate` date DEFAULT NULL,
11       `is_hot` int(11) DEFAULT NULL,
12       `pdesc` varchar(255) DEFAULT NULL,
13       `pflag` int(11) DEFAULT NULL,
14       `cid` varchar(50) DEFAULT NULL,
15       PRIMARY KEY (`pid`)*/
16     
17     private String pid;
18     private String pname;
19     private double market_price;
20     private double shop_price;
21     private String pimage;
22     private String pdate;
23     private int is_hot;
24     private String pdesc;
25     private int pflag;
26     private String cid;
27     
28     public Product(){
29         pid="";
30         pname="";
31         pimage="";
32         pdate="";
33         pdesc="";
34         cid="";
35     }
36     
37     public String getPid() {
38         return pid;
39     }
40     public void setPid(String pid) {
41         this.pid = pid;
42     }
43     public String getPname() {
44         return pname;
45     }
46     public void setPname(String pname) {
47         this.pname = pname;
48     }
49     public double getMarket_price() {
50         return market_price;
51     }
52     public void setMarket_price(double market_price) {
53         this.market_price = market_price;
54     }
55     public double getShop_price() {
56         return shop_price;
57     }
58     public void setShop_price(double shop_price) {
59         this.shop_price = shop_price;
60     }
61     public String getPimage() {
62         return pimage;
63     }
64     public void setPimage(String pimage) {
65         this.pimage = pimage;
66     }
67     public String getPdate() {
68         return pdate;
69     }
70     public void setPdate(String pdate) {
71         this.pdate = pdate;
72     }
73     public int getIs_hot() {
74         return is_hot;
75     }
76     public void setIs_hot(int is_hot) {
77         this.is_hot = is_hot;
78     }
79     public String getPdesc() {
80         return pdesc;
81     }
82     public void setPdesc(String pdesc) {
83         this.pdesc = pdesc;
84     }
85     public int getPflag() {
86         return pflag;
87     }
88     public void setPflag(int pflag) {
89         this.pflag = pflag;
90     }
91     public String getCid() {
92         return cid;
93     }
94     public void setCid(String cid) {
95         this.cid = cid;
96     }
97     
98 }

 

2.ProductListD

 1 package dao;
 2 
 3 import java.sql.*;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import domain.Product;
 8 
 9 public class ProductListDao {
10 
11     public List<Product> getAllProduct(){
12         List<Product> productList=new ArrayList<Product>();//List是接口,不能直接new List
13 //        Product product=new Product();//不能只定义一个变量,页面上只显示最后一个商品
14         
15         try {
16             Class.forName("com.mysql.jdbc.Driver");
17         } catch (ClassNotFoundException e) {
18             e.printStackTrace();
19         }
20         try {
21             Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/web17","root","root");
22             PreparedStatement ps=con.prepareStatement("select * from product");
23             ResultSet rs=ps.executeQuery();
24 //            int i=0;
25             while(rs.next()) {
26                 Product product=new Product();
27                 
28                 product.setPid(rs.getString(1));
29                 product.setPname(rs.getString(2));
30                 product.setMarket_price(rs.getInt(3));
31                 product.setShop_price(rs.getInt(4));
32                 product.setPimage(rs.getString(5));
33                 product.setPdate(rs.getString(6));
34                 product.setIs_hot(rs.getInt(7));
35                 product.setPdate(rs.getString(8));
36                 product.setPflag(rs.getInt(9));
37                 product.setCid(rs.getString(10));
38                 
39 //                System.out.println(product.getPname());
40                 productList.add(product);
41 //                System.out.println(productList.get(i).getPname());
42 //                i++;
43             }
44             
45             rs.close();
46             ps.close();
47             con.close();
48         } catch (SQLException e) {
49             e.printStackTrace();
50         }
51         
52         return productList;
53     }
54     
55 }

 

3.ProductListServlet

 1 package servlet;
 2 
 3 import java.io.IOException;
 4 import java.util.List;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 import dao.ProductListDao;
12 import domain.Product;
13 
14 public class ProductListServlet extends HttpServlet {
15 
16     protected void doGet(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18 
19         ProductListDao productListDao=new ProductListDao();
20         List<Product> productList=productListDao.getAllProduct();
21 //        System.out.println(productList.isEmpty());
22         
23         request.setAttribute("productList", productList);
24         request.getRequestDispatcher("/product_list.jsp").forward(request, response);
25         
26     }
27 
28     protected void doPost(HttpServletRequest request, HttpServletResponse response)
29             throws ServletException, IOException {
30         doGet(request, response);
31     }
32 
33 }

 

4.product_list.jsp

  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2     pageEncoding="UTF-8"%>
  3 <%@ page import="java.util.*"%>
  4 <%@ page import="domain.*"%>
  5 <!DOCTYPE html>
  6 <html>
  7 <head>
  8 <meta name="viewport" content="width=device-width, initial-scale=1">
  9 <title>会员登录</title>
 10 <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
 11 <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
 12 <script src="js/bootstrap.min.js" type="text/javascript"></script>
 13 <!-- 引入自定义css文件 style.css -->
 14 <link rel="stylesheet" href="css/style.css" type="text/css" />
 15 
 16 <style>
 17 body {
 18     margin-top: 20px;
 19     margin: 0 auto;
 20     width: 100%;
 21 }
 22 
 23 .carousel-inner .item img {
 24     width: 100%;
 25     height: 300px;
 26 }
 27 </style>
 28 </head>
 29 
 30 <body>
 31 
 32 
 33     <!-- 引入header.jsp -->
 34     <jsp:include page="/header.jsp"></jsp:include>
 35 
 36 
 37     <div class="row" style="width: 1210px; margin: 0 auto;">
 38         <div class="col-md-12">
 39             <ol class="breadcrumb">
 40                 <li><a href="#">首页</a></li>
 41             </ol>
 42         </div>
 43 
 44         <%
 45             List<Product> productList = (List<Product>) request.getAttribute("productList");
 46 
 47             if (productList != null) {
 48                 for (Product product : productList) {
 49 
 50                     out.write("<div class=‘col-md-2‘>");
 51                     out.write("<a href=‘product_info.htm‘> <img src=‘"+product.getPimage()+"‘ width=‘170‘ height=‘170‘ style=‘display: inline-block;‘>");
 52                     out.write("</a>");
 53                     out.write("<p>");
 54                     out.write("<a href=‘product_info.html‘ style=‘color: green‘>"+product.getPname()+"</a>");
 55                     out.write("</p>");
 56                     out.write("<p>");
 57                     out.write("<font color=‘#FF0000‘>商城价:&yen;"+product.getShop_price()+"</font>");
 58                     out.write("</p>");
 59                     out.write("</div>");
 60 
 61                 }
 62             }
 63         %>
 64 
 65     </div>
 66 
 67     <!--分页 -->
 68     <div style="width: 380px; margin: 0 auto; margin-top: 50px;">
 69         <ul class="pagination" style="text-align: center; margin-top: 10px;">
 70             <li class="disabled"><a href="#" aria-label="Previous"><span
 71                     aria-hidden="true">&laquo;</span></a></li>
 72             <li class="active"><a href="#">1</a></li>
 73             <li><a href="#">2</a></li>
 74             <li><a href="#">3</a></li>
 75             <li><a href="#">4</a></li>
 76             <li><a href="#">5</a></li>
 77             <li><a href="#">6</a></li>
 78             <li><a href="#">7</a></li>
 79             <li><a href="#">8</a></li>
 80             <li><a href="#">9</a></li>
 81             <li><a href="#" aria-label="Next"> <span aria-hidden="true">&raquo;</span>
 82             </a></li>
 83         </ul>
 84     </div>
 85     <!-- 分页结束 -->
 86 
 87     <!--商品浏览记录-->
 88     <div
 89         style="width: 1210px; margin: 0 auto; padding: 0 9px; border: 1px solid #ddd; border-top: 2px solid #999; height: 246px;">
 90 
 91         <h4 style="width: 50%; float: left; font: 14px/30px 微软雅黑">浏览记录</h4>
 92         <div style="width: 50%; float: right; text-align: right;">
 93             <a href="">more</a>
 94         </div>
 95         <div style="clear: both;"></div>
 96 
 97         <div style="overflow: hidden;">
 98 
 99             <ul style="list-style: none;">
100                 <li
101                     style="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><img
102                     src="products/1/cs10001.jpg" width="130px" height="130px" /></li>
103             </ul>
104 
105         </div>
106     </div>
107 
108 
109     <!-- 引入footer.jsp -->
110     <jsp:include page="/footer.jsp"></jsp:include>
111 
112 </body>
113 
114 </html>

 

结果:

http://localhost:8080/WEB17_2/ProductListServlet

技术图片

 

 

注意:

加入List内的元素只保存了地址,而不是复制元素。将元素加入List后,修改该元素值,会影响到List内的元素值。所以每次放入一个新元素都需要new一个新地址。否则结果只有最后一个元素值,前面的元素值都被覆盖了。

 

【jsp】案例:显示商品列表 & 问题:List内添加元素,为什么值都变成一样的了

标签:use   jdb   pat   hid   height   oat   java   display   header   

原文地址:https://www.cnblogs.com/musecho/p/11254412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!