欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java毕业设计实战之图片展览馆管理系统的实现

程序员文章站 2022-03-04 11:44:44
一、项目运行环境配置:jdk1.8 + tomcat8.5 + mysql + eclispe(intellij idea,eclispe,myeclispe,sts都支持)项目技术:springbo...

一、项目运行

环境配置:

jdk1.8 + tomcat8.5 + mysql + eclispe(intellij idea,eclispe,myeclispe,sts都支持)

项目技术:

springboot+ springmvc + jpa+ jsp + html+ javascript + jquery + ajax + maven等等

Java毕业设计实战之图片展览馆管理系统的实现

Java毕业设计实战之图片展览馆管理系统的实现

Java毕业设计实战之图片展览馆管理系统的实现

Java毕业设计实战之图片展览馆管理系统的实现

Java毕业设计实战之图片展览馆管理系统的实现

Java毕业设计实战之图片展览馆管理系统的实现

订单服务:

@webservlet("/order/orderservlet")
public class orderservlet extends httpservlet {
	private orderservice service = new orderserviceimpl();
	private clientserviceimpl clientservice=new clientserviceimpl();
	@override
	protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		req.setcharacterencoding("utf-8");
		resp.setcontenttype("text/html;charset=utf-8");
		string op = req.getparameter("op");// 得到传过来的请求
		// 生成订单
		if (op.equals("genorder")) {
			genorder(req, resp);
		}
		// 查看订单
		if (op.equals("findallorders")) {
			findallorders(req, resp);
		}
		// 管理员查看订单
		if (op.equals("findorders")) {
			findorders(req, resp);
		}
		// 发货
		if (op.equals("fahuo")) {
			fahuo(req, resp);
		}
		//删除订单
		if(op.equals("deletes")) {
			deletes(req,resp);
		}
	}
 
	private void deletes(httpservletrequest req, httpservletresponse resp) throws ioexception {
		string ordernumber = req.getparameter("ordernum");
		order findorderbynum = service.findorderbynum(ordernumber);
		if(findorderbynum.getitems().size()!=0) {
			resp.getwriter().print("<script>alert('该订单下有订单子项不可删除,请先删除订单子项!')</script>");
			resp.getwriter().print("<script>location.href='../admin/managerorder.jsp'</script>");
			return;
		}
		try {
			service.deletebynumber(ordernumber);
			resp.getwriter().print("<script>alert('删除成功!')</script>");
			resp.sendredirect("../admin/managerorder.jsp");
		}catch (exception e) {
			resp.getwriter().print("<script>alert('该订单下有订单子项不可删除,请先删除订单子项!')</script>");
			return;
		}
	}
	
	private void fahuo(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		string ordernum = req.getparameter("ordernum");
		service.fahuo(ordernum);
		list<order> orders = service.findorders();
		httpsession session = req.getsession();
		session.setattribute("orders", orders);
		system.out.println(orders);
		resp.sendredirect(req.getcontextpath() + "/admin/managerorder.jsp");
	}
 
	private void findorders(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		list<order> orders = service.findorders();
		httpsession session = req.getsession();
		session.setattribute("orders", orders);
		req.getrequestdispatcher("/admin/managerorder.jsp").forward(req, resp);
	}
 
	private void findallorders(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		httpsession session = req.getsession();
		user user = (user) session.getattribute("user");
		list<order> orders = service.finduserorders(user);
		req.setattribute("orders", orders);
		req.getrequestdispatcher("/person/personorder.jsp").forward(req, resp);
	}
 
	private void genorder(httpservletrequest req, httpservletresponse resp) throws ioexception, servletexception {
		// 取出购物车信息
		// 取出购物项信息
		httpsession session = req.getsession();
		cart cart = (cart) session.getattribute("cart");
		user user = (user) session.getattribute("user");
		if (cart == null) {
			session.setattribute("message", "会话已经结束!");
			req.getrequestdispatcher("../message.jsp").forward(req, resp);
			return;
		}
		//先判断库存够不够!
		book book = cart.getbook();
		string book_id = book.getbook_id();
		book findbookbyid = clientservice.findbookbyid(book_id);
		if(findbookbyid.getbook_kunumber()-cart.gettotalquantity()<0) {
			session.setattribute("message", "库存不足,无法购买!");
			session.removeattribute("cart");
			req.getrequestdispatcher("../message.jsp").forward(req, resp);
			return;
		}
		order order = new order();
		order.settime(new simpledateformat("yyyy-mm-dd hh:mm:ss").format(new date()));
		string ordernum = genordernum();
		order.setordernum(ordernum);
		order.setquantity(cart.gettotalquantity());
		order.setmoney(cart.gettotalmoney());
		order.setuser(user);
		// 订单项
		list<orderitem> oitems = new arraylist<orderitem>();
		for (map.entry<string, cartitem> me : cart.getitmes().entryset()) {
			cartitem citem = me.getvalue();
			orderitem oitem = new orderitem();
			oitem.setid(genordernum());
			oitem.setbook(citem.getbook());
			oitem.setprice(citem.getmoney());
			oitem.setquantity(citem.getquantity());
			oitem.setordernum(ordernum);
			oitems.add(oitem);
		}
		// 建立订单项和订单的关系
		order.setitems(oitems);
		service.genorder(order);
		req.setattribute("order", order);
		req.getsession().removeattribute("cart");
		req.removeattribute("cart");
		req.getrequestdispatcher("/order.jsp").forward(req, resp);
	}
 
	// 生成订单号
	private string genordernum() {
		date now = new date();
		dateformat df = new simpledateformat("yyyymmdd");
		string s1 = df.format(now);
		return s1 + system.nanotime();
	}
 
	@override
	protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		doget(req, resp);
	}
}

