package com.xf.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import com.xf.model.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class loginCl extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); //中文乱码问题 response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); //得到传过来的账号密码 String u=request.getParameter("username"); String p=request.getParameter("password"); //调用BO中的方法验证 UsersBeanBo ubb=new UsersBeanBo(); if(ubb.checkUser(u, p)) { //用户合法 //1.成功登陆的用户信息放入session,后面用到 UsersBean ub=ubb.getUserBean(u); request.getSession().setAttribute("userInfo", ub); //2.把购物车的信息取出,准备在shopping3中显示 MyCartBO mcb=(MyCartBO)request.getSession().getAttribute("mycart"); ArrayList<GoodsBean> a1=mcb.showMyCart(); //吧a1放入到request传到下个界面 request.setAttribute("mycartInfo", a1); request.getRequestDispatcher("shopping3.jsp").forward(request, response); } else { //用户不合法 request.getRequestDispatcher("shopping2.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
package com.xf.servlet; import java.io.IOException; import java.io.PrintWriter; import com.xf.model.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class OrderClServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); //处理完成订单(把订单写入数据库) //该任务将会交给OrderBeanBO OrderBeanBO obbo=new OrderBeanBO(); //得到购物车 MyCartBO mcbo=(MyCartBO)request.getSession().getAttribute("mycart"); //用户id long userId=((UsersBean)request.getSession().getAttribute("userInfo")).getUserid(); OrderInfoBean oifb=obbo.addOrder(mcbo, userId+""); if(oifb != null) { //添加成功 //给shopping4显示地数据 request.setAttribute("detailbean", oifb); request.getRequestDispatcher("shopping4.jsp").forward(request, response); } else { //添加订单失败 request.getRequestDispatcher("shopping3.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
package com.xf.servlet; import java.io.IOException; import java.io.PrintWriter; import com.xf.model.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ShoppingCl2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); //中文乱码解决 response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); //这个控制器用于检测用户是否登录,如果没有登录就进入登录界面 //若登录过就直接进入显式用户信息和购物车的情况的页面 //看session中是否有用户登录的信息 UsersBean ub=(UsersBean)request.getSession().getAttribute("userInfo"); if(ub == null) { //如果没有登录过! //跳转到shopping页面进行登录,转发 request.getRequestDispatcher("shopping2.jsp").forward(request, response); } else { //若已经登录 request.getRequestDispatcher("shopping3.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
package com.xf.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import com.xf.model.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ShoppingClServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); //中文乱码问题解决 response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); //得到type值,判断用户要进行如何的操作 String type=request.getParameter("type"); // MyCartBO mcbo=new MyCartBO(); //每次都会创建一个新的购物车,没法保存,用session //多购物车问题 //试图从session中取出一个购物单选栏,重名字取 MyCartBO mcbo=(MyCartBO)request.getSession().getAttribute("mycart"); //如果是第一次取得话是取不出来的 if(mcbo == null) { //该用户第一次买 //创建一个购物车栏,放入session mcbo=new MyCartBO(); //把mycart名字,mcbo值,传入session,session中只有一个mycart,不然就会覆盖类似hashmap request.getSession().setAttribute("mycart", mcbo); } if(type.equals("addGoods")) { //要把数据转换到showMycart.jsp中要有ID号 String goodsId=request.getParameter("goodsId"); //调用BO里面的函数添加到购物车 mcbo.addGoods(goodsId, "1"); } else if(type.equals("delGoods")) { //得到要删除的ID String goodsId=request.getParameter("goodsId"); //删除货物 mcbo.delGoods(goodsId); } else if(type.equals("show")) { //donothing,因为后面有跳转,这里就不要跳转了 } else if(type.equals("delAll")) { System.out.println("!!!"); mcbo.clear(); } else if(type.equals("updateGoods")) { //用户希望修改数量 //如何得到ID 和NUM //接受ID,可能有很多个ID String goodsId[]=request.getParameterValues("goodsId"); String newNums[]=request.getParameterValues("newNums"); for(int i=0 ; i<goodsId.length ; ++i) { System.out.println("id=="+goodsId[i]+"数量="+newNums[i]); //修改 mcbo.upGoods(goodsId[i], newNums[i]); } // mcbo.upGoods(goodsId, newNum); } //然后把购物车里面的信息全部取出准备拉出来显示 ArrayList<GoodsBean> a1=mcbo.showMyCart(); //吧信息跳转到购物车页面showMyCart.jsp,放到request里面去 request.setAttribute("mycartinfo", a1); //跳转 request.getRequestDispatcher("showMycart.jsp").forward(request, response); } }
/** * 功能:这是一个控制处理器,控制goodsBeanBo和jsp关联的数据 * 时间:2014年7月6日21:08:34 */ package com.xf.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.xf.model.*; public class showGoodsClServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); //得到type看是那种要求工作 String type=request.getParameter("type"); if(type.equals("showDetail")) { //得到要显示地ID号 String goodsId=request.getParameter("id"); //调用GoodsBeanBo GoodsBeanBO gbb=new GoodsBeanBO(); //得到数据 GoodsBean gb=gbb.getGoodsBean(goodsId); //吧gb放入到request里面去 request.setAttribute("goodsInfo", gb); //跳转到showDetail.jsp里面去 request.getRequestDispatcher("showDetail.jsp").forward(request, response); } else if(type.equals("fenye")) { //得到pageNow String pageNow=request.getParameter("pageNow"); //吧pageNow传出去,传到request里面去 request.setAttribute("abc", pageNow); //跳转到主界面 request.getRequestDispatcher("index.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
淘宝商城servlet部分
时间: 2024-10-09 18:57:27