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

Java实战玩具商城的前台与后台实现流程

程序员文章站 2022-03-09 23:17:15
一、项目简述本系统主要实现的功能有:网上商城系统,前台+后台管理,用户注册,登录,商品展示,分组展示,搜索,收货地址管理,购物车管理,添加,购买,个人信息修改。订单查询等等,后台商品管理,分类管理,库...

一、项目简述

本系统主要实现的功能有:
网上商城系统,前台+后台管理,用户注册,登录,商品展示,分组展示,搜索,收货地址管理,购物车管理,添加,购买,个人信息修改。订单查询等等,后台商品管理,分类管理,库存管理,订单管理,用户管理,信息修改等等。

二、项目运行

环境配置:

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

项目技术:

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

Java实战玩具商城的前台与后台实现流程

Java实战玩具商城的前台与后台实现流程

Java实战玩具商城的前台与后台实现流程

Java实战玩具商城的前台与后台实现流程

Java实战玩具商城的前台与后台实现流程

Java实战玩具商城的前台与后台实现流程

后台管理代码:

@controller
public class indexcontroller {
    /**
     * 网站首页
     * */
    @getmapping("/")
    public string index(){
        return "/index";
    }
 
    /**
     * 联系我们
     * */
    @getmapping("/contacts")
    public string contacts(){
        return "/common/contacts";
    }
 
    /**
     * 关于我们
     * */
    @getmapping("/about")
    public string about(){
        return "/common/about";
    }
 
    /**
     * 后台管理首页
     * */
    @getmapping("/admin/index")
    public string adminindex(httpsession session, httpservletrequest request, httpservletresponse response) throws ioexception {
        string admin = (string) session.getattribute("admin");
        /**拦截器:如果不是管理员,则进行重定向*/
        if (stringutils.isempty(admin)){
            response.sendredirect(request.getcontextpath() + "/");//重定向
        }
        return "/admin/index";
    }
 
    /**
     * 用户登录注册
     * */
    @getmapping("/login")
    public string login(){
        return "/user/logreg";
    }
 
    /**
     * 用户忘记密码
     * */
    @getmapping("/forget")
    public string forget(){
        return "user/forget";
    }
 
    /**
     * 个人中心
     * */
    @getmapping("/user/center")
    public string usercenter(httpsession session, httpservletrequest request, httpservletresponse response) throws ioexception {
        string userid = (string) session.getattribute("userid");
        /**拦截器:如果不是用户角色登录,则进行重定向*/
        if (stringutils.isempty(userid)){
            response.sendredirect(request.getcontextpath() + "/");//重定向
        }
        return "/user/user-center";
    }
 
    /**
     * 用户修改密码
     * */
    @requirespermissions("user:userinfo")
    @getmapping("/user/pass")
    public string userinfo(){
        return "/user/updatepass";
    }
 
    /**
     * 用户更换手机号
     * */
    @requirespermissions("user:userinfo")
    @getmapping("/user/phone")
    public string userphone(){
        return "/user/updatephone";
    }
 
    /**
     * 用户商品列表
     * */
    @getmapping("/user/product")
    public string userproduct(){
        return "/user/product/productlist";
    }
 
    /**
     * 通知消息
     * */
    @getmapping("/user/message")
    public string commonmessage(){
        return "/user/message/message";
    }
    /**
     * 弹出式通知消息
     * */
    @getmapping("/user/alertmessage")
    public string alertmessage(){
        return "/user/message/alertmessage";
    }
    /**
     * 跳转到产品清单界面
     * */
    @getmapping("/product-listing")
    public string toproductlisting() {
        return "/common/product-listing";
    }
 
    /**
     * 跳转到产品清单搜索界面
     * */
    @getmapping("/product-search")
    public string toproductsearchs(string keys, modelmap modelmap) {
        if(keys==null){
            return "/error/404";
        }
        modelmap.put("keys",keys);
        return "/common/product-search";
    }
 
    /**用户个人中心默认展示图*/
    @getmapping("/home/console")
    public string homeconsole(){
        return "/admin/home/console";
    }
 
    /**
     * 管理员首页默认展示图
     * */
    @getmapping("/echars/console")
    public string echars(){
        return "/admin/echars/console";
    }
 
 
    @getmapping("/app/message/index")
    public string appmessageindex(){
        return "/admin/app/message/index";
    }
 
    /**
     * 用户收藏列表
     * */
    @getmapping("/user/collect")
    public string usercollect(){
        return "/user/collect/collectlist";
    }
 
    /**
     * 用户售出记录
     * */
    @getmapping("/user/sold")
    public string sold(){
        return "/user/sold/soldrecord";
    }
 
    /**
     * 销量列表
     * */
    @getmapping("/admin/sold")
    public string adminsold(){
        return "/admin/sold/soldrecord";
    }
 
    /**
     * 首页公告清单
     * */
    @getmapping("/user/newslist")
    public string usernews(){
        return "/common/listnews";
    }
 
    /**
     * 管理员公告列表
     * */
    @getmapping("/admin/newslist")
    public string adminnews(){
        return "/admin/news/newslist";
    }
}

订单和商品服务类:

@webservlet("/backstage_goodsservlet")
public class goodsservlet extends httpservlet{
	
	goodsservice service = new goodsservice();
	
	@override
	protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		string action = req.getparameter("action");		
		switch(action) {
			case "addgoods":addgoods(req,resp);break;
			case "getgoodslist":getgoodslist(req,resp);break;
			case "getgoodslistbyname":getgoodslistbyname(req,resp);break;
			case "deletegoods":deletegoods(req,resp);break;
			case "togoodsupdatepage":togoodsupdatepage(req,resp);break;
			case "updategoods":updategoods(req,resp);break;
			case "upgoodsimage":upgoodsimage(req,resp);break;
			case "fastbuy":sendorder(req,resp);break;
		}
	}
	
	/**
	 * 提交订单
	 * @throws ioexception 
	 */
	public void sendorder(httpservletrequest req, httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
		req.setcharacterencoding("utf-8");
		//设置响应编码格式			
		resp.setcontenttype("text/html;charset=utf-8");
		simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");//设置日期格式
		orders order = new orders();
		order.setnumber(order.getrandomstring(10));
		order.settime(df.format(new date()));
		order.setname(req.getparameter("recipients"));
		order.setaddress(req.getparameter("address"));
		order.setphone(req.getparameter("phone"));
		order.setaddress_label(req.getparameter("addresslabel"));
		order.setsex(req.getparameter("sex"));
		
		order.setuser("sjb");
		order.setgoods_id(99);
		order.setgoods_num(3);
		order.setgoods_status(1);
		int d = service.addorder(order);
		try {
			if(d>0) {
				jsonresult ok = jsonresult.ok();
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult error = jsonresult.errormsg("订单提交失败!");
				resp.getwriter().println(jsonutil.javaobjecttojson(error));
			}
		} catch (exception e) {
			e.printstacktrace();
		}
	}
 
	/**
	 * 保存商品
	 * @throws ioexception 
	 */
	public void addgoods(httpservletrequest req,httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
		req.setcharacterencoding("utf-8");
		//设置响应编码格式			
		resp.setcontenttype("text/html;charset=utf-8");
		fileuploadutil upfile = new fileuploadutil();
		servletcontext servletcontext = req.getsession().getservletcontext();
		list<fileitem> items= upfile.getrequsetfileitems(req,servletcontext);
		
		//保存所有用户提交的 表单数据 的map
		map<string, string>  formdata = new hashmap<>();
		
		string savepath = new file(getservletcontext().getrealpath("/")).getparentfile().getparentfile().getpath()+"/imgs";
		
		
		try {
			//遍历
			for (fileitem file : items) {
				
				if(!file.isformfield()) {
					string filesuffix = fileuploadutil.getfilesuffix(file).touppercase();
					
					//必须是jpg或png  图片文件才能进行上传操作
					if("jpg".equals(filesuffix) || "png".equals(filesuffix)) {
						//生成新图片名
						string imgname = fileuploadutil.getimgnewname(filesuffix);
						
						fileuploadutil.savefile(file, savepath, imgname);
						
						formdata.put("imgurl",imgname);
					}
				}else {	
						//非文件数据存入
						formdata.put(file.getfieldname(), file.getstring("utf-8"));	
				}
			}
		} catch (unsupportedencodingexception e) {
			e.printstacktrace();
		}
	
		goods goods = new goods();
		goods.setname(formdata.get("goodsname"));
		goods.setnum(integer.parseint(formdata.get("num")));
		goods.setprice(double.parsedouble(formdata.get("price")));
		goods.setimgurl(formdata.get("imgurl"));
		goods.settype(formdata.get("type"));
		goods.setdescription(formdata.get("description"));
		goods.setcolor(formdata.get("color"));
		goods.setmemory(formdata.get("memory"));
		
		goods.setcreatedate(new simpledateformat("yyyy-mm-dd").format(new date()));
		int d = service.addgoods(goods);
		
		try {
			if(d>0) {
				jsonresult ok = jsonresult.ok();
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("添加失败,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}
		} catch (exception e) {
			e.printstacktrace();
		}
	}
	
	
	/**
	 * 向前端页面返回商品数据列表
	 */
	public void getgoodslist(httpservletrequest req,httpservletresponse resp) {
		list<goods> allgoods = service.getallgoods();
		req.setattribute("goodslist", allgoods);
		try {
			req.getrequestdispatcher("/backstage/tgls/goodsmanage/goods_list.jsp").forward(req, resp);
		} catch (servletexception e) {
			e.printstacktrace();
		} catch (ioexception e) {
			e.printstacktrace();
		}	
	}
	
	
	/**
	 * @throws ioexception 
	 * 根据商品名查询商品
	 * @param req
	 * @param resp
	 * @throws  
	 */
	public void getgoodslistbyname(httpservletrequest req,httpservletresponse resp) throws ioexception  {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
		string name = req.getparameter("goodsname");
		string type = req.getparameter("type");
		
		map<string,string> parmas = new hashmap<>();
		parmas.put("name", name);
		parmas.put("type", type);
		
		list<goods> list = service.getgoodsbyname(parmas);
	
		try {
			if(list != null) {
				jsonresult ok = jsonresult.ok(list);
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("未获取到任何数据,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}			
			
		} catch (exception e) {
			e.printstacktrace();
		}
	}
	
	
	
	
	
	/**
	 * 修改商品时,获取此商品的全部数据,并返回至修改页面
	 */
	public void togoodsupdatepage(httpservletrequest req,httpservletresponse resp) {
		int id = integer.parseint(req.getparameter("id"));
		goods goods = service.getgoodsinfobyid(id);
		req.setattribute("goods", goods);
		try {
			req.getrequestdispatcher("/backstage/tgls/goodsmanage/goods_update.jsp").forward(req, resp);
		} catch (servletexception e) {
			e.printstacktrace();
		} catch (ioexception e) {
			e.printstacktrace();
		}	
		
	}
	
	
	
	/**
	 *修改商品信息时,如果有修改图片,先将图片进行修改
	 * @throws ioexception 
	 */
	public void upgoodsimage(httpservletrequest req,httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
		fileuploadutil upfile = new fileuploadutil();
		servletcontext servletcontext = req.getsession().getservletcontext();
		list<fileitem> items= upfile.getrequsetfileitems(req,servletcontext);
		string savepath = new file(getservletcontext().getrealpath("/")).getparentfile().getparentfile().getpath()+"/imgs";
		
		string imgname = null;
		
		try {
			//遍历
			for (fileitem file : items) {
				if(!file.isformfield()) {
					string filesuffix = fileuploadutil.getfilesuffix(file);
					//必须是jpg或png  图片文件才能进行上传操作
					if("jpg".equals(filesuffix) || "png".equals(filesuffix)) {
						//生成新图片名
						imgname = fileuploadutil.getimgnewname(filesuffix);
						fileuploadutil.savefile(file, savepath, imgname);					
					}
				}
			}
			
			int d = service.updategoodsimgbyid(imgname, integer.parseint(req.getparameter("id")));
			if(d>0) {
				jsonresult ok = jsonresult.ok(imgname);
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("修改失败,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}		
		} catch (exception e) {
			e.printstacktrace();
		}	
	}
	
	
	
	/**
	 *  修改商品
	 * @throws ioexception 
	 */
	public void updategoods(httpservletrequest req,httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
		goods goods = new goods();
		goods.setid(integer.parseint(req.getparameter("id")));
		goods.setname(req.getparameter("goodsname"));
		goods.setnum(integer.parseint(req.getparameter("num")));
		goods.setprice(double.parsedouble(req.getparameter("price")));
		goods.settype(req.getparameter("type"));
		goods.setcolor(req.getparameter("color"));
		goods.setmemory(req.getparameter("memory"));
		goods.setdescription(req.getparameter("description"));
				
		int d = service.updategoodsbyid(goods);
		try {
			if(d>0) {
				jsonresult ok = jsonresult.ok();
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("修改失败,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}		
		} catch (exception e) {
			e.printstacktrace();
		}
	}
	
	
 
	/**
	 * 删除商品
	 * @throws ioexception 
	 */
	public void deletegoods(httpservletrequest req,httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
		int id = integer.parseint(req.getparameter("id"));
		
		int d = service.deletegoodsbyid(id);
		try {
			if(d>0) {
				jsonresult ok = jsonresult.ok();
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("删除失败,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}			
			
		} catch (exception e) {
			e.printstacktrace();
		}
	}
}

订单服务类:

@webservlet("/backstage_ordersservlet")
public class ordersservlet extends httpservlet{
	
	ordersservice service = new ordersservice();
	
	@override
	protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		string action = req.getparameter("action");
		system.out.println(action);
		switch(action) {
			case "getorderslist":getorderslist(req,resp);break;
			case "getorderslistbyname":getorderslistbyname(req,resp);break;
			case "deleteorders":deleteorders(req,resp);break;
			case "deleteallorders":deleteallorders(req,resp);break;
			case "toordersupdatepage":toordersupdatepage(req,resp);break;
			case "updateorders":updateorders(req,resp);break;
			case "fastbuy":sendorder(req,resp);break;
		}
	}
 
	/* 
	 * 删除全部订单 
	 */
	private void deleteallorders(httpservletrequest req, httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
		// todo 自动生成的方法存根
		int d = service.deleteallorders();
		try {
			if(d>0) {
				jsonresult ok = jsonresult.ok();
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("删除失败,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}			
			
		} catch (exception e) {
			e.printstacktrace();
		}
	}
 
	/**
	 * 提交订单
	 * @throws ioexception 
	 */
	public void sendorder(httpservletrequest req, httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
			simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");//设置日期格式
			user user =(user) req.getsession().getattribute("user");
			orders order = new orders();
			order.setnumber(order.getrandomstring(10));
			order.settime(df.format(new date()));
			order.setname(req.getparameter("recipients"));
			order.setaddress(req.getparameter("address"));
			order.setphone(req.getparameter("phone"));
			order.setaddress_label(req.getparameter("addresslabel"));
			order.setsex(req.getparameter("sex"));
			order.setuser(user.getusername());
			order.setgoods_id(integer.parseint(req.getparameter("id")));
			order.setgoods_num(integer.parseint(req.getparameter("num")));
			order.setgoods_status(1);
			int d = service.addorder(order);
				try {
					if(d>0) {
						jsonresult ok = jsonresult.ok();
						resp.getwriter().println(jsonutil.javaobjecttojson(ok));
					}else {
						jsonresult error = jsonresult.errormsg("订单提交失败!");
						resp.getwriter().println(jsonutil.javaobjecttojson(error));
					}
				} catch (exception e) {
					e.printstacktrace();
				}	
		
	}
 
	
	/**
	 * 向前端页面返回订单数据列表
	 */
	public void getorderslist(httpservletrequest req,httpservletresponse resp) {
		list<orders> allorders = service.getallorders();
		req.setattribute("orderslist",allorders);
		try {
			req.getrequestdispatcher("/backstage/tgls/ordersmanage/orders_list.jsp").forward(req, resp);
		} catch (servletexception e) {
			e.printstacktrace();
		} catch (ioexception e) {
			e.printstacktrace();
		}	
	}
	
	
	/**
	 * 根据订单名查询商品(注意!类别没改)
	 * @param req
	 * @param resp
	 * @throws ioexception 
	 */
	public void getorderslistbyname(httpservletrequest req,httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
		string name = req.getparameter("ordersuser");
		//string type = req.getparameter("type");
		
		map<string,string> parmas = new hashmap<>();
		parmas.put("name", name);
		//parmas.put("type", type);
		
		list<orders> list = service.getordersbyname(parmas);
	
		try {
			if(list != null) {
				jsonresult ok = jsonresult.ok(list);
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("未获取到任何数据,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}			
			
		} catch (exception e) {
			e.printstacktrace();
		}
	}
	
	
	/**
	 * 修改订单时,获取此订单的全部数据,并返回至修改页面
	 */
	public void toordersupdatepage(httpservletrequest req,httpservletresponse resp) {
		int id = integer.parseint(req.getparameter("id"));
		orders orders = service.getordersinfobyid(id);
		req.setattribute("orders", orders);
		try {
			req.getrequestdispatcher("/backstage/tgls/ordersmanage/orders_update.jsp").forward(req, resp);
		} catch (servletexception e) {
			e.printstacktrace();
		} catch (ioexception e) {
			e.printstacktrace();
		}	
		
	}
	
	/**
	 *  修改订单
	 * @throws ioexception 
	 */
	
	public void updateorders(httpservletrequest req,httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
		req.setcharacterencoding("utf-8");
		//设置响应编码格式			
		resp.setcontenttype("text/html;charset=utf-8");
		orders orders = new orders();
		orders.setid(integer.parseint(req.getparameter("id")));
		orders.setnumber(req.getparameter("number"));
		orders.setuser(req.getparameter("user"));
		orders.settime(req.getparameter("time"));
		orders.setname(req.getparameter("name"));
		orders.setsex(req.getparameter("sex"));
		orders.setaddress(req.getparameter("address"));
		orders.setphone(req.getparameter("phone"));
		orders.setaddress_label(req.getparameter("address_label"));
		orders.setgoods_id(integer.parseint(req.getparameter("goods_id")));
		orders.setgoods_num(integer.parseint(req.getparameter("goods_num")));
		orders.setgoods_status(integer.parseint(req.getparameter("goods_status")));
		
		system.out.println(orders);
		int d = service.updateordersbyid(orders);
		system.out.println(d);
		try {
			if(d>0) {
				
				jsonresult ok = jsonresult.ok();
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("修改失败,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}		
		} catch (exception e) {
			e.printstacktrace();
		}
	}
	
	
 
	/**
	 * 删除订单
	 * @throws unsupportedencodingexception 
	 */
	public void deleteorders(httpservletrequest req,httpservletresponse resp) throws unsupportedencodingexception {
		//设置请求编码格式:
		req.setcharacterencoding("utf-8");
		//设置响应编码格式			
		resp.setcontenttype("text/html;charset=utf-8");
		int id = integer.parseint(req.getparameter("id"));
		
		int d = service.deleteordersbyid(id);
		try {
			if(d>0) {
				jsonresult ok = jsonresult.ok();
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("删除失败,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}			
			
		} catch (exception e) {
			e.printstacktrace();
		}
	}
}

用户服务类:

@webservlet("/backstage_userservlet")
public class userservlet extends httpservlet{
	userservice service = new userservice();
	
	@override
	protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
		string action = req.getparameter("action");
		switch(action) {
			case "login":login(req, resp);break;
			case "modifypassword":modifypassword(req, resp);break;
			case "checkoldpassword":checkoldpassword(req, resp);break;
			case "aftermodifypassword":aftermodifypassword(req, resp);break;
			case "logout":logout(req, resp);break;
		}
	}
	
	/**
	 * 用户登录
	 * @throws ioexception 
	 */
	public void login(httpservletrequest req, httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
		//获取前端页面传来的用户名
		string username = req.getparameter("username");
		//获取前端页面传来的密码
		string password = req.getparameter("password");
		// 1、先验证该用户的角色是否为管理员
		string userrole = service.getuserrolebyname(username);
		try {
			if(!"admin".equals(userrole)) {
				jsonresult errormsg = jsonresult.errormsg("暂无登录权限");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}else {
				//2、获取用户数据 进行用户名和密码验证
				user user = service.getuserbynameandpassword(username, password);
				if(user!=null) {
					//将用户数据保存到session 作用域中,方便在后续的操作中使用用户数据
					req.getsession().setattribute("user", user);
					jsonresult ok = jsonresult.ok();
					resp.getwriter().println(jsonutil.javaobjecttojson(ok));
				}else {
					// 将错误信息封装在结果集中
					jsonresult result = jsonresult.errormsg("用户名或密码错误,请重试");
					//以json的形式返回给前端
					resp.getwriter().println(jsonutil.javaobjecttojson(result));
				}
			}
		} catch (exception e) {
			e.printstacktrace();
		}
	}
	
	/**
	 * 检查旧密码是否正确 ,在修改密码操作时使用
	 * @throws ioexception 
	 */
	public void checkoldpassword(httpservletrequest req, httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
		user user =(user) req.getsession().getattribute("user");
		string oldpassword = service.getpasswordbyid(user.getid()); 
		string password = req.getparameter("password");
		try {
			if(!oldpassword.equals(password)) {
				jsonresult errormsg = jsonresult.errormsg("原始密码错误,请重新输入");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}else {
				jsonresult ok = jsonresult.ok();
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}
		} catch (ioexception e) {
			e.printstacktrace();
		}
		
	}
	
	
	/**
	 *  修改密码
	 * @throws ioexception 
	 */
	public void modifypassword(httpservletrequest req, httpservletresponse resp) throws ioexception {
		//设置请求编码格式:
				req.setcharacterencoding("utf-8");
				//设置响应编码格式			
				resp.setcontenttype("text/html;charset=utf-8");
		
		string password = req.getparameter("newpassword");
		user user =(user) req.getsession().getattribute("user");
		int id = user.getid();
		
		int d = service.updatepasswordbyid(id, password);
		
		try {
			if(d>0) {
				jsonresult ok = jsonresult.ok();
				resp.getwriter().println(jsonutil.javaobjecttojson(ok));
			}else {
				jsonresult errormsg = jsonresult.errormsg("修改失败,请重试");
				resp.getwriter().println(jsonutil.javaobjecttojson(errormsg));
			}			
			
		} catch (exception e) {
			e.printstacktrace();
		}
	}
	
	/**
	 * 成功修改密码后,清除session中的数据,并且重新跳转到登录页
	 * @param req
	 * @param resp
	 */
    public void aftermodifypassword(httpservletrequest req, httpservletresponse resp) {
    	req.getsession().removeattribute("user");
		try {
			//使用iframe时,要避免这种直接跳转的方式
			// 此时只会让内嵌的iframe跳转到登录页,并非整个页面
//			resp.sendredirect("backstage/login.jsp");
			
			printwriter writer = resp.getwriter();
			writer.print("<html>");
			writer.print("<script>");
			writer.print("window.open('"+ req.getcontextpath()+"/backstage/login.jsp','_top')");
			writer.print("</script>");
			writer.print("</html>");
		} catch (ioexception e) {
			e.printstacktrace();
		}
    }
	
 
	/**
	 * 退出登录
	 */
    public void logout(httpservletrequest req, httpservletresponse resp) {
		req.getsession().removeattribute("user");
		try {
			resp.sendredirect("backstage/login.jsp");
		} catch (ioexception e) {
			e.printstacktrace();
		}
	}
}

到此这篇关于java实战玩具商城的前台与后台实现流程的文章就介绍到这了,更多相关java 玩具商城内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!