管理员登录服务:

@webservlet("/admin/managerservlet")
public class managerservlet extends httpservlet {
    private managerservice service = new managerserviceimpl();
 
    @override
    protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        req.setcharacterencoding("utf-8");
        resp.setcontenttype("text/html;charset=utf-8");
        string op = req.getparameter("op");// 得到传过来的请求
        // 管理员登陆
        if (op.equals("login")) {
            login(req, resp);
        }
        // 修改管理员资料
        if (op.equals("managerinformation")) {
            managerinformation(req, resp);
        }
        // 修改管理员登录密码
        if (op.equals("managerpassword")) {
            managerpassword(req, resp);
        }
        // 注销
        if (op.equals("layout")) {
            layout(req, resp);
        }
        // 添加书籍前先获取所有分类
        if (op.equals("addbookui")) {
            addbookui(req, resp);
        }
        // 添加书籍
        if (op.equals("addbook")) {
            try {
                addbook(req, resp);
            } catch (fileuploadexception e) {
                // todo auto-generated catch block
                e.printstacktrace();
            }
        }
        // 添加书籍分类
        if (op.equals("addcategory")) {
            addcategory(req, resp);
        }
        // 文学艺术类书籍列表
        if (op.equals("category")) {
            getcategorybook(req, resp);
        }
        // 编辑书籍信息前获取书籍的信息回显
        if (op.equals("editbookui")) {
            editbookui(req, resp);
        }
        // 编辑书籍
        if (op.equals("editbook")) {
            try {
                editbook(req, resp);
            } catch (fileuploadexception e) {
                // todo auto-generated catch block
                e.printstacktrace();
            }
        }
        // 删除书籍
        if (op.equals("delbook")) {
            delbook(req, resp);
        }
        // 获取书籍分类列表
        if (op.equals("categorylist")) {
            categorylist(req, resp);
        }
        // 获得分类信息
        if (op.equals("editcategoryui")) {
            editcategoryui(req, resp);
        }
        // 修改书籍分类信息
        if (op.equals("editcategory")) {
            editcategory(req, resp);
        }
        // 删除书籍分类
        if (op.equals("delcategory")) {
            delcategory(req, resp);
        }
        // 用户信息管理
        if (op.equals("findusers")) {
            findusers(req, resp);
        }
        // 添加工作人员
        if (op.equals("addadmin")) {
            addadmin(req, resp);
        }
        // 书籍销售情况
        if (op.equals("sales")) {
            sales(req, resp);
        }
        //搜索
        if(op.equals("search")) {
        	search(req,resp);
        }
    }
    
    private void search(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
    	  string name = req.getparameter("search");
    	  if(name==null) {
    		  name="";
    	  }
    	  list<book> findlistbybookname = service.findlistbybookname(name);
          list<category> categorylist = service.findallcategory();// 分类
          req.setattribute("currentname", name);
          req.setattribute("books", findlistbybookname);
          req.setattribute("categorylist", categorylist);
          req.getrequestdispatcher("/admin/bookslist.jsp").forward(req, resp);
    }
    
    private void sales(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        list<book> sales = service.sales();
        req.setattribute("sales", sales);
        req.getrequestdispatcher("/admin/sales.jsp").forward(req, resp);
    }
 
    private void addadmin(httpservletrequest req, httpservletresponse resp) throws ioexception {
        string username = req.getparameter("username");
        string password = req.getparameter("password");
        string name = req.getparameter("name");
        string sex = req.getparameter("sex");
        string tel = req.getparameter("tel");
        administrator admin = new administrator(username, password, name, sex, tel);
        service.addadmin(admin);
        resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
                + "/img/duigou.png'/>添加成功!</div>");
    }
 
    private void findusers(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        list<user> list = service.findusers();
        httpsession session = req.getsession();
        session.setattribute("users", list);
        req.getrequestdispatcher("/admin/managerusers.jsp").forward(req, resp);
    }
 
    private void login(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        string username = req.getparameter("username");
        string password = req.getparameter("password");
        if(username==null || username=="") {
        	  req.setattribute("message", "请填写用户名");
        	  req.getrequestdispatcher("/admin/404.jsp").forward(req, resp);
        	  return;
        }
        if(password==null || password=="") {
        	 req.setattribute("message", "请填写密码");
       	  req.getrequestdispatcher("/admin/404.jsp").forward(req, resp);
       	  return;
        }
        httpsession session = req.getsession();
        administrator admin = service.login(username, password);
        if (admin.getusername() != null && admin.getusername() != "") {
            req.setattribute("message", "登陆成功");
            session.setattribute("admin", admin);
            req.getrequestdispatcher("/admin/message.jsp").forward(req, resp);
        } else {
        	  req.setattribute("message", "账号或密码错误!");
        	  req.getrequestdispatcher("/admin/404.jsp").forward(req, resp);
        }
    }
 
    private void managerinformation(httpservletrequest req, httpservletresponse resp) throws ioexception {
        string username = req.getparameter("username");
        string name = req.getparameter("name");
        string sex = req.getparameter("sex");
        string tel = req.getparameter("tel");
        service.managerinformation(username, name, sex, tel);
        httpsession session = req.getsession();
        administrator admin = (administrator) session.getattribute("admin");
        admin.setname(name);
        admin.setsex(sex);
        admin.settel(tel);
        session.setattribute("admin", admin);
        resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
                + "/img/duigou.png'/>修改成功!</div>");
 
    }
    //修改管理员密码
    private void managerpassword(httpservletrequest req, httpservletresponse resp) throws ioexception {
        string username = req.getparameter("username");
        string password = req.getparameter("password");
        string repassword = req.getparameter("repassword");
        string oldpassword = req.getparameter("oldpassword");
        if(!password.equals(repassword)) {
        	resp.getwriter().print("<script>alert('两次密码不一致')</script>");
        	return;
        }
        administrator admin = service.findadminbyusername(username);
        if(!admin.getpassword().equals(oldpassword)) {
        	resp.getwriter().print("<script>alert('旧密码填写错误!')</script>");
        	return;
        }
        service.managerpassword(username, password);
        resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
                + "/img/duigou.png'/>修改成功!</div>");
    }
 
    private void layout(httpservletrequest req, httpservletresponse resp) {
        try {
            httpsession session = req.getsession();
            session.removeattribute("admin");
            resp.sendredirect("../admin/managerlogin.jsp");
        } catch (exception e) {
            e.printstacktrace();
        }
    }
 
    private void addbookui(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        list<category> categorys = service.findallcategory();
        req.setattribute("cs", categorys);
        req.getrequestdispatcher("/admin/addbook.jsp").forward(req, resp);
 
    }
 
    private void addbook(httpservletrequest req, httpservletresponse resp) throws fileuploadexception, ioexception {
        // 判断表单是不是multipart/form-data类型
        boolean ismultipart = servletfileupload.ismultipartcontent(req);
        if (!ismultipart) {
            throw new runtimeexception("表单上传类型有误!!");
        }
        // 创建工厂用来 解析请求内容
        diskfileitemfactory factory = new diskfileitemfactory();
        // 构建核心类对象
        servletfileupload sfu = new servletfileupload(factory);
        list<fileitem> items = new arraylist<fileitem>();
        items = sfu.parserequest(req);
        book book = new book();
        for (fileitem item : items) {
            if (item.isformfield()) {
                // 普通字段:把数据分装到book对象中
                processformfield(item, req, book);
            } else {
                // 上传字段:上传
                processuplodfiled(item, req, book);
            }
        }
        // 把书籍信息保存到数据库中
        service.addbook(book);
        resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
                + "/img/duigou.png'/>添加成功!</div>");
    }
 
    // 处理文件上传
    private void processuplodfiled(fileitem item, httpservletrequest req, book book) {
        try {
            // 存放路径:不要放在web-inf中
            // 01.获取存放文件的真实目录
          //  string dirimages = getservletcontext().getrealpath("/images");
            string dirimages =req.getservletcontext().getrealpath("/")+"images/";
//            string dirimages = getservletcontext().getrealpath("/img");
            // 02. 通过io存文件
            // 03. 生成文件名 (用户上传图片, 图片名可能重复)
            string fieldname = item.getfieldname();// 输入框的name值
            string name = item.getname();
            string filetype = name.substring(name.lastindexof(".") + 1);
            string filename = uuid.randomuuid().tostring();// 生成用不重复的文件名
            // 生成文件夹名
			date time = new date();
			simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd");
			string strtime = simpledateformat.format(time);
            // path属性filename
            string path = strtime +file.separator + "books";// 存放到book对象中的路径
//            string path = "books";// 存放到book对象中
            string filename = filename + "." + filetype;
            // filedir:图片最终存在于filedir
            file filedir = new file(dirimages, path + file.separator + filename);
            // inputstream inputstream = item.getinputstream(); 从请求 对象中 通过流的方式
            // 把 上传的文件加载到 内存中 构建输出流
            file parentdir = new file(dirimages, path);// 父目录
            if (!parentdir.exists()) {
                parentdir.mkdirs();
            }
            book.setfilename(filename);
            book.setpath(path);
 
            inputstream inputstream = item.getinputstream();
            fileoutputstream fos = new fileoutputstream(filedir);
            int len = 0;
            while ((len = inputstream.read()) != -1) {
                fos.write(len);
            }
        } catch (ioexception e) {
            e.printstacktrace();
        }
 
    }
 
    // 把fielditem中的数据封装到book中
    private void processformfield(fileitem item, httpservletrequest req, book book) {
        try {
            // item每一个item对象代表一个输入框
            // 01. 获取input输入框的 name 的值 根据规范 输入框 的name的取值, 与 javabean 中的 属性名一致
            string inputname = item.getfieldname();
            string inputvalue = item.getstring("utf-8");
            if (inputname.equals("categoryid")) {// 分类单独处理
                // 获取到选择的分类的id,根据这个id取到分类的信息
                string categoryid = item.getstring();
                system.out.println("categoryid=" + categoryid);
                category category = service.findcategorybyid(categoryid);
                system.out.println(category);
                book.setcategory(category);
            } else {
                beanutils.setproperty(book, inputname, inputvalue);
            }
        } catch (exception e) {
            e.printstacktrace();
        }
    }
 
    // 添加书籍分类
    private void addcategory(httpservletrequest req, httpservletresponse resp) {
        try {
            category category = new category();
            beanutils.populate(category, req.getparametermap());
            string categoryname = category.getcategory_name();
            if(stringutils.isnullorempty(categoryname)) {
            	resp.getwriter().print("<script>alert('请填写分类名称')</script>");
            	resp.getwriter().print("<script>location.href='../admin/addcategory.jsp'</script>");
            	return;
            }
            service.addcategory(category);// 调用添加方法
            resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
                    + "/img/duigou.png'/>添加成功!</div>");
        } catch (exception e) {
            e.printstacktrace();
        }
    }
 
    private void getcategorybook(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        list<book> books = service.getcategorybook(req.getparameter("cid"));// 文学艺术类书籍
        list<category> categorylist = service.findallcategory();// 分类
        req.setattribute("books", books);
        req.setattribute("categorylist", categorylist);
        req.setattribute("currentname", "");
        req.getrequestdispatcher("/admin/bookslist.jsp").forward(req, resp);
    }
 
    private void editbookui(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        string book_id = req.getparameter("book_id");
        book book = findbookbyid(book_id);
        list<category> category = service.findallcategory();
        hashmap map = new hashmap();
        map.put("book", book);
        map.put("category", category);
        req.setattribute("map", map);
        req.getrequestdispatcher("/admin/editbook.jsp").forward(req, resp);
    }
 
    private void editbook(httpservletrequest req, httpservletresponse resp) throws fileuploadexception, ioexception {
        string book_id = req.getparameter("book_id");
        string book_name = req.getparameter("book_name");
        string book_author = req.getparameter("book_author");
        string book_press = req.getparameter("book_press");
        string book_desc = req.getparameter("book_desc");
        double book_price = double.parsedouble(req.getparameter("book_price"));
        string book_kunumber = req.getparameter("book_kunumber");
        string categoryid = req.getparameter("categoryid");
        category findcategorybyid = service.findcategorybyid(categoryid);
        service.editbook(book_id, book_name, book_author, book_press, book_desc, book_price, book_kunumber,findcategorybyid);
        resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
                + "/img/duigou.png'/>修改成功!</div>");
    }
 
    private void delbook(httpservletrequest req, httpservletresponse resp) throws ioexception {
        string book_id = req.getparameter("book_id");
        service.delbook(book_id);
        resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
                + "/img/duigou.png'/>删除成功!</div>");
    }
 
    private void categorylist(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        list<category> categorylist = service.findallcategory();
        req.setattribute("categorylist", categorylist);
        req.getrequestdispatcher("/admin/categoryslist.jsp").forward(req, resp);
    }
 
    private void editcategoryui(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        category category = service.findcategorybyid(req.getparameter("category_id"));
        req.setattribute("category", category);
        req.getrequestdispatcher("/admin/editcategory.jsp").forward(req, resp);
    }
 
    private void editcategory(httpservletrequest req, httpservletresponse resp) throws ioexception {
        category category = new category(req.getparameter("category_id"), req.getparameter("category_name"),
                req.getparameter("category_desc"));
        string categoryname = category.getcategory_name();
        if(stringutils.isnullorempty(categoryname)) {
        	resp.getwriter().print("<script>alert('请填写分类名称')</script>");
        	resp.getwriter().print("<script>location.href='../admin/editcategory.jsp'</script>");
        	return;
        }
        service.editcategory(category);
        resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
                + "/img/duigou.png'/>修改成功!</div>");
    }
 
    private void delcategory(httpservletrequest req, httpservletresponse resp) throws ioexception {
        string category_id = req.getparameter("category_id");
        service.delcategory(category_id);
        resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
                + "/img/duigou.png'/>删除成功!</div>");
    }
 
    // 通过书籍id找到书籍信息
    private book findbookbyid(string book_id) {
        return service.findbookbyid(book_id);
    }
 
    @override
    protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        doget(req, resp);
    }
}

