<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'productinfo.jsp' starting page
欢迎来到商品信息管理平台
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
My JSP 'prolist.jsp' starting page
商品的列表页面
package product;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class productfindallservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*调用业务层查找*/
try{
productservice ps=new productservice();
Listlist=ps.findall();
//数据保存传给jsp页面
request.setAttribute("list", list);
//页面跳转
request.getRequestDispatcher("/html/prolist.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
/*业务层查询所有商品的方法*/
public List findall() throws SQLException {
// TODO Auto-generated method stub
productdao pd=new productdao();
return pd.findall();
}
public List findall() throws SQLException {
// DAO中查询所有商品
QueryRunner qr=new QueryRunner(c3p0tool.getDataSource());
String sql="select *from product order by pdate desc";
Listlist=qr.query(sql, new BeanListHandler(product.class));
return list;
}
<%@page import="utils.UUIDutils"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
My JSP 'addproduct.jsp' starting page
<%
String token = UUIDutils.getUUID();//产生一个随机序号
session.setAttribute("token", token);//将随机序号存储到session的token中
%>
添加商品的页面
package utils;
import java.util.UUID;
//产生随机的字符串
public class UUIDutils {
public static String getUUID(){
return UUID.randomUUID().toString();
}
}
package product;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
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 utils.UUIDutils;
public class productaddservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
*
* 添加商品的servlet
*/
request.setCharacterEncoding("UTF-8");
//判断是否为重复提交
String token1=(String) request.getSession().getAttribute("token");//获得session中的token
String token2=request.getParameter("token");//获得表单提交的token
request.getSession().removeAttribute("token");//移除session中的token
if(!token2.equals(token1)){
request.setAttribute("msg", "亲,不能重复提交了哦");
request.getRequestDispatcher("/html/msg.jsp").forward(request, response);//跳转到提示页面
return;
}
Mapmap=request.getParameterMap();//将表单中的所有数封装在map中
//封装数据
product p=new product();
try {
BeanUtils.populate(p, map);//将map封装成product对象,map
public void save(product p) throws SQLException {
/*业务层保存商品*/
productdao pd=new productdao();
pd.save(p);
}
public void save(product p) throws SQLException {
// DAO中保存商品的方法
QueryRunner qr=new QueryRunner(c3p0tool.getDataSource());
String sql="insert into product values(?,?,?,?)";
qr.update(sql, p.getPid(),p.getPname(),p.getMarket_price(),p.getPdate());
}
package product;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class editproductservlet extends HttpServlet {
/*商品编辑的servlet*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收数据pid
String pid=request.getParameter("pid");
productservice ps=new productservice();
try {
product p=ps.findbyid(pid);//通过id查找到这个商品
p.setPid(pid);
request.setAttribute("product", p);//将查询到的product保存起来
request.getRequestDispatcher("/html/editproduct.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet( request, response);
}
}
public product findbyid(String pid) throws SQLException {
/*业务层根据ID来查找商品*/
productdao pd=new productdao();
return pd.findbyid(pid);
}
public product findbyid(String pid) throws SQLException {
// DAO中通过pid来查询商品
QueryRunner qr=new QueryRunner(c3p0tool.getDataSource());
String sql="select *from product where pid=?";
product p=qr.query(sql, new BeanHandler(product.class),pid);
return p;
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'editproduct.jsp' starting page
修改商品的页面
package product;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
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;
public class updateproductservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收数据
request.setCharacterEncoding("UTF-8");
Mapmap=request.getParameterMap();//将接收的的参数封装在map中
product p=new product();
try {
BeanUtils.populate(p, map);//将map封装在product中
p.setPdate(new Date());
//调用业务层处理数据
productservice ps=new productservice();
ps.update(p);
request.getRequestDispatcher("/productfindallservlet").forward(request, response);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public void update(product p) throws SQLException {
// 业务层修改商品的方法
productdao pd=new productdao();
pd.update(p);
}
public void update(product p) throws SQLException {
// DAO中修改商品
QueryRunner qr=new QueryRunner(c3p0tool.getDataSource());
String sql="update product set pname=?,market_price=? where pid=?";
qr.update(sql,p.getPname(),p.getMarket_price(),p.getPid());
}
package product;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class deleteproductservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//商品删除传入pid
String pid=request.getParameter("pid");
productservice ps=new productservice();
try {
ps.delete(pid);
request.getRequestDispatcher("/productfindallservlet").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public void delete(String pid) throws SQLException {
// 业务层删除商品的方法
productdao pd=new productdao();
pd.delete(pid);
}
public void delete(String pid) throws SQLException {
// DAO中删除商品的方法
QueryRunner qr=new QueryRunner(c3p0tool.getDataSource());
String sql="delete from product where pid=?";
qr.update(sql,pid);
}
package product;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class productdeleteallservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收多个ID
String[] ids=request.getParameterValues("ids");
productservice ps=new productservice();
ps.deleteall(ids);
request.getRequestDispatcher("/productfindallservlet").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet( request, response);
}
}
public void deleteall(String[] ids) {
// 业务层删除所有商品的方法
Connection con = c3p0tool.getConnection();
try {
con.setAutoCommit(false);//调用事物同时删除
productdao pd=new productdao();
for(String id:ids){
pd.delete(con,id);
}
DbUtils.commitAndCloseQuietly(con);//提交
} catch (Exception e) {
// 回滚
DbUtils.rollbackAndCloseQuietly(con);
}
}
public void delete(Connection con, String id) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr=new QueryRunner();
String sql="delete from product where pid=?";
qr.update(con,sql,id);
}
public void delete(Connection con, String id) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr=new QueryRunner();
String sql="delete from product where pid=?";
qr.update(con,sql,id);
}
package product;
import java.io.IOException;
import java.io.PrintWriter;
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;
public class productsearchservlet extends HttpServlet {
/**
* 商品的模糊查询
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收数据
request.setCharacterEncoding("UTF-8");
String pname=request.getParameter("pname");
productservice ps=new productservice();
try {
Listlist=ps.search(pname);//通过pname来查询
request.setAttribute("list", list);//将list保存传给prolist.jsp
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("/html/prolist.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public List search(String pname) throws SQLException {
// 业务层查询商品
productdao pd=new productdao();
return pd.search(pname);
}
public List search(String pname) throws SQLException {
// DAO中查询商品
QueryRunner qr=new QueryRunner(c3p0tool.getDataSource());
String sql="select *from product where pname like ? order by pdate desc";
List p=qr.query(sql, new BeanListHandler(product.class),"%"+pname+"%");
return p;
}
package product;
import java.io.IOException;
import java.io.PrintWriter;
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;
public class productfindbypageservlet extends HttpServlet {
/*
* 分页查询商品的servlet
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收数据
int currentpage=Integer.parseInt(request.getParameter("currentpage"));
//调用业务层处理数据
productservice ps=new productservice();
try {
pagebean pb=ps.findbypage(currentpage);
//将写好的数据保存起来
request.setAttribute("pb", pb);
request.getRequestDispatcher("/html/productpage.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package product;
import java.util.List;
/*
*封装分页数据的JAVABEAN
*/
public class pagebean {
private int currpage;//当前页数
private int totalcount;//总记录数
private int pagesize;//每页的记录数
private Listlist;//每页的数据集合
private int totalpage;
public int getCurrpage() {
return currpage;
}
public void setCurrpage(int currpage) {
this.currpage = currpage;
}
public int getTotalcount() {
return totalcount;
}
public void setTotalcount(int totalcount) {
this.totalcount = totalcount;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getTotalpage() {
return totalpage;
}
public void setTotalpage(int totalpage) {
this.totalpage = totalpage;
}
}
public pagebean findbypage(int currentpage) throws SQLException {
// 业务层分页查询
pagebean pb=new pagebean();
//设置当前页数
pb.setCurrpage(currentpage);
//设置每页显示的最大记录
int pagesize=3;
pb.setPagesize(pagesize);
productdao pd=new productdao();
//将开始序号和pagesize传入DAO中
int begin=(currentpage-1)*pagesize;
Listlist=pd.findbypage(begin,pagesize);//DAO中返回一个list集合
//设置list集合
pb.setList(list);
//设置总序列数
int totalcount=pd.findcount();
pb.setTotalcount(totalcount);
double tc=(double)totalcount;
double ps=(double)pagesize;
//设置总页数
int totalpage=(int) Math.ceil(tc/ps);
pb.setTotalpage(totalpage);
return pb;
}
public List findbypage(int begin, int pagesize) throws SQLException {
// 显示当前页的信息
QueryRunner qr=new QueryRunner(c3p0tool.getDataSource());
String sql="select * from product order by pdate desc limit ?,?";
List list = qr.query(sql, new BeanListHandler(product.class),begin,pagesize);
return list;
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
My JSP 'prolist.jsp' starting page
商品的列表页面