添加商品
步骤分析:
1.在index.jsp添加一个超链接
跳转到add.jsp
2.add.jsp放入一个表单
3.表单提交到 AddProductServlet
封装数据
调用service完成保存操作
跳转到FindAllServlet
(请求转发和重定向)
有表单使用的时候若使用请求转发会出现重复提交
方案1:重定向
方案2:令牌机制
扩展:令牌机制
在添加页面上随机生成一个字符串,
放入session中一份,放入表单中一份
提交的时候在后台获取两个码
然后移除session中码(只使用一次)
然后判断两个码是否一致,
若不一致就是重复提交了
add.jsp
<%@page import="com.feizhu.utils.UUIDUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加商品</title>
</head>
<body>
<%
String code= UUIDUtils.getCode();
//将code放入session 后台需要验证
session.setAttribute("s_lingpai", code);
//将code放入pagecontext域中
pageContext.setAttribute("r_code", code);
%>
<form method="post" action="${pageContext.request.contextPath }/addProduct">
<input type="hidden" name="r_lingpai" value="${r_code }">
<table border="1" align="center">
<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="text" name="pdesc"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="保存"></td>
</tr>
</table>
</form>
</body>
</html>
Servlet
package com.feizhu.web.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
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 com.feizhu.domain.Product;
import com.feizhu.service.ProductService;
import com.feizhu.utils.UUIDUtils;
/**
* 添加商品
*/
public class AddProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
//扩展令牌机制
//获取session中令牌和提交过来的令牌
String r_lingpai=request.getParameter("r_lingpai");
String s_lingpai=(String) request.getSession().getAttribute("s_lingpai");
//移除session中的令牌
request.getSession().removeAttribute("s_lingpai");
//比较两个令牌
if(s_lingpai==null|| !s_lingpai.equals(r_lingpai)) {
//已经提交过了,生成错误提示信息放入request域中,到msg.jsp
request.setAttribute("msg","商品已经保存!");
request.getRequestDispatcher("/msg.jsp").forward(request, response);
return;
}
//封装数据
Product p= new Product();
try {
BeanUtils.populate(p, request.getParameterMap());
//设置pid
p.setPid(UUIDUtils.getId());
//设置时间
p.setPdate(new Date());
//调用Service完成添加操作
new ProductService().addProduct(p);
//页面跳转
//先用请求转发
request.getRequestDispatcher("/findAll").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("msg","添加商品失败");
request.getRequestDispatcher("/msg.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Service
/**
* 添加商品
*
* @param p
* @return
* @throws SQLException
*/
public void addProduct(Product p) throws SQLException {
new ProductDao().addProduct(p);
}
Dao
/**
* 添加商品
*
* @param p
* @return
* @throws SQLException
*/
public void addProduct(Product p) throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
/**
* `pid` varchar (96), `pname` varchar (150), `market_price` double ,
* `shop_price` double , `pimage` varchar (600), `pdate` date , `pdesc` varchar
* (765)
*/
String sql = "insert into product(pid,pname,market_price,shop_price,pdate,pdesc) values(?,?,?,?,?,?)";
qr.update(sql, p.getPid(), p.getPname(), p.getMarket_price(), p.getShop_price(), p.getPdate(), p.getPdesc());
}
Javabean:
package com.feizhu.domain;
import java.util.Date;
public class Product {
/**
* `pid` varchar (96),
`pname` varchar (150),
`market_price` double ,
`shop_price` double ,
`pimage` varchar (600),
`pdate` date ,
`pdesc` varchar (765)
*/
private String pid;
private String pname;
private Double market_price;
private Double shop_price;
private String pimage;
private Date pdate;
private String pdesc;
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 Date getPdate() {
return pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
}
备注:由于个人原因,本博客暂停更新。如有问题可联系本人,本人提供技术指导、学习方向、学习路线。本人微信wlp1156107728(添加注明来意) QQ1156107728(添加注明来意)