收藏夹业务处理:

@webservlet("/client/clientservlet")
public class clientservlet extends httpservlet {
	private clientservice service = new clientserviceimpl();
	private managerservice managerservice = new managerserviceimpl();
 
	@override
	protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		req.setcharacterencoding("utf-8");
		resp.setcontenttype("text/html;charset=utf-8");
		string op = req.getparameter("op");// 得到传过来的请求
		if (op != null && !op.equals("")) {
			// 登录
			if (op.equals("login")) {
				login(req, resp);
			}
			// 注销
			if (op.equals("layout")) {
				layout(req, resp);
			}
			// 注册
			if (op.equals("register")) {
				register(req, resp);
			}
			// 文学艺术类书籍列表
			if (op.equals("category")) {
				getcategorybook(req, resp);
			}
 
			// 个人信息修改
			if (op.equals("personinformation")) {
				personinformation(req, resp);
			}
			// 修改密码
			if (op.equals("personpassword")) {
				personpassword(req, resp);
			}
			// 搜索框
			if (op.equals("search")) {
				search(req, resp);
			}
			// 详情页面
			if (op.equals("particulars")) {
				particulars(req, resp);
			}
			// 添加购物车
			if (op.equals("addcart")) {
				addcart(req, resp);
			}
			// 删除购物车中的购物项
			if (op.equals("delitem")) {
				delitem(req, resp);
			}
			// 修改购物项数量
			if (op.equals("changenum")) {
				changenum(req, resp);
			}
			// 添加收藏夹
			if (op.equals("addfavorite")) {
				addfavorite(req, resp);
			}
			// 显示收藏夹
			if (op.equals("showfavorite")) {
				showfavorite(req, resp);
			}
			// 删除收藏夹
			if (op.equals("delfavorite")) {
				delfavorite(req, resp);
			}
			// 删除收藏夹
			if (op.equals("buynow")) {
				bunow(req, resp);
			}
 
		}
	}
 
	private void delfavorite(httpservletrequest req, httpservletresponse resp) throws ioexception, servletexception {
		string book_id = req.getparameter("book_id");
		service.delfavorite(book_id);
		httpsession session = req.getsession();
		list<favorite> lists = (list<favorite>) session.getattribute("favorite");
		iterator<favorite> iterator = lists.iterator();
		while (iterator.hasnext()) {
			favorite favorite = iterator.next();
			if (book_id.equals(favorite.getbook().getbook_id())) {
				iterator.remove();// 使用迭代器的删除方法删除
			}
		}
		resp.sendredirect(req.getcontextpath() + "/favorite.jsp");
	}
 
	private void showfavorite(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		httpsession session = req.getsession();
		user user = (user) session.getattribute("user");
		list<favorite> favorites = service.findfavoritebyuserid(user);
		session.setattribute("favorite", favorites);
		req.getrequestdispatcher("/favorite.jsp").forward(req, resp);
	}
 
	private void addfavorite(httpservletrequest req, httpservletresponse resp) throws ioexception {
		httpsession session = req.getsession();
		user user = (user) session.getattribute("user");
		string user_id = user.getid();
		string book_id = req.getparameter("book_id");
		boolean isexit = service.findfavorite(user_id, book_id);
		if (isexit == false) {
			service.addfavorite(user_id, book_id);
		}
	}
 
	private void changenum(httpservletrequest req, httpservletresponse resp) throws ioexception {
		string num = req.getparameter("num");
		string book_id = req.getparameter("book_id");
		// 取出购物车
		cart cart = (cart) req.getsession().getattribute("cart");
		cartitem item = cart.getitmes().get(book_id);
		item.setquantity(integer.parseint(num));
		resp.sendredirect(req.getcontextpath() + "/showcart.jsp");
 
	}
 
	private void login(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		string username = req.getparameter("username");
		string password = req.getparameter("password");
		httpsession session = req.getsession();
		user user = service.login(username, password);
		if (user.getusername() != null && user.getusername() != "") {
			req.setattribute("message", "登陆成功");
			session.setattribute("user", user);
			req.getrequestdispatcher("/message.jsp").forward(req, resp);
		} else {
			req.setattribute("message", "用户名或密码错误,请重新登录");
			req.getrequestdispatcher("/message.jsp").forward(req, resp);
		}
	}
 
	private void layout(httpservletrequest req, httpservletresponse resp) {
		try {
			httpsession session = req.getsession();
			session.removeattribute("user");// 获取session对象,从session中移除登陆信息
			resp.sendredirect("../client/clientservlet?op=category");
		} catch (exception e) {
			e.printstacktrace();
		}
	}
 
	private void register(httpservletrequest req, httpservletresponse resp) {
		try {
			string username = req.getparameter("username");
			string password = req.getparameter("password");
			string name = req.getparameter("name");
			string sex = req.getparameter("sex");
			string tel = req.getparameter("tel");
			string address = req.getparameter("address");
			boolean isexist = false;// 判断是否存在该用户
			if (!username.equals("") && !password.equals("")) {
				isexist = service.register(username, password, name, sex, tel, address);
				if (isexist == true) {
					resp.getwriter().print("<script>alert('该用户已经注册,请直接登录')</script>");
					resp.getwriter().print("<script>location.href='../client/clientservlet?op=category'</script>");
				} else {
					resp.getwriter().write("注册成功!");
					resp.getwriter().print("<script>location.href='../client/clientservlet?op=category'</script>");
				}
			}else {
				resp.getwriter().print("<script>alert('请填写账号或密码')</script>");
				resp.getwriter().print("<script>location.href='../client/clientservlet?op=category'</script>");
			}
		} catch (exception e) {
			e.printstacktrace();
		}
	}
 
	private void getcategorybook(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		list<book> books = service.getcategorybook(req.getparameter("cid"));// 文学艺术类书籍
		req.setattribute("books", books);
		list<category> categorylist= managerservice.findallcategory();
		req.setattribute("categorylist", categorylist);
		req.getrequestdispatcher("/showbook.jsp").forward(req, resp);
	}
 
	private void personinformation(httpservletrequest req, httpservletresponse resp)
			throws servletexception, ioexception {
		string username = req.getparameter("username");
		string name = req.getparameter("name");
		string sex = req.getparameter("sex");
		string tel = req.getparameter("tel");
		string address = req.getparameter("address");
		service.personinformation(username, name, sex, tel, address);
		user user = (user) req.getsession().getattribute("user");
		user.setusername(username);
		user.setname(name);
		user.setsex(sex);
		user.settel(tel);
		user.setaddress(address);
		req.getsession().setattribute("user", user);
		resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
				+ "/img/duigou.png'/>修改成功!</div>");
	}
 
	private void personpassword(httpservletrequest req, httpservletresponse resp) throws ioexception {
		string username = req.getparameter("username");
		string password = req.getparameter("password");
		string repassword = req.getparameter("repassword");
		string oldpassword = req.getparameter("oldpassword");
		if(!repassword.equals(password)) {
			resp.getwriter().print("<script>alert('两次密码不一致')</script>");	
		}
		user user = service.finduserbyusername(username);
		if(user==null) {
			resp.getwriter().print("<script>alert('会话失效或未登录,请重新登录!')</script>");	
			return;
		}
		if(!user.getpassword().equals(oldpassword)) {
			resp.getwriter().print("<script>alert('旧密码输入错误!')</script>");
			return;
		}
		service.personpassword(password, username);
		resp.getwriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getcontextpath()
				+ "/img/duigou.png'/>修改成功!</div>");
	}
 
	private void search(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		string search = req.getparameter("search");
		list<book> searchmessage = service.search(search);
		req.setattribute("books", searchmessage);
		req.setattribute("name", search==null?"":search);
		req.getrequestdispatcher("/showbook.jsp").forward(req, resp);
	}
 
	private void particulars(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		string book_id = req.getparameter("book_id");
		book book = findbookbyid(book_id);
		req.setattribute("book", book);
		req.getrequestdispatcher("/particulars.jsp").forward(req, resp);
	}
 
	// 通过书籍id找到书籍信息
	private book findbookbyid(string book_id) {
		book book = service.findbookbyid(book_id);
		return book;
	}
 
	private void addcart(httpservletrequest req, httpservletresponse resp) throws ioexception, servletexception {
		string book_id = req.getparameter("book_id");
		book book = findbookbyid(book_id);
		httpsession session = req.getsession();
		cart cart = (cart) session.getattribute("cart");
		if (cart == null) {
			cart = new cart();
			cart.addbook(book);
			cart.setbook(book);
			session.setattribute("cart", cart);
		}else {
			session.setattribute("message", "购物车里有商品,请支付之后再来添加!");
			req.getrequestdispatcher("../message.jsp").forward(req, resp);
			return;
		}
		req.getrequestdispatcher("../showcart.jsp").forward(req, resp);
	}
 
	private void delitem(httpservletrequest req, httpservletresponse resp) throws ioexception {
		string book_id = req.getparameter("book_id");
		cart cart = (cart) req.getsession().getattribute("cart");
		cart.getitmes().remove(book_id);
		req.getsession().removeattribute("cart");
		resp.sendredirect(req.getcontextpath() + "/showcart.jsp");
	}
 
	private void bunow(httpservletrequest req, httpservletresponse resp) throws ioexception, servletexception {
		cart cart1 = (cart) req.getsession().getattribute("cart");
		if(cart1==null) {
			cart cart = new cart();
			string book_id = req.getparameter("book_id");
			book book = findbookbyid(book_id);
			cart.addbook(book);
			cart.setbook(book);
			req.getsession().setattribute("cart", cart);
		}else {
			req.getsession().setattribute("message", "购物车里有商品,请支付之后再来添加!");
			req.getrequestdispatcher("../message.jsp").forward(req, resp);
			return;
		}
		req.getrequestdispatcher("../showcart.jsp").forward(req, resp);
	}
 
	@override
	protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		doget(req, resp);
	}
}

到此这篇关于java毕业设计实战之图片展览馆管理系统的实现的文章就介绍到这了,更多相关java 图片展览馆管理系统内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!