博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【jsp】案例:显示商品列表 & 问题:List内添加元素,为什么值都变成一样的了...
阅读量:5096 次
发布时间:2019-06-13

本文共 7736 字,大约阅读时间需要 25 分钟。

 

 

代码:

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
getAllProduct(){12 List
productList=new ArrayList
();//List是接口,不能直接new List13 // 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
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   6   7   8 
9 会员登录 10
11 12 13
14
15 16 28 29 30 31 32 33
34
35 36 37
38
39
42
43 44 <% 45 List
productList = (List
) request.getAttribute("productList"); 46 47 if (productList != null) { 48 for (Product product : productList) { 49 50 out.write("
"); 51 out.write("
"); 52 out.write(""); 53 out.write("

"); 54 out.write(""+product.getPname()+""); 55 out.write("

"); 56 out.write("

"); 57 out.write("商城价:¥"+product.getShop_price()+""); 58 out.write("

"); 59 out.write("
"); 60 61 } 62 } 63 %> 64 65
66 67
68
69
84
85
86 87
88
90 91

浏览记录

92
93
more 94
95
96 97
98 99
    100
    103
104 105
106
107 108 109
110
111 112 113 114

 

结果:

 

 

注意:

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

 

转载于:https://www.cnblogs.com/musecho/p/11254412.html

你可能感兴趣的文章
Linux 释放cache化缓存
查看>>
loadrunner11的移动端性能测试之场景设计
查看>>
C#颜色转Delphi颜色的C#代码
查看>>
MainFrame知识小结(20110925)--cobol中table越界
查看>>
hdu 5159 Card (期望)
查看>>
hdu 5429 Geometric Progression(存个大数模板)
查看>>
深入理解java异常处理机制
查看>>
Spring知识整理【二】 Spring IOC
查看>>
DTD举例一
查看>>
Package与Activity简介
查看>>
python使用cookielib库示例分享
查看>>
Android GridView自定义Adapter
查看>>
CsQuery获取IDomObject元素的完整CSS选择器
查看>>
文件下载 NSURLConnection——文件下载与上传
查看>>
高德地图相关开发
查看>>
Java多线程系列--“JUC线程池”02之 线程池原理(一)
查看>>
kindeditor编辑器图片水印
查看>>
测试驱动开发-TDD
查看>>
Bzoj3756
查看>>
Use w3m as Web Browser
查看>>