【jsp】案例:显示商品列表 & 问题:List内添加元素,为什么值都变成一样的了
2024-09-02 05:53:58
代码:
1.Product:
package domain; public class Product { /* `pid` varchar(50) NOT NULL,
`pname` varchar(150) DEFAULT NULL,
`market_price` double DEFAULT NULL,
`shop_price` double DEFAULT NULL,
`pimage` varchar(200) DEFAULT NULL,
`pdate` date DEFAULT NULL,
`is_hot` int(11) DEFAULT NULL,
`pdesc` varchar(255) DEFAULT NULL,
`pflag` int(11) DEFAULT NULL,
`cid` varchar(50) DEFAULT NULL,
PRIMARY KEY (`pid`)*/ private String pid;
private String pname;
private double market_price;
private double shop_price;
private String pimage;
private String pdate;
private int is_hot;
private String pdesc;
private int pflag;
private String cid; public Product(){
pid="";
pname="";
pimage="";
pdate="";
pdesc="";
cid="";
} public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getMarket_price() {
return market_price;
}
public void setMarket_price(double market_price) {
this.market_price = market_price;
}
public double getShop_price() {
return shop_price;
}
public void setShop_price(double shop_price) {
this.shop_price = shop_price;
}
public String getPimage() {
return pimage;
}
public void setPimage(String pimage) {
this.pimage = pimage;
}
public String getPdate() {
return pdate;
}
public void setPdate(String pdate) {
this.pdate = pdate;
}
public int getIs_hot() {
return is_hot;
}
public void setIs_hot(int is_hot) {
this.is_hot = is_hot;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
public int getPflag() {
return pflag;
}
public void setPflag(int pflag) {
this.pflag = pflag;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
} }
2.ProductListD
package dao; import java.sql.*;
import java.util.ArrayList;
import java.util.List; import domain.Product; public class ProductListDao { public List<Product> getAllProduct(){
List<Product> productList=new ArrayList<Product>();//List是接口,不能直接new List
// Product product=new Product();//不能只定义一个变量,页面上只显示最后一个商品 try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/web17","root","root");
PreparedStatement ps=con.prepareStatement("select * from product");
ResultSet rs=ps.executeQuery();
// int i=0;
while(rs.next()) {
Product product=new Product(); product.setPid(rs.getString(1));
product.setPname(rs.getString(2));
product.setMarket_price(rs.getInt(3));
product.setShop_price(rs.getInt(4));
product.setPimage(rs.getString(5));
product.setPdate(rs.getString(6));
product.setIs_hot(rs.getInt(7));
product.setPdate(rs.getString(8));
product.setPflag(rs.getInt(9));
product.setCid(rs.getString(10)); // System.out.println(product.getPname());
productList.add(product);
// System.out.println(productList.get(i).getPname());
// i++;
} rs.close();
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
} return productList;
} }
3.ProductListServlet
package servlet; import java.io.IOException;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import dao.ProductListDao;
import domain.Product; public class ProductListServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { ProductListDao productListDao=new ProductListDao();
List<Product> productList=productListDao.getAllProduct();
// System.out.println(productList.isEmpty()); request.setAttribute("productList", productList);
request.getRequestDispatcher("/product_list.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
4.product_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="domain.*"%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员登录</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" /> <style>
body {
margin-top: 20px;
margin: 0 auto;
width: 100%;
} .carousel-inner .item img {
width: 100%;
height: 300px;
}
</style>
</head> <body> <!-- 引入header.jsp -->
<jsp:include page="/header.jsp"></jsp:include> <div class="row" style="width: 1210px; margin: 0 auto;">
<div class="col-md-12">
<ol class="breadcrumb">
<li><a href="#">首页</a></li>
</ol>
</div> <%
List<Product> productList = (List<Product>) request.getAttribute("productList"); if (productList != null) {
for (Product product : productList) { out.write("<div class='col-md-2'>");
out.write("<a href='product_info.htm'> <img src='"+product.getPimage()+"' width='170' height='170' style='display: inline-block;'>");
out.write("</a>");
out.write("<p>");
out.write("<a href='product_info.html' style='color: green'>"+product.getPname()+"</a>");
out.write("</p>");
out.write("<p>");
out.write("<font color='#FF0000'>商城价:¥"+product.getShop_price()+"</font>");
out.write("</p>");
out.write("</div>"); }
}
%> </div> <!--分页 -->
<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
<ul class="pagination" style="text-align: center; margin-top: 10px;">
<li class="disabled"><a href="#" aria-label="Previous"><span
aria-hidden="true">«</span></a></li>
<li class="active"><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">6</a></li>
<li><a href="#">7</a></li>
<li><a href="#">8</a></li>
<li><a href="#">9</a></li>
<li><a href="#" aria-label="Next"> <span aria-hidden="true">»</span>
</a></li>
</ul>
</div>
<!-- 分页结束 --> <!--商品浏览记录-->
<div
style="width: 1210px; margin: 0 auto; padding: 0 9px; border: 1px solid #ddd; border-top: 2px solid #999; height: 246px;"> <h4 style="width: 50%; float: left; font: 14px/30px 微软雅黑">浏览记录</h4>
<div style="width: 50%; float: right; text-align: right;">
<a href="">more</a>
</div>
<div style="clear: both;"></div> <div style="overflow: hidden;"> <ul style="list-style: none;">
<li
style="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><img
src="products/1/cs10001.jpg" width="130px" height="130px" /></li>
</ul> </div>
</div> <!-- 引入footer.jsp -->
<jsp:include page="/footer.jsp"></jsp:include> </body> </html>
结果:
http://localhost:8080/WEB17_2/ProductListServlet
注意:
加入List内的元素只保存了地址,而不是复制元素。将元素加入List后,修改该元素值,会影响到List内的元素值。所以每次放入一个新元素都需要new一个新地址。否则结果只有最后一个元素值,前面的元素值都被覆盖了。
最新文章
- cocos2dx中对象的两步初始化
- discuz判断用户登录
- Java 数组 可变长参数 实例
- 【重走Android之路】【路线篇(二)】知识点归纳
- bzoj 1834 [ZJOI2010]network 网络扩容(MCMF)
- XML及PullParser解析
- Go语言简单的TCP编程
- Non recursive Depth first search
- Using CrunchBase API
- 在新浪sae上部署WeRoBot
- WMI入门教程之WMI中的类在哪里?
- Struts2深入
- mysql中utf8和utf8mb4区别
- shell生成随机字符串
- windows系统 webstorm安装zencoding方法
- select2中的ajax请求
- Python3 k-邻近算法(KNN)
- 前端 网络三剑客之html 02
- springboot 搭建 简单 web项目 【springboot + freemark模板 + yml 配置文件 + 热修复 + 测试用例】附源码
- JQuery Ajax 使用FormData上传文件对象