IE禁用Cookie后的session处理
IE禁用Cookie后解决方案:URL重写
购物车案例《IE禁用Cookie后》
购物界面ShowBook.servlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //先死后活 out.println("<h1>欢迎购买</h1>"); out.println("oracle书<a href='/myCart/BuyBookCl?id=3&name=oracle' >点击购买</a></br>"); //取出Db ArrayList<Book> mydb=Db.getDb(); //访问一些session request.getSession(); for(Book book:mydb){ String url=response.encodeURL("/myCart/BuyBookCl?id="+book.getId()); //我们希望href ="/myCart/BuyBookCl?id=1&" out.println(book.getName()+"----"+book.getPrice()+"<a href='"+url+"' >点击购买</a></br>"); } }
BuyBookCl.servlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); @SuppressWarnings("unused") PrintWriter out = response.getWriter(); //request.getAttribute(name)接受的servlet与本身(request里面放置的属性) //浏览器发过来的(http协议相关的) String id=request.getParameter("id"); Book bookinfo=Db.getBookById(id); HttpSession session=request.getSession(); HashMap<String,Book> hm=(HashMap<String,Book>)session.getAttribute("myBooks"); //如果第一次购物al=null if(hm==null){ hm=new LinkedHashMap<String,Book>(); Book book=new Book(); book.setId(id); book.setName(bookinfo.getName()); book.setNum(1); hm.put(id,book); //al放到session中 session.setAttribute("myBooks", hm); }else{ //判断hm中是否有该书 if(hm.containsKey(id)){ //表示购买过一次 //取出 Book book=hm.get(id); int bookNum=book.getNum(); book.setNum(bookNum+1); }else{ Book book=new Book(); book.setId(id); book.setName(bookinfo.getName()); book.setNum(1); hm.put(id,book); } //更新 session.setAttribute("myBooks", hm); } //跳转到购物车 request.getRequestDispatcher("/ShowMyCart").forward(request, response); }
ShowMyCart.servlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); HashMap<String,Book> myBooks=(HashMap<String,Book>)request.getSession().getAttribute("myBooks"); out.println("你的购物车有以下书籍:</br>"); //out.println(myBooks); //遍历hashMap Iterator iterator=myBooks.keySet().iterator(); while(iterator.hasNext()){ String key=(String)iterator.next(); Book book=myBooks.get(key); out.println(book.getName()+"----"+book.getNum()+"</br>"); } out.println("购物车的总价是"); //这里我们也需要地址重写 String url=response.encodeURL("/myCart/ShowBook"); out.println("<a href='"+url+"'>返回购物大厅</a>"); }