(产生随机序号的工具类):
package bull.utils;
/**
* 随机获取序号
*/
import java.util.UUID;
public class UUIDUtils {
public static String getUUID () {
return UUID.randomUUID().toString().replace("-" , "" );
}
}
查询所有商品
ProductDao:
public List<Product> findAll () throws SQLException {
/**
* 查询所有数据
*/
QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
String sql = "select * from product order by pdate desc" ;
List<Product> list = queryRunner.query(sql, new BeanListHandler<Product>(Product.class));
return list;
}
ProductService:
/**
* 查询所有数据
* @return
* @throws SQLException
*/
public List<Product> findAll () throws SQLException {
ProductDao productDao = new ProductDao();
List<Product> list = productDao.findAll();
return list;
}
ProductFindAllSerclet:
package bull01.Product.ProductServlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bull01.Product.ProductDomain.Product;
import bull01.Product.ProductService.ProductService;
public class ProductFindAllServlet extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
ProductService productService = new ProductService();
List<Product> list = productService.findAll();
request.setAttribute("list" , list);
request.getRequestDispatcher("/product/product_list.jsp" ).forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
product_list.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
String path = request .getContextPath();
String basePath = request .getScheme()+"://" +request .getServerName()+":" +request .getServerPort()+path+"/" ;
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html >
<head >
<base href ="<%=basePath%>" >
<title > My JSP 'product_list.jsp' starting page</title >
<meta http-equiv ="pragma" content ="no-cache" >
<meta http-equiv ="cache-control" content ="no-cache" >
<meta http-equiv ="expires" content ="0" >
<meta http-equiv ="keywords" content ="keyword1,keyword2,keyword3" >
<meta http-equiv ="description" content ="This is my page" >
<script type ="text/javascript" >
function addProduct () {
window.location.href="${pageContext.request.contextPath}/product/addProduct.jsp" ;
}
</script >
</head >
<body >
<table border ="1px" width ="800" >
<tr >
<td colspan ="7" >
商品名称:<input type ="text" name ="pname" > <input type ="button" value ="查询" onclick ="serach()" >
<input type ="button" value ="添加" onclick ="addProduct()" >
<input type ="button" value ="删除" onclick ="deleteProduct()" >
</td >
</tr >
<tr >
<td > 序号</td >
<td > 商品名称</td >
<td > 市场价格</td >
<td > 商城价格</td >
<td > 是否热门</td >
<td > 是否下架</td >
<td > 操作</td >
</tr >
<c:forEach var ="p" items ="${ list }" varStatus ="status" >
<tr >
<td > ${ status.count }</td >
<td > ${ p.pname }</td >
<td > ${ p.market_price }</td >
<td > ${ p.shop_price }</td >
<td >
<c:if test ="${ p.is_hot == 1 }" >
是
</c:if >
<c:if test ="${ p.is_hot == 0 }" >
否
</c:if >
</td >
<td >
<c:if test ="${ p.pflag == 1 }" >
已下架
</c:if >
<c:if test ="${ p.pflag == 0 }" >
未下架
</c:if >
</td >
<td > <a href ="/Web13_1/EditProductServlet?pid=${ p.pid }" > 修改</a > |<a href ="" > 删除</a > </td >
</tr >
</c:forEach >
</table >
</body >
</html >
关键字模糊查询
SearchProductServlet:
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8" );
response.setContentType("text/html;charset=UTF-8" );
try {
String pname = request.getParameter("pname" );
ProductService productService = new ProductService();
List<Product> list = productService.search(pname);
request.setAttribute("list" , list);
request.getRequestDispatcher("/product/product_list.jsp" ).forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
ProductService:
/**
* 查找数据
* @param pname
* @return
* @throws SQLException
*/
public List<Product> search (String pname) throws SQLException {
ProductDao productDao = new ProductDao();
List<Product> list = productDao.search(pname);
return list;
}
ProductDao:
/**
* 查询数据
* @param pname
* @return
* @throws SQLException
*/
public List<Product> search (String pname) throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
String sql = "select * from product where pname like ? order by pdate desc" ;
List<Product> list = queryRunner.query(sql, new BeanListHandler<Product>(Product.class), "%" +pname+"%" );
return list;
}
js:
function search () {
var pname = document.getElementById("pname" ).value;
document.getElementById("form1" ).action="${ pageContext.request.contextPath }/SearchProductServlet" ;
document.getElementById("form1" ).submit();
}
添加商品
ProductDao:
/**
* 添加数据
* @param product
* @throws SQLException
*/
public void add (Product product) throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)" ;
Object[] params = {product.getPid(),product.getPname(),product.getMarket_price(),
product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(),
product.getPdesc(),product.getPflag(),product.getCid()};
queryRunner.update(sql, params);
}
ProductService:
/**
* 添加数据
* @param product
* @throws SQLException
*/
public void add (Product product) throws SQLException {
ProductDao productDao = new ProductDao();
productDao.add(product);
}
AddProductServlet:
package bull01.Product.ProductServlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import bull.utils.UUIDUtils;
import bull01.Product.ProductDomain.Product;
import bull01.Product.ProductService.ProductService;
public class AddProductServlet extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8" );
request.setCharacterEncoding("UTF-8" );
try {
/**
* 判断是否重复提交
*/
String token1 = request.getParameter("token" );
String token2 = (String) request.getSession().getAttribute("token" );
request.getSession().removeAttribute("token" );
if (!token1.equals(token2)) {
request.setAttribute("msg" ,"您已经提交过了,请不要重复提交!" );
request.getRequestDispatcher("/product/err.jsp" ).forward(request, response);
return ;
}
Map<String,String[]> map = request.getParameterMap();
Product product = new Product();
BeanUtils.populate(product, map);
product.setPdate(new Date());
product.setPid(UUIDUtils.getUUID());
ProductService productService = new ProductService();
productService.add(product);
request.getRequestDispatcher("/ProductFindAllServlet" ).forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
addProduct.jsp:
<body >
<%
//生成一个随机令牌
String token = UUIDUtils.getUUID();
//保存到session中
session.setAttribute("token" , token);
%>
<form action ="/Web13_1/AddProductServlet" method ="post" >
<input type ="hidden" value ="<%=token %>" name ="token" >
<table border ="1px" width =500 >
<tr >
<td > 商品名称</td >
<td > <input type ="text" name ="pname" > </td >
</tr >
<tr >
<td > 市场价格</td >
<td > <input type ="text" name ="market_price" > </td >
</tr >
<tr >
<td > 商城价格</td >
<td > <input type ="text" name ="shop_price" > </td >
</tr >
<tr >
<td > 是否热门</td >
<td >
<input type ="radio" name ="is_hot" value ="1" > 是
<input type ="radio" name ="is_hot" value ="0" > 否
</td >
</tr >
<tr >
<td > 是否下架</td >
<td >
<select name ="pflag" >
<option value ="0" selected > 否</option >
<option value ="1" > 是</option >
</select >
</td >
</tr >
<tr >
<td > 商品描述</td >
<td > <textarea rows ="10" cols ="10" name ="pdesc" > </textarea > </td >
</tr >
<tr >
<td > 商品分类</td >
<td >
<select name ="cid" >
<option value ="1" > 手机数码</option >
<option value ="2" > 电脑办公</option >
<option value ="3" > 汽车用品</option >
<option value ="4" > 鞋靴箱包</option >
</select >
</td >
</tr >
<tr >
<td colspan ="2" > <input type ="submit" value ="添加" > </td >
</tr >
</table >
</form >
</body >