实现的功能:
1.显示所有物品 ShowBookServlet
2.能把选中物品放到购物车中(客户端禁用Cookie的前提下,仍然可以使用) BuyServlet
3.显示购物车,并且有清除所选物品的功能 ShowCartServlet ClearCartServlet
ShowBookServlet.java
BuyServlet.java
1 import java.io.IOException; 2 import java.io.PrintWriter; 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 12 import com.itheima.domain.Book; 13 import com.itheima.domain.BookDB; 14 15 /** 16 实现功能:将传送过来的书加入购物车 17 18 功能分析: 19 1.得到传送过来的参数,通过BookDB得到书 20 2.得到session 不存在则创建并加入 存在则直接加入到ArrayList中 21 */ 22 public class BuyServlet extends HttpServlet { 23 24 public void doGet(HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException { 26 response.setContentType("text/html;charset=UTF-8"); 27 PrintWriter out = response.getWriter(); 28 29 //得到要买的书 30 String id = request.getParameter("id"); 31 Book book = BookDB.findBookById(id); 32 //向购物车中放书 33 HttpSession session = request.getSession(); 34 List<Book> cart = (List<Book>) session.getAttribute("cart");//此处书不止一本且重复用ArrayList集合作为书的所选正合适 35 if(cart==null){ 36 cart = new ArrayList<Book>();//第一次购物产生 37 session.setAttribute("cart", cart); 38 } 39 //有购物车 40 cart.add(book);//作为引用不用刷新 41 String url = request.getContextPath()+"/servlet/ShowBooksServlet"; 42 url = response.encodeURL(url); 43 out.write("【"+book.getName()+"】已经放入您的购物车了!<a href='"+url+"'>继续购物</a>"); 44 } 45 46 public void doPost(HttpServletRequest request, HttpServletResponse response) 47 throws ServletException, IOException { 48 49 doGet(request, response); 50 } 51 52 }
ClearCartServlet
1 import java.io.IOException; 2 import java.io.PrintWriter; 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import javax.servlet.http.HttpSession; 8 //清空购物车,重新购买 9 public class ClearCartServlet extends HttpServlet { 10 11 public void doGet(HttpServletRequest request, HttpServletResponse response) 12 throws ServletException, IOException { 13 response.setContentType("text/html;charset=UTF-8"); 14 PrintWriter out = response.getWriter(); 15 16 HttpSession session = request.getSession(false); 17 if(session==null){ 18 out.write("你都没有购物车,你销个鬼"); 19 return; 20 } 21 // session.invalidate();//太绝 会把全部的session 清空 不适合 22 session.removeAttribute("cart"); 23 String url = request.getContextPath()+"/servlet/ShowBooksServlet"; 24 url = response.encodeURL(url); 25 out.write("您的购物车清空完毕<br/><a href='"+url+"'>继续购物</a>"); 26 27 } 28 29 public void doPost(HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException { 31 32 doGet(request, response); 33 } 34 35 }
ShowCartServlet
1 import java.io.IOException; 2 import java.io.PrintWriter; 3 import java.util.List; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 10 import com.itheima.domain.Book; 11 //显示购物车中的商品 12 public class ShowCartServlet extends HttpServlet { 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) 15 throws ServletException, IOException { 16 response.setContentType("text/html;charset=UTF-8"); 17 PrintWriter out = response.getWriter(); 18 19 HttpSession session = request.getSession(false);//仅作查看 20 if(session==null){ 21 out.write("对不起,您还没有买任何的东东"); 22 return; 23 } 24 List<Book> cart = (List<Book>)session.getAttribute("cart"); 25 //此处在移除后cart的值为空,若是cart.size()在前 则会出现NullPointerException 26 if(cart==null||cart.size()<1){ 27 out.write("对不起,您还没有买任何的东东"); 28 return; 29 } 30 String url0 = request.getContextPath()+"/servlet/ClearCartServlet"; 31 url0 = response.encodeURL(url0); 32 out.write("您购买的商品如下:<a href='"+url0+"'>清空购物车</a><br/>"); 33 for(Book b:cart){ 34 out.write(b.getName()+"<br/>"); 35 } 36 String url = request.getContextPath()+"/servlet/ShowBooksServlet"; 37 url = response.encodeURL(url); 38 out.write("<br/><a href='"+url+"'>继续购物</a>"); 39 } 40 41 public void doPost(HttpServletRequest request, HttpServletResponse response) 42 throws ServletException, IOException { 43 44 doGet(request, response); 45 } 46 47 }