package cn.zzk.servlet;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import cn.zzk.dao.ProductDao;
import cn.zzk.po.Page;
import cn.zzk.po.Product;
/**
* Servlet implementation class ProductServlet
*/
@WebServlet("/ProductServlet")
public class ProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
ProductDao productDao = new ProductDao();
public ProductServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
switch (method) {
case "all":
queryAllProduct(request, response);
break;
case "add":
addProduct(request, response);
break;
case "del":
delProduct(request, response);
break;
case "update":
updateProduct(request, response);
break;
case "byid":
queryProductById(request, response);
break;
case "batchDel":
batchDelProduct(request, response);
break;
case "byPage":
queryProductByPage(request,response);
break;
}
}
private void queryProductByPage(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//得到当前页数
String currPage=request.getParameter("currentPage");
int currentPage=1;
if(currPage!=null)
{
System.out.println(currPage);
currentPage=Integer.parseInt(currPage);
}
//得到总记录数量
int totalCount=0;
List<Product> products=productDao.queryAllProducts();
if(products!=null)
{
totalCount=products.size();
}
//页面大小——每页显示几条记录
int pageSize=5;
//创建页处理对象
Page page=new Page(currentPage,pageSize,totalCount);
//得到当前页显示的记录索引(从0开始)
int beginIndex=page.getBeginIndex();
//得到当前页的商品列表
List<Product> productByPage = productDao.queryProductByPage(beginIndex, pageSize);
//将商品信息和页对象放入request作用域,供页面展示使用
request.setAttribute("productList", productByPage);
request.setAttribute("page", page);
//转向页面
request.getRequestDispatcher("/product/m_productMain.jsp").forward(request, response);
}
private void queryAllProduct(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<Product> products = productDao.queryAllProducts();
request.setAttribute("productList", products);
request.getRequestDispatcher("/ProductServlet?method=byPage").forward(request, response);
}
// 增加商品(涉及上传图片文件)
private void addProduct(HttpServletRequest request,
HttpServletResponse response) throws IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// 判断是带文件上传的表单?还是普通表单
boolean isuploadSuccess = false; // 设置上传成功的标志
File imageFile = null; // 图片文件对象
String proname = null; // 商品名称
String pronumber = null; // 商品数量
String proprice = null; // 商品价格
String prosupplier = null; // 商品供应商
String propic = null; // 商品的图片文件名
String destFileName = null; // 重新组织的商品文件名
try {
String uploaddir = request.getServletContext().getRealPath(
"/images");
// 设置上传路径字符串
File upFile = new File(uploaddir); // 创建路径对应的文件对象
if (!upFile.exists()) { // 如果路径不存在,就逐级创建
upFile.mkdirs();
}
if (isMultipart == true) { // 若是带文件上传的表单,继续
FileItemFactory factory = new DiskFileItemFactory(); // 创建文件项工厂
ServletFileUpload upload = new ServletFileUpload(factory); // 创建文件上传对象
List<FileItem> items; // 表单项列表
items = upload.parseRequest(request); // 解析请求对象,得到各个表单项
// 对表单里的各个项目进行分析:
// 如果是普通文本框,则取得其字段名称,根据名称获得其值
// 如果是文件上传项,则取得文件名,对文件名进行处理,然后上传
Iterator<FileItem> it = items.iterator();
while (it.hasNext()) { // 单文件上传
FileItem item = (FileItem) it.next();
// 处理普通文本
String fileldName = item.getFieldName(); // 得到域名称
switch (fileldName) // 根据名称取得其数据,由二进制数据转换成UTF-8编码
{
case "proname":
proname = new String(item.getString().getBytes(
"iso-8859-1"), "utf-8");
break;
case "proprice":
proprice = new String(item.getString().getBytes("iso-8859-1"), "utf-8");