基于javaweb+mysql的jsp+servlet美食商城管理系统(java+jsp+bootstrap+jquery+mysql)

该项目是一个融合了jsp、servlet、mysql的在线美食商城系统,包括用户登录、管理员管理、美食分类、制作方法、库存管理等多个功能模块。管理员可进行会员、资讯、链接、美食等管理,用户能查看商品、收藏、下单等操作。系统使用Eclipse、IDEA等开发工具,支持分页、数据库操作、Ajax交互等技术。
摘要由CSDN通过智能技术生成

基于javaweb+mysql的jsp+servlet美食商城管理系统(java+jsp+bootstrap+jquery+mysql)

运行环境

Java≥8、MySQL≥5.7、Tomcat≥8

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

基于javaweb+mysql的JSP+Servlet美食商城管理系统(java+jsp+bootstrap+jquery+mysql)

项目介绍

本项目分为前台与后台,前台由用户登录,后台由管理员登录;

管理员角色包含以下功能:

管理员登录,会员管理,资讯管理,链接管理,图片管理,美食类别管理,制作方法管理,美食管理,美食库存管理,订单管理,留言管理等功能。

用户角色包含以下功能: 用户登录,按分类查看菜品,查看站内资讯,提交留言,查看商品详情,加入购物车,提交订单,付款,查看我的订单等功能。

环境需要

1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本; 6.是否Maven项目:否;

技术栈

JSP+CSS+JavaScript+jQuery+Bootstrap+mysql

使用说明

  1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven; 若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行; 3. 将项目中util/SimpleDataSource.java配置文件中的数据库配置改为自己的配置; 4. 运行项目,输入localhost:8080/jsp_zhctms_site 登录 注:tomcat中配置项目路径必须为jsp_zhctms_site 用户账号/密码: user/123456 管理员账号/密码:admin/admin
				go("/admin/imgadv.jsp", request, response);

			} catch (Exception e1) {
				e1.printStackTrace();
				request.setAttribute("error", "");
				request.getRequestDispatcher("/admin/imgadv.jsp").forward(
						request, response);
			}
		}

		// 美食制作方法
		if (ac.equals("ppinfoadd")) {
			String ppname = request.getParameter("ppname");
			String delstatus = "0";
			dao.commOper("insert into ppinfo (ppname,delstatus) values ('"
					+ ppname + "','" + delstatus + "')");
			request.setAttribute("suc", "操作成功!");
			go("/admin/ppinfo.jsp", request, response);
		}
		// 美食制作方法编辑
		if (ac.equals("ppinfoedit")) {
			String id = request.getParameter("id");
			String ppname = request.getParameter("ppname");
			dao.commOper("update ppinfo set  ppname='" + ppname + "' where id="
					+ id);
			request.setAttribute("suc", "操作成功!");
			go("/admin/ppinfo.jsp", request, response);
		}
		// 会员收藏美食
		if (ac.equals("addfav")) {
			String goodid = request.getParameter("goodid");
			String page = request.getParameter("page");
			if (member == null) {
				go("/login.jsp", request, response);
			} else {
				ArrayList cklist = (ArrayList) dao
						.select("select * from fav where memberid='"
								+ member.get("id") + "' and goodid='" + goodid
								+ "'");
				if (cklist.size() == 0) {
					dao.commOper("insert into fav (goodid,memberid) values ('"
							+ goodid + "','" + member.get("id") + "')");
					request.setAttribute("suc", "收藏成功!");
					go("/" + page, request, response);
				} else {
					request.setAttribute("suc", "此美食已收藏过!");
					go("/" + page, request, response);
				}
			}
		}
		// 取消收藏
							"' class='ls'>").append("上页")
					.append("</a>");
		} else {
			buf.append("上页");

		}
		buf.append("&nbsp;&nbsp;");

		int currentSegment = this.currentPage % segment == 0 ? this.currentPage
				/ segment : this.currentPage / segment + 1;

		/*for (int i = 1; i <= this.pageNumber; i++) {
			if (this.currentPage == i)
				buf.append("<font color='red'>").append(i).append("</font>");

			else
				buf.append("<a href='").append(this.path).append(
						"&currentPage=").append(i).append(parameter).append(
						"' class='ls'>[").append(i).append(
						"]</a>");
		}*/

		buf.append("&nbsp;&nbsp;");
		if (this.currentPage < this.pageNumber) {
			buf.append("<a href='").append(this.path).append("&currentPage=")
					.append(currentPage + 1).append(parameter).append(
							"' class='ls'>").append("下页")
					.append("</a>");
		} else {

			buf.append("下页");

		}

		buf.append("&nbsp;&nbsp;&nbsp;&nbsp;");
		if (this.currentPage == this.pageNumber)
			buf.append("末页&nbsp;&nbsp;");
		else
			buf.append("<a href='").append(this.path).append("&currentPage=")
					.append(this.pageNumber).append(parameter).append(
							"' class='ls'>").append("末页")
					.append("</a></font>&nbsp;&nbsp;");
		// 
		// for (int i = 0; i < this.pageNumber; i++) {
		// if (this.currentPage == i + 1) {
		// buf.append("<font color=red>[" + (i + 1) + "]</font>").append(
		// "&nbsp;");
		// } else {
		// buf.append("<a href='").append(this.path).append(
		// "&currentPage=").append(i + 1).append(parameter)
		// .append("' style='TEXT-DECORATION:none'>").append(
		// "[" + (i + 1) + "]").append("</a>&nbsp;");
		// }

public class MainCtrl extends HttpServlet {

	public MainCtrl() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

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

	MainMethod responses = new MainMethod();

	public void go(String url, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getRequestDispatcher(url).forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void gor(String url, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.sendRedirect(url);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		StringBuffer   sb   =   new   StringBuffer(50);   
		 response.setContentType("application/x-msdownload;charset=utf-8");   
        try {
			response.setHeader("Content-Disposition",   new   String(sb.toString()   
			         .getBytes(),   "ISO8859-1"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		 String filename = request.getParameter("filename");
		  if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0){
		     try {
				filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		 else 
		      if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){
		           try {
					filename = URLEncoder.encode(filename, "UTF-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		      }
		 response.setContentType("text/plain");
		 response.setHeader("Location",filename);
		 response.reset();
		 response.setHeader("Cache-Control", "max-age=0" );
		 response.setHeader("Content-Disposition", "attachment; filename=" + filename);
		   

		    try {
				       BufferedInputStream bis = null;
					+ "','"
					+ shrtel + "','" + shraddr + "')");
			// 删除购物车下的美食
			dao.commOper("delete from car where mid=" + memberid);
			request.setAttribute("suc", "订单生成成功!");
			go("/mydd.jsp", request, response);
		}

		//
		if (ac.equals("pay")) {
			String ddid = request.getParameter("ddid");
			String fkstatus = "已付款";
			String fhstatus = "待发货";
			String shstatus = "待收货";
			dao.commOper("update ddinfo set fkstatus='" + fkstatus
					+ "',fhstatus='" + fhstatus + "',shstatus='" + shstatus
					+ "' where id=" + ddid);
			request.setAttribute("suc", "支付成功!");
			go("/mydd.jsp?ddid=" + ddid, request, response);
		}
		// 订单发货
		if (ac.equals("ddfh")) {
			String ddid = request.getParameter("ddid");
			String wlcompany = request.getParameter("wlcompany");
			String wlno = request.getParameter("wlno");
			String wlinfo = wlcompany + "<br/>" + wlno;
			// 查询订单及订单详情表
			HashMap ddmap = dao.select("select * from ddinfo where id=" + ddid)
					.get(0);
			ArrayList<HashMap> dddetaillist = (ArrayList<HashMap>) dao
					.select("select * from dddetail where ddno="
							+ ddmap.get("ddno"));
			boolean flag = true;// 用作订单美食库存校验结果
			for (HashMap dddetailmap : dddetaillist) {
				// 如果其中某个美食的数量大于其库存量 则置 FLASE标识
				if (Integer.valueOf(dddetailmap.get("sl").toString()) > Info
						.getkc(dddetailmap.get("goodid").toString())) {
					flag = false;
				}
			}
			if (flag) {
				dao.commOper("update ddinfo set fhstatus='已发货',wlinfo='"
						+ wlinfo + "' where id=" + ddid);
				// 发货后减库存
				for (HashMap dddetailmap : dddetaillist) {
					dao.commOper("insert into kcrecord (gid,happennum,type,savetime) values "
							+ "('"
							+ dddetailmap.get("goodid")
							+ "','"
							+ dddetailmap.get("sl")
							+ "','out','"
							+ Info.getDateStr() + "')");
				}
		}

	}
	
	
	
	

	
	

	/**
	 * 
	 * @param 下一页的分页链接
	 * @param 一页最大记录数
	 * @param 当前HttpServletRequest对象
	 * @param 数据库操作对象
	 */
	public static PageManager getPage(String path, int pageSize,
			HttpServletRequest request) {
		return new PageManager(path, pageSize, request);

	}

	/**
	 * 
	 * 
	 * @param hql语句
	 * 
	 */
	public void doList(String hql) {
        String sql = "select count(*)  "+hql.substring(hql.indexOf("from"));
        sql = sql.substring(0,sql.indexOf("order"));
        
		this.count = this.dao.getInt(sql);
		if (this.count != 0) {
			this.pageNumber = count % this.pageSize == 0 ? this.count
					/ this.pageSize : this.count / this.pageSize + 1;
			if (this.currentPage > this.pageNumber)
				this.currentPage = (int) this.pageNumber;

		}
		this.request.getSession().setAttribute("currentPage",
				String.valueOf(this.currentPage));
		this.collection = this.dao.select(hql,
				this.currentPage , this.pageSize);

		this.refreshUrl();
	}

	/**
	 * 
			if (member != null) {
				String memberid = member.get("id").toString();
				ArrayList<HashMap> addrlist = (ArrayList<HashMap>) dao
						.select("select * from addr where delstatus='0' and memberid="
								+ member.get("id"));
				if (addrlist.size() == 0) {
					out.print("false");
				} else {
					shraddr = addrlist.get(0).get("id").toString();
					HashMap gmap = dao.select(
							"select * from goods where id=" + gid).get(0);
					String price = gmap.get("price").toString();
					if (gmap.get("tprice") != null
							&& !gmap.get("tprice").equals("")) {
						price = gmap.get("tprice").toString();
					}
					ddprice = Double.valueOf(price) * sl;
					// 直接生成订单
					dao.commOper("insert into ddinfo (ddno,memberid,ddprice,fhstatus,savetime,shstatus,wlinfo,fkstatus,shrname,shrtel,shraddr) values "
							+ "('"
							+ ddno
							+ "','"
							+ memberid
							+ "','"
							+ ddprice
							+ "','"
							+ fhstatus
							+ "','"
							+ savetime
							+ "','"
							+ shstatus
							+ "','"
							+ wlinfo
							+ "','"
							+ fkstatus
							+ "','"
							+ shrname + "','" + shrtel + "','" + shraddr + "')");
					dao.commOper("insert into dddetail (ddno,goodid,sl) values ('"
							+ ddno + "','" + gid + "','" + sl + "') ");

					out.print("true");
				}
			} else {
				out.print("false");
					upload.setSizeMax(100 * 1024 * 1024);
					List items = new ArrayList();
					items = upload.parseRequest(request);
					FileItem fileItem = (FileItem) items.get(0);
					if (fileItem.getName() != null && fileItem.getSize() != 0) {
						if (fileItem.getName() != null
								&& fileItem.getSize() != 0) {
							File fullFile = new File(fileItem.getName());
							filename = Info.generalFileName(fullFile.getName());
							File newFile = new File(
									request.getRealPath("/upfile/") + "/"
											+ filename);
							try {
								fileItem.write(newFile);
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
						}
					}
				}

				go("/js/uploadimg.jsp?filename=" + filename, request, response);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

		// 库存预警数值设置
		if (ac.equals("kcwarningset")) {
			String num = request.getParameter("num");
			String id = request.getParameter("id");
			dao.commOper("update kcwarnning set num=" + Integer.parseInt(num)
					+ " where id=" + id);
			request.setAttribute("suc", "");
			go("/admin/kcwarningset.jsp", request, response);
		}
		// 美食入库
		if (ac.equals("kcinto")) {
			String pid = request.getParameter("pid");
			String num = request.getParameter("num");
			String type = request.getParameter("type");
			String reason = request.getParameter("reason");
			String savetime = Info.getDateStr();
			dao.commOper("insert into kcrecord (pid,num,type,reason,savetime) values"
	    public static int dayToday(String DATE1, String DATE2) {
	       int i = 0;
	       DATE1 = DATE1.substring(0,DATE1.indexOf(" "));
	       DATE2 = DATE2.substring(0,DATE2.indexOf(" "));
	       
	       String[] d1 = DATE1.split("-");
	       if(d1[1].length()==1)
	       {
	    	   DATE1 = d1[0]+"-0"+d1[1];
	       }else{
	    	   DATE1 = d1[0]+"-"+d1[1];
	       }
	       
	       if(d1[2].length()==1)
	       {
	    	   DATE1 = DATE1+"-0"+d1[2];
	       }else{
	    	   DATE1 = DATE1+"-"+d1[2];
	       }
	       
	       String[] d2 = DATE2.split("-");
	       if(d2[1].length()==1)
	       {
	    	   DATE2 = d2[0]+"-0"+d2[1];
	       }else{
	    	   DATE2 = d2[0]+"-"+d2[1];
	       }
	       
	       if(d2[2].length()==1)
	       {
	    	   DATE2 = DATE2+"-0"+d2[2];
	       }else{
	    	   DATE2 = DATE2+"-"+d2[2];
	       }
	       
	       
	       for(int j=0;j<10000;j++)
	       {
	    	i=j;
	    	String gday = Info.getDay(DATE1, j);
	    	if(gday.equals(DATE2))
	    	{
	    		break;
	    	}
	       }
	        return i;
	    }
	    
	   
	    

	    /**
	 	 * 比较时间大小
					request.setAttribute("suc", "此美食已收藏过!");
					go("/" + page, request, response);
				}
			}
		}
		// 取消收藏
		if (ac.equals("delfav")) {
			String goodid = request.getParameter("goodid");
			String page = request.getParameter("page");
			dao.commOper("delete from fav where memberid='" + member.get("id")
					+ "' and goodid='" + goodid + "'");
			request.setAttribute("suc", "取消收藏成功!");
			go("/" + page, request, response);
		}
		// 新增收货地址
		if (ac.equals("addradd")) {
			String memberid = member.get("id").toString();
			String shr = request.getParameter("shr");
			String shrtel = request.getParameter("shrtel");
			String shraddr = request.getParameter("shraddr");
			String delstatus = "0";
			dao.commOper("insert into addr (memberid,shr,shrtel,shraddr,delstatus) values "
					+ "('"
					+ memberid
					+ "','"
					+ shr
					+ "','"
					+ shrtel
					+ "','"
					+ shraddr + "','" + delstatus + "') ");
			request.setAttribute("suc", "操作成功!");
			go("/addr.jsp", request, response);
		}

		// 检查用户名唯一性AJAX 系统用户
		if (ac.equals("usernamecheck")) {
			String username = request.getParameter("username");
			ArrayList cklist = (ArrayList) dao
					.select("select * from sysuser where username='" + username
							+ "' and delstatus='0' ");
			if (cklist.size() > 0) {
				out.print("false");
			} else {
				out.print("true");
			}
		}

		if (ac.equals("useradd")) {
					+ "','"
					+ addr
					+ "','"
					+ ybcode
					+ "','"
					+ qq
					+ "','" + tel + "','" + delstatus + "','" + savetime + "')");
			request.setAttribute("suc", "注册成功");
			go("/reg.jsp", request, response);
		}

		// 会员修改个人信息
		if (ac.equals("memberinfo")) {
			String id = request.getParameter("id");
			String upass = request.getParameter("upass");
			String email = request.getParameter("email") == null ? "" : request
					.getParameter("email");
			String tname = request.getParameter("tname") == null ? "" : request
					.getParameter("tname");
			String sex = request.getParameter("sex") == null ? "" : request
					.getParameter("sex");
			String addr = request.getParameter("addr") == null ? "" : request
					.getParameter("addr");
			String ybcode = request.getParameter("ybcode") == null ? ""
					: request.getParameter("ybcode");
			String qq = request.getParameter("qq") == null ? "" : request
					.getParameter("qq");
			String tel = request.getParameter("tel") == null ? "" : request
					.getParameter("tel");
			dao.commOper("update member set upass='" + upass + "',email='"
					+ email + "',tname='" + tname + "',sex='" + sex
					+ "',addr='" + addr + "',ybcode='" + ybcode + "',qq='" + qq
					+ "',tel='" + tel + "' where id=" + id);
			request.setAttribute("suc", "会员信息修改成功!");
			go("/grinfo.jsp", request, response);
		}

		// 会员登录
		if (ac.equals("frontlogin")) {
			String uname = request.getParameter("uname");
			String upass = request.getParameter("upass");
			ArrayList cklist = (ArrayList) dao
					.select("select * from member where uname='" + uname

}
package control;

public class Upload extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Upload() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	// 总记录数
	protected long count;

	// 数据
	protected Collection collection;

	// 数据查询对象
	protected CommDAO dao = new CommDAO();

	// 表现层代码
	protected String info;

	// 请求路径
	protected String path;

	// 服务器请求对象
	protected HttpServletRequest request;

	/*
	 * 仅仅只是加到路径中去
	 */
	protected String parameter = "";

	/**
	 * 
	 * @param 下一页的分页链接
	 * @param 一页最大记录数
	 * @param 当前HttpServletRequest对象
	 * @param 数据库操作对象
	 */
	protected PageManager(String path, int pageSize, HttpServletRequest request) {
		// 任意一个dao都行
		this.currentPage = 1;
		this.pageNumber = 1;
		this.count = 0;
		this.pageSize = pageSize <= 0 ? DEFAULTPAGESIZE : pageSize;
		this.request = request;
		this.path = path;

		request.setAttribute("page", this);

		try {
					dao.commOper(sql);
					request.setAttribute("suc", "");
					go("/admin/imgadvadd.jsp", request, response);
				}
			} catch (Exception e1) {
				e1.printStackTrace();
				request.setAttribute("no", "");
				request.getRequestDispatcher("/admin/imgadvadd.jsp").forward(
						request, response);
			}
		}
		// 编辑图片
		if (ac.equals("imgadvedit")) {
			String id = request.getParameter("id");
			HashMap map = dao.select("select * from imgadv where id=" + id)
					.get(0);
			try {
				String img = map.get("filename").toString();
				request.setCharacterEncoding("utf-8");
				RequestContext requestContext = new ServletRequestContext(
						request);
				if (FileUpload.isMultipartContent(requestContext)) {

					DiskFileItemFactory factory = new DiskFileItemFactory();
					factory.setRepository(new File(request
							.getRealPath("/upfile/") + "/"));
					ServletFileUpload upload = new ServletFileUpload(factory);
					upload.setSizeMax(100 * 1024 * 1024);
					List items = new ArrayList();
					items = upload.parseRequest(request);

					FileItem fileItem = (FileItem) items.get(0);
					if (fileItem.getName() != null && fileItem.getSize() != 0) {
						if (fileItem.getName() != null
								&& fileItem.getSize() != 0) {
							File fullFile = new File(fileItem.getName());
							img = Info.generalFileName(fullFile.getName());
							File newFile = new File(
									request.getRealPath("/upfile/") + "/" + img);
							try {
								fileItem.write(newFile);
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
						}
					}
				}
				String sql = "update imgadv set filename='" + img
						+ "' where id=" + id;
				if (FileUpload.isMultipartContent(requestContext)) {

					DiskFileItemFactory factory = new DiskFileItemFactory();
					factory.setRepository(new File(request
							.getRealPath("/upfile/") + "/"));
					ServletFileUpload upload = new ServletFileUpload(factory);
					upload.setSizeMax(100 * 1024 * 1024);
					List items = new ArrayList();
					items = upload.parseRequest(request);

					FileItem fileItem = (FileItem) items.get(0);
					if (fileItem.getName() != null && fileItem.getSize() != 0) {
						if (fileItem.getName() != null
								&& fileItem.getSize() != 0) {
							File fullFile = new File(fileItem.getName());
							img = Info.generalFileName(fullFile.getName());
							File newFile = new File(
									request.getRealPath("/upfile/") + "/" + img);
							try {
								fileItem.write(newFile);
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
						}
					}
				}
				String sql = "update imgadv set filename='" + img
						+ "' where id=" + id;
				dao.commOper(sql);
				request.setAttribute("suc", "");
				go("/admin/imgadvedit.jsp?id=" + id, request, response);
			} catch (Exception e1) {
				e1.printStackTrace();
				request.setAttribute("error", "");
				request.getRequestDispatcher("/admin/imgadvedit.jsp?id=" + id)
						.forward(request, response);
			}
		}

		// 检查用户名唯一性AJAX 会员注册
		if (ac.equals("memberunamecheck")) {
			String uname = request.getParameter("username");
			ArrayList cklist = (ArrayList) dao
					.select("select * from member where uname='" + uname
							+ "' and delstatus='0' ");
			if (cklist.size() > 0) {
				out.print("false");

			} else {
				out.print("true");
			}
		}
		// 如果session不为空,则可以浏览其他页面
		String url = request.getServletPath();
		//System.out.println(url);
		String path = request.getRequestURI();
		//这里判断目录,后缀名,当然也可以写在web.xml中,用url-pattern进行拦截映射
		if ((!request.getServletPath().equals("/admin/login.action"))
				&& (!request.getServletPath().equals("/admin/login.jsp"))
				&& (!request.getServletPath().equals("/admin/relogin.jsp"))
				&& (!request.getServletPath().equals("/admin/lib/font-awesome/css/font-awesome.css"))
				&& (!request.getServletPath().equals("/admin/lib/bootstrap/js/bootstrap.js"))
				&& (!request.getServletPath().equals("/admin/lib/jquery-1.7.2.min.js"))
				&& (!request.getServletPath().equals("/admin/stylesheets/theme.css"))
				&& (!request.getServletPath().equals("/admin/lib/bootstrap/css/bootstrap.css"))
				) {
			// 登陆页面无需过滤
			if(path.indexOf("/admin/login.jsp") > -1) {
				chain.doFilter(request, response);
				return;
			}
			
			

			if (session.getAttribute("admin") == null) {
				session.invalidate();
				response.setContentType("text/html;charset=gb2312");
				PrintWriter out = response.getWriter();
				out.println("<script language='javascript' type='text/javascript'>");
				out.println("alert('由于你长时间没有操作,导致Session失效!请你重新登录!');parent.location.href='" + request.getContextPath() + "/admin/login.jsp'");
				out.println("</script>");
			} else {
				chain.doFilter(request, response);
			}
		} else {
			chain.doFilter(request, response);
		}

	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

				request.setAttribute("error", "");
				request.getRequestDispatcher("/admin/imgadvedit.jsp?id=" + id)
						.forward(request, response);
			}
		}

		// 检查用户名唯一性AJAX 会员注册
		if (ac.equals("memberunamecheck")) {
			String uname = request.getParameter("username");
			ArrayList cklist = (ArrayList) dao
					.select("select * from member where uname='" + uname
							+ "' and delstatus='0' ");
			if (cklist.size() > 0) {
				out.print("false");

			} else {
				out.print("true");
			}
		}

		// 检查美食的库存
		if (ac.equals("checkgoodkc")) {
			String gid = request.getParameter("gid");
			String sl = request.getParameter("sl");
			if (Integer.valueOf(sl) > Info.getkc(gid)) {
				out.write("1");
			} else {
				out.write("0");
			}
		}
		// 美食加入购物车
		if (ac.equals("tocar")) {
			String gid = request.getParameter("gid");
			int sl = Integer.valueOf(request.getParameter("sl"));
			if (member != null) {
				String mid = member.get("id").toString();
				// 检查该人的购物车是否有该物品
				ArrayList<HashMap> cklist = (ArrayList<HashMap>) dao
						.select("select * from car where mid='" + mid
								+ "' and gid='" + gid + "'");
				if (cklist.size() > 0) {
					dao.commOper("update car set sl=sl+" + sl + " where mid='"
							+ mid + "' and gid='" + gid + "' ");
				} else {
					dao.commOper("insert into car (gid,sl,mid) values ('" + gid
							+ "','" + sl + "','" + mid + "')");
				}
				out.print("true");
			} else {
				out.print("false");
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		StringBuffer   sb   =   new   StringBuffer(50);   
		 response.setContentType("application/x-msdownload;charset=utf-8");   
        try {
				request.setCharacterEncoding("utf-8");
				RequestContext requestContext = new ServletRequestContext(
						request);
				if (FileUpload.isMultipartContent(requestContext)) {

					DiskFileItemFactory factory = new DiskFileItemFactory();
					factory.setRepository(new File(request
							.getRealPath("/upfile/") + "/"));
					ServletFileUpload upload = new ServletFileUpload(factory);
					upload.setSizeMax(100 * 1024 * 1024);
					List items = new ArrayList();
					items = upload.parseRequest(request);
					goodname = ((FileItem) items.get(0)).getString();
					goodname = Info.getUTFStr(goodname);
					fid = ((FileItem) items.get(1)).getString();
					fid = Info.getUTFStr(fid);
					sid = ((FileItem) items.get(2)).getString();
					sid = Info.getUTFStr(sid);
					goodpp = ((FileItem) items.get(3)).getString();
					goodpp = Info.getUTFStr(goodpp);
					price = ((FileItem) items.get(4)).getString();
					price = Info.getUTFStr(price);
					note = ((FileItem) items.get(6)).getString();
					note = Info.getUTFStr(note);
					FileItem fileItem = (FileItem) items.get(5);
					if (fileItem.getName() != null && fileItem.getSize() != 0) {
						if (fileItem.getName() != null
								&& fileItem.getSize() != 0) {
							File fullFile = new File(fileItem.getName());
							filename = Info.generalFileName(fullFile.getName());
							File newFile = new File(
									request.getRealPath("/upfile/") + "/"
											+ filename);
							try {
								fileItem.write(newFile);
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
						}
					}
	 */
	public static PageManager getPage(String path, int pageSize,
			HttpServletRequest request) {
		return new PageManager(path, pageSize, request);

	}

	/**
	 * 
	 * 
	 * @param hql语句
	 * 
	 */
	public void doList(String hql) {
        String sql = "select count(*)  "+hql.substring(hql.indexOf("from"));
        sql = sql.substring(0,sql.indexOf("order"));
        
		this.count = this.dao.getInt(sql);
		if (this.count != 0) {
			this.pageNumber = count % this.pageSize == 0 ? this.count
					/ this.pageSize : this.count / this.pageSize + 1;
			if (this.currentPage > this.pageNumber)
				this.currentPage = (int) this.pageNumber;

		}
		this.request.getSession().setAttribute("currentPage",
				String.valueOf(this.currentPage));
		this.collection = this.dao.select(hql,
				this.currentPage , this.pageSize);

		this.refreshUrl();
	}

	/**
	 * 
	 * @param 查询条件集合
	 *            如没有条件只是列表就不使用这个方法
	 */
	public void addParameter(List parameter) {

	protected PageManager(String path, int pageSize, HttpServletRequest request) {
		// 任意一个dao都行
		this.currentPage = 1;
		this.pageNumber = 1;
		this.count = 0;
		this.pageSize = pageSize <= 0 ? DEFAULTPAGESIZE : pageSize;
		this.request = request;
		this.path = path;

		request.setAttribute("page", this);

		try {
			this.currentPage = Integer.parseInt(request
					.getParameter("currentPage")) <= 0 ? 1 : Integer
					.parseInt(request.getParameter("currentPage"));

		} catch (Exception e) {

			try {
				this.currentPage = Integer.parseInt((String) request
						.getSession().getAttribute("currentPage"));

			} catch (Exception e1) {
				this.currentPage = 1;

			}

		}

	}
	
	
	
	

	
	

	/**
	 * 
	 * @param 下一页的分页链接
	 * @param 一页最大记录数
						} else {
						}
					}
				}
				String sql = "update news set title='" + title + "',note='"
						+ note + "',img='" + img + "' where id=" + id;
				dao.commOper(sql);
				request.setAttribute("suc", "操作成功!");
				go("/admin/newslist.jsp?id=" + id, request, response);
			} catch (Exception e1) {
				e1.printStackTrace();
				request.setAttribute("error", "");
				request.getRequestDispatcher("/admin/newsedit.jsp?id=" + id)
						.forward(request, response);
			}
		}
		// 新增公告
		if (ac.equals("noticesadd")) {
			String title = request.getParameter("title");
			String note = request.getParameter("note");
			String savetime = Info.getDateStr();
			String type = "公告";
			dao.commOper("insert into news (title,note,savetime,type) "
					+ " values ('" + title + "','" + note + "','" + savetime
					+ "','" + type + "')");
			request.setAttribute("suc", "");
			go("admin/noticesadd.jsp", request, response);
		}
		// 编辑公告
		if (ac.equals("noticesedit")) {
			String id = request.getParameter("id");
			String title = request.getParameter("title");
			String note = request.getParameter("note");
			dao.commOper("update news set title='" + title + "',note='" + note
					+ "' where id=" + id);
			request.setAttribute("suc", "");
			go("admin/noticesedit.jsp?id=" + id, request, response);
		}
		// 新增链接
		if (ac.equals("yqlinkadd")) {
			String linkname = request.getParameter("linkname");
			String linkurl = request.getParameter("linkurl");
			dao.commOper("insert into yqlink (linkname,linkurl) "
					+ " values ('" + linkname + "','" + linkurl + "')");
			request.setAttribute("suc", "操作成功!");
			go("admin/yqlink.jsp", request, response);
		}
		// 编辑公告
		if (ac.equals("yqlinkedit")) {
		// for (int i = 0; i < this.pageNumber; i++) {
		// if (this.currentPage == i + 1) {
		// buf.append("<font color=red>[" + (i + 1) + "]</font>").append(
		// "&nbsp;");
		// } else {
		// buf.append("<a href='").append(this.path).append(
		// "&currentPage=").append(i + 1).append(parameter)
		// .append("' style='TEXT-DECORATION:none'>").append(
		// "[" + (i + 1) + "]").append("</a>&nbsp;");
		// }

		// }
		buf.append("<select class='input-xlarge' οnchange=\"javascript:window.location='").append(
				this.path).append("&currentPage='+").append(
				"this.options[this.selectedIndex].value").append(parameter)
				.append("\">");
		for (int i = 0; i < this.pageNumber; i++) {
			if (this.currentPage == i + 1)
				buf.append("<option value=" + (i + 1)
						+ " selected=\"selected\">" + (i + 1) + "</option>");
			else
				buf.append("<option value=" + (i + 1) + ">" + (i + 1)
						+ "</option>");

		}
		buf.append("</select>");
		this.info = buf.toString();
		
	}

	public Collection getCollection() {
		return collection;
	}

	public long getCount() {
		return count;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public long getPageNumber() {
		return pageNumber;
	}

	public int getPageSize() {
		return pageSize;
	}

	public String getInfo() {
		return info;
	}

				request.setCharacterEncoding("utf-8");
				RequestContext requestContext = new ServletRequestContext(
						request);
				if (FileUpload.isMultipartContent(requestContext)) {

					DiskFileItemFactory factory = new DiskFileItemFactory();
					factory.setRepository(new File(request
							.getRealPath("/upfile/") + "/"));
					ServletFileUpload upload = new ServletFileUpload(factory);
					upload.setSizeMax(100 * 1024 * 1024);
					List items = new ArrayList();
					items = upload.parseRequest(request);

					FileItem fileItem = (FileItem) items.get(0);
					if (fileItem.getName() != null && fileItem.getSize() != 0) {
						if (fileItem.getName() != null
								&& fileItem.getSize() != 0) {
							File fullFile = new File(fileItem.getName());
							img = Info.generalFileName(fullFile.getName());
							File newFile = new File(
									request.getRealPath("/upfile/") + "/" + img);
							try {
								fileItem.write(newFile);
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
						}
					}
				}
				String sql = "update imgadv set filename='" + img
						+ "' where id=" + id;
				dao.commOper(sql);
				request.setAttribute("suc", "");
				go("/admin/imgadvedit.jsp?id=" + id, request, response);
			} catch (Exception e1) {
				e1.printStackTrace();
				request.setAttribute("error", "");
				request.getRequestDispatcher("/admin/imgadvedit.jsp?id=" + id)
						.forward(request, response);
			}
    	}catch (Exception e) {
			e.printStackTrace();
		}
      return jscode;
	}
	
	
	
	public static String generalFileName(String srcFileName) {
		try{
		   int index=srcFileName.lastIndexOf(".");
		   return StrUtil.generalSrid()+srcFileName.substring(index).toLowerCase();
		}catch(Exception e){
			return StrUtil.generalSrid();
		}
	}

	public synchronized static String getID() {
		Random random = new Random();
		StringBuffer ret = new StringBuffer(20);
		String rand = String.valueOf(Math.abs(random.nextInt()));
		ret.append(getDateStr());
		ret.append(rand.substring(0,6));
		
		return ret.toString();
	} 
	
	public synchronized static String subStr(String source,int length) {
		if(source.length()>length)
		{
			source=source.substring(0,length)+"...";
		}
		
		return source;
	} 
	
	

	public static String getDateStr(){
		String dateString="";
		try{//yyyyMMddHHmmss
		java.text.SimpleDateFormat formatter=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date currentTime_1=new java.util.Date();
		dateString=formatter.format(currentTime_1);
		}catch(Exception e){
		     }
		 return dateString;
		 } 
	
	public static String getAutoNo(){
		String dateString="";
		try{//yyyyMMddHHmmss
		java.text.SimpleDateFormat formatter=new java.text.SimpleDateFormat("MMddHHmmss");
				} else {
					dao.commOper("insert into car (gid,sl,mid) values ('" + gid
							+ "','" + sl + "','" + mid + "')");
				}
				out.print("true");
			} else {
				out.print("false");
			}
			// request.setAttribute("suc", "");
			// go("/tocar.jsp?gid="+gid, request, response);
		}

		// 直接购买
		if (ac.equals("tobuy")) {
			String gid = request.getParameter("gid");
			int sl = Integer.valueOf(request.getParameter("sl"));
			String ddno = Info.getAutoNo();
			String shrname = "";
			String shrtel = "";
			String shraddr = request.getParameter("addr");
			double ddprice = 0.0;
			String fhstatus = "待发货";
			String shstatus = "待收货";
			String fkstatus = "待付款";
			String wlinfo = "暂无物流信息";
			String savetime = Info.getDateStr();

			if (member != null) {
				String memberid = member.get("id").toString();
				ArrayList<HashMap> addrlist = (ArrayList<HashMap>) dao
						.select("select * from addr where delstatus='0' and memberid="
								+ member.get("id"));
				if (addrlist.size() == 0) {
					out.print("false");
				} else {
					shraddr = addrlist.get(0).get("id").toString();
					HashMap gmap = dao.select(
							"select * from goods where id=" + gid).get(0);
					String price = gmap.get("price").toString();
					if (gmap.get("tprice") != null
							&& !gmap.get("tprice").equals("")) {
						price = gmap.get("tprice").toString();

请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值