⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adminitemservlet.java

📁 一套购物车项目。电子商务系统。实现了前台和后台的业务逻辑。
💻 JAVA
字号:
package com.softfz.jn0708.servlet;

import java.io.File;
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;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import com.softfz.jn0708.bean.ItemBean;
import com.softfz.jn0708.dao.ClassDAO;
import com.softfz.jn0708.dao.ItemDAO;
import com.softfz.jn0708.util.XPage;
import com.softfz.jn0708.util.FileHttpServletRequest;
import com.softfz.jn0708.util.FileRequestHandler;


/**
 * \商品管理Servlet
 * @author student
 *
 */
public class AdminItemServlet extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7633077744996936149L;



	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
		
	}

	
	/**
	 * doPost方法
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		String adminuname = (String)session.getAttribute("adminuname");	
		
		if(adminuname!=null){
			request.setCharacterEncoding("GBK");
			response.setCharacterEncoding("GBK");
//			因为该servlet中处理的请求包含有文件上传的内容,所以这部分的请求参数就无法获取所有需要转化request为FileHttpServletRequest
			FileHttpServletRequest req = null;
			try {
				req = FileRequestHandler.fileParse(request);
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//根据不同请求处理不同业务
			String task = req.getParameter("task");
			ItemDAO dao = new ItemDAO();
			ClassDAO classdao = new ClassDAO();
			List bClasslist = classdao.getBclass();
			List nClasslist = classdao.getNclass();
			request.setAttribute("bclasslist",bClasslist);
			request.setAttribute("nclasslist",nClasslist);		
			if(task==null){//默认是查询
				doquery(request,response);
			}else if(task.equals("add")){//添加商品			
				request.getRequestDispatcher("/admin/manager/item.jsp").forward(request,response);
			}else if(task.equals("doadd")){
				doadd(req,response);
			}else if(task.equals("edit")){//得到商品编辑页面显示的数据
				String itemid= request.getParameter("itemid");
				ItemBean item = dao.getItemById(itemid);
				request.setAttribute("item",item);						
				request.getRequestDispatcher("/admin/manager/item.jsp").forward(request,response);
			}else if(task.equals("doedit")){
				doedit(req,response);
			}else if(task.equals("del")){
			
				String contextPath = request.getContextPath();
				PrintWriter out =response.getWriter();
				
				String itemid = request.getParameter("itemid");
				boolean flag=dao.delete(itemid);


				String filepath = request.getParameter("filepath");
				if(itemid!=null && !itemid.trim().equals("")){
					String filepaths = this.getServletContext().getRealPath("/")+"/"+filepath;
					File file = new File(filepaths);
					if(file.isFile()){
						file.delete();
					}
				}
				//根据dao的返回值进行处理
				if(flag){
					
					out.print("<SCRIPT>");
					out.print("alert('删除商品成功');");
					out.print("location.href='"+contextPath+"/adminItemServlet';");
					out.print("</script>");
				}else{
					out.print("<script>");
					out.print("alert('删除商品失败');");
					out.print("history.back();");
					out.print("</script>");
				}
			}
		}else{
			request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
		}		
	}

	/**
	 * 修改商品信息方法
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doedit(FileHttpServletRequest request, HttpServletResponse response) throws IOException {
		ItemDAO dao = new ItemDAO();
//		封装Bean对象
		ItemBean bean = new ItemBean();		
		bean.parseRequest(request);
		FileItem file = request.getFileParameter("pic");		
		if (file.getSize() > 0) {
			String realpath = this.getServletContext().getRealPath("");// 上下文物理路径
			realpath = realpath.replace('\\', '/');			
			long key = System.currentTimeMillis();// 新的文件名称不包含扩展名
			String fulloldname = file.getName();// 旧的文件完整物理路径				
			fulloldname = fulloldname.replace('\\', '/');
			
			String filename = fulloldname.substring(fulloldname
					.lastIndexOf("/") + 1, fulloldname.length());// 旧文件名
			String extendname = filename.substring(filename
					.lastIndexOf("."), filename.length());// 扩展名
			
			String filepath = "upload/" + key + extendname;
			String fullnewname = realpath + "/" + filepath;
			
			// 删除旧文件即原来已经上传的商品图片
			String orgoldname = realpath + "/" + bean.getFilepath();
			File o = new File(orgoldname);
			if (o.isFile()) {
				o.delete();
			}
			try {
				file.write(new File(fullnewname));
				bean.setFilename(filename);
				bean.setFilepath(filepath);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		boolean flag = dao.update(bean);
		String contextPath = request.getContextPath();
		PrintWriter out =response.getWriter();	
		
		//根据dao的返回值进行处理
		if(flag){
			
			out.print("<SCRIPT>");
			out.print("alert('修改商品成功');");
			out.print("location.href='"+contextPath+"/adminItemServlet';");
			out.print("</script>");
		}else{
			out.print("<script>");
			out.print("alert('修改商品失败');");
			out.print("history.back();");
			out.print("</script>");
		}
	}

	/**
	 * 添加商品信息
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doadd(FileHttpServletRequest request, HttpServletResponse response) throws IOException {
		

		//把参数封装成JavaBean对象
		ItemDAO dao = new ItemDAO();
		//封装Bean对象
		ItemBean bean = new ItemBean();		
		bean.parseRequest(request);
//		 上传文件
		FileItem file = request.getFileParameter("pic");
		if (file.getSize() > 0) {
			String realpath = this.getServletContext().getRealPath("");// 上下文物理路径
			realpath = realpath.replace('\\', '/');
			long key = System.currentTimeMillis();// 新的文件名称不包含扩展名
			String fulloldname = file.getName();// 旧的文件完整物理路径
			fulloldname = fulloldname.replace('\\', '/');
			String filename = fulloldname.substring(fulloldname
					.lastIndexOf("/") + 1, fulloldname.length());// 旧文件名
			String extendname = filename.substring(filename.lastIndexOf("."),
					filename.length());// 扩展名
			String filepath = "upload/" + key + extendname;
			String fullnewname = realpath + "/" + filepath;
			try {
				file.write(new File(fullnewname));
				bean.setFilename(filename);
				bean.setFilepath(filepath);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		boolean flag = dao.create(bean);
		String contextPath = request.getContextPath();
		PrintWriter out =response.getWriter();
		//根据dao的返回值进行处理
		if(flag){
			
			out.print("<SCRIPT>");
			out.print("alert('添加商品成功');");
			out.print("location.href='"+contextPath+"/adminItemServlet';");
			out.print("</script>");
		}else{
			out.print("<script>");
			out.print("alert('添加商品失败');");
			out.print("history.back();");
			out.print("</script>");
		}
		
	}



	/**
	 * 查询商品信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doquery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	
		//获取页面参数			
 		String pagenum = request.getParameter("currentPage");
		String title = request.getParameter("title");
		String startprice = request.getParameter("startprice");
		String endprice = request.getParameter("endprice");
		String classid = request.getParameter("classid");
		String smalltypeid = request.getParameter("smalltypeid");
		ItemDAO dao = new ItemDAO();
//		ClassDAO classdao = new ClassDAO();
		int currentPage = 1;
		try {
			currentPage = Integer.parseInt(pagenum);
		} catch (Exception e) {
			currentPage=1;
		}		
		int count = 10;		
		ItemBean bean = new ItemBean();
		String path = request.getContextPath()+"/adminItemServlet?";
		String sql = "select count(itemid) from t_iteminfo where 1=1";
		if(title!=null&&!title.trim().equals("")){
			sql=sql+" and title like '%"+title+"%'";
			path = path+"title="+title+"&";
			bean.setTitle(title);
		}
		if((startprice!=null&&!startprice.trim().equals(""))&&(endprice!=null&&!endprice.trim().equals(""))){
			sql=sql+" and price between '"+startprice+"'" +" and '"+endprice+"'";
			path = path+"startprice="+startprice+"endprice="+endprice+"&";
			bean.setPrice(Double.parseDouble(startprice));
			bean.setEndprice(Double.parseDouble(endprice));
		}
		if(classid!=null&&!classid.trim().equals("")){
			sql=sql+" and classid = "+classid;
			path = path+"classid="+classid+"&";
			bean.setClassid(Integer.parseInt(classid));
		}
		if(smalltypeid!=null&&!smalltypeid.trim().equals("")){
			sql=sql+" and nclassid = "+smalltypeid;
			path = path+"nclassid = "+smalltypeid+"&";
			bean.setNclassid(Integer.parseInt(smalltypeid));
		}		
		List list = dao.getItemList(currentPage,count,bean);
		XPage page = new XPage(currentPage,count,sql);
		page.setPath(path);
		request.setAttribute("result",list);
		request.setAttribute("mpage",page);
		request.getRequestDispatcher("/admin/manager/list.jsp").forward(request,response);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -