servlet-cookie和session应用示例:模拟购物车

时间:2022-08-25 20:49:41
思路分析:
1. 根据需求抽象出业务对象
User{
usrename;
password;
}


ShoppingCarItem{
product;
num;
}

ShoppingCar{
List<ShoppingCarItem>
}

2. 写Servlet处理请求
...


domain:
Java代码 servlet-cookie和session应用示例:模拟购物车 servlet-cookie和session应用示例:模拟购物车servlet-cookie和session应用示例:模拟购物车
  1. package cn.itcast.cd.domain;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.UUID;
  5. public class ShoppingCar {
  6. private List<ShoppingCarItem> list = new ArrayList<ShoppingCarItem>();
  7. /**
  8. * 根据货品和数量往购物车中添加
  9. * @param product
  10. * @param num
  11. */
  12. public void add(String product,Integer num){
  13. ShoppingCarItem carItem = new ShoppingCarItem();
  14. carItem.setNum(num);
  15. carItem.setProduct(product);
  16. carItem.setId(UUID.randomUUID().toString());
  17. list.add(carItem);
  18. }
  19. /**
  20. * 根据唯一的标示删除购物车中对应的明细
  21. * @param id
  22. */
  23. public void remove(String id){
  24. for (ShoppingCarItem item : list) {
  25. if(id.equals(item.getId())){
  26. list.remove(item);
  27. break;
  28. }
  29. }
  30. }
  31. public List<ShoppingCarItem> getList() {
  32. return list;
  33. }
  34. }
  35. package cn.itcast.cd.domain;
  36. public class ShoppingCarItem {
  37. private String product;
  38. private Integer num;
  39. private String id; //给每一个明细生成一个唯一的id
  40. public String getProduct() {
  41. return product;
  42. }
  43. public void setProduct(String product) {
  44. this.product = product;
  45. }
  46. public Integer getNum() {
  47. return num;
  48. }
  49. public void setNum(Integer num) {
  50. this.num = num;
  51. }
  52. public String getId() {
  53. return id;
  54. }
  55. public void setId(String id) {
  56. this.id = id;
  57. }
  58. }
  59. package cn.itcast.cd.domain;
  60. public class User {
  61. private String username;
  62. private String password;
  63. public String getUsername() {
  64. return username;
  65. }
  66. public void setUsername(String username) {
  67. this.username = username;
  68. }
  69. public String getPassword() {
  70. return password;
  71. }
  72. public void setPassword(String password) {
  73. this.password = password;
  74. }
  75. }


servlet:

Java代码 servlet-cookie和session应用示例:模拟购物车 servlet-cookie和session应用示例:模拟购物车servlet-cookie和session应用示例:模拟购物车
  1. package cn.itcast.cd.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  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. import cn.itcast.cd.domain.ShoppingCar;
  10. public class BuyServlet extends HttpServlet {
  11. @Override
  12. protected void service(HttpServletRequest request, HttpServletResponse response)
  13. throws ServletException, IOException {
  14. request.setCharacterEncoding("UTF-8");
  15. response.setContentType("text/html;charset=utf-8");
  16. //1. 获取请求中的参数.
  17. String product = request.getParameter("product");
  18. String num = request.getParameter("num");
  19. //2. 获取到购物车
  20. //2.1 首先要到session中找一下是否已经有了购物车. 如果有的话就不需要创建了... 直接把货品放到购物车中就可以了
  21. HttpSession httpSession = request.getSession();
  22. ShoppingCar shoppingCar = (ShoppingCar)httpSession.getAttribute("SHOPINGCAR_IN_SESSION");
  23. if(shoppingCar==null){ //说明是第一次购物. 还没有购物车的
  24. //2.2 如果从session中没有找到购物车. 需要新创建一个购物车并且放到session中.为了下次购物使用这个购物车
  25. shoppingCar = new ShoppingCar();
  26. httpSession.setAttribute("SHOPINGCAR_IN_SESSION", shoppingCar);
  27. }
  28. shoppingCar.add(product, Integer.parseInt(num));
  29. PrintWriter pw = response.getWriter();
  30. pw.println("添加成功<br/>");
  31. pw.println("<a href='/buy.html'>继续购物</a><br/>");
  32. pw.println("<a href='/list'>管理购物</a><br/>");
  33. }
  34. }
  35. package cn.itcast.cd.servlet;
  36. import java.io.IOException;
  37. import java.io.PrintWriter;
  38. import javax.servlet.ServletException;
  39. import javax.servlet.http.HttpServlet;
  40. import javax.servlet.http.HttpServletRequest;
  41. import javax.servlet.http.HttpServletResponse;
  42. import javax.servlet.http.HttpSession;
  43. import cn.itcast.cd.domain.ShoppingCar;
  44. import cn.itcast.cd.domain.ShoppingCarItem;
  45. import cn.itcast.cd.domain.User;
  46. public class ListServlet extends HttpServlet {
  47. @Override
  48. protected void service(HttpServletRequest request, HttpServletResponse response)
  49. throws ServletException, IOException {
  50. request.setCharacterEncoding("UTF-8");
  51. response.setContentType("text/html;charset=utf-8");
  52. HttpSession httpSession = request.getSession();
  53. //1. 先获取session中的User
  54. User user = (User) httpSession.getAttribute("USER_IN_SESSION");
  55. PrintWriter pw = response.getWriter();
  56. pw.println("当前用户为:"+user.getUsername()+"<br/>");
  57. //2. 获取session的购物车
  58. ShoppingCar shoppingCar = (ShoppingCar) httpSession.getAttribute("SHOPINGCAR_IN_SESSION");
  59. //2.1 如果在session中购物车的话,列出购物车中的明细
  60. if(shoppingCar!=null){
  61. for (ShoppingCarItem item : shoppingCar.getList()) {
  62. pw.println("货品:"+item.getProduct()+" 数量:"+item.getNum()+"<br/>");
  63. }
  64. }else{
  65. //2.2 在session中没有购物车的话.提示用户没有当前用户名购物车
  66. pw.println("当前用户没有购物车.");
  67. }
  68. }
  69. }
  70. package cn.itcast.cd.servlet;
  71. import java.io.IOException;
  72. import java.io.PrintWriter;
  73. import javax.servlet.ServletException;
  74. import javax.servlet.http.HttpServlet;
  75. import javax.servlet.http.HttpServletRequest;
  76. import javax.servlet.http.HttpServletResponse;
  77. import javax.servlet.http.HttpSession;
  78. import cn.itcast.cd.domain.User;
  79. public class LoginServlet extends HttpServlet {
  80. @Override
  81. protected void service(HttpServletRequest request, HttpServletResponse response)
  82. throws ServletException, IOException {
  83. request.setCharacterEncoding("UTF-8");
  84. response.setContentType("text/html;charset=utf-8");
  85. //1. 接收请求中的数据封装对象
  86. String username = request.getParameter("username");
  87. String password = request.getParameter("password");
  88. User user = new User();
  89. user.setPassword(password);
  90. user.setUsername(username);
  91. //2. 将用户方到session*ListServlet从session中访问到User
  92. HttpSession httpSession = request.getSession();
  93. httpSession.setAttribute("USER_IN_SESSION", user);
  94. PrintWriter pw = response.getWriter();
  95. pw.println("欢迎:"+username+"<br/>");
  96. pw.println("<a href='/buy.html'>购物</a><br/>");
  97. pw.println("<a href='/list'>管理购物</a><br/>");
  98. }
  99. }


用户登录页面:
Html代码 servlet-cookie和session应用示例:模拟购物车 servlet-cookie和session应用示例:模拟购物车servlet-cookie和session应用示例:模拟购物车
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>用户登录</title>
  6. </head>
  7. <body>
  8. <form action="/login" method="post">
  9. 用户名:<input type="text" name="username"><br/>
  10. 密码:<input type="text" name="password"><br/>
  11. <input type="submit" value="登录">
  12. </form>
  13. </body>
  14. </html>


购买页面:
Html代码 servlet-cookie和session应用示例:模拟购物车 servlet-cookie和session应用示例:模拟购物车servlet-cookie和session应用示例:模拟购物车
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>购物货品</title>
  6. </head>
  7. <body>
  8. <form action="/buy" method="post">
  9. 货品:<select name="product">
  10. <option value="鼠标">鼠标</option>
  11. <option value="键盘">键盘</option>
  12. <option value="内存">内存</option>
  13. <option value="显示器">显示器</option>
  14. </select> <br/>
  15. 数量:<input type="text" name="num"><br/>
  16. <input type="submit" value="购买">
  17. </form>
  18. </body>
  19. </html>


web.xml配置:
Xml代码 servlet-cookie和session应用示例:模拟购物车 servlet-cookie和session应用示例:模拟购物车servlet-cookie和session应用示例:模拟购物车
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. version="2.5">
  6. <servlet>
  7. <servlet-name>LoginServlet</servlet-name>
  8. <servlet-class>cn.itcast.cd.servlet.LoginServlet</servlet-class>
  9. </servlet>
  10. <servlet-mapping>
  11. <servlet-name>LoginServlet</servlet-name>
  12. <url-pattern>/login</url-pattern>
  13. </servlet-mapping>
  14. <servlet>
  15. <servlet-name>BuyServlet</servlet-name>
  16. <servlet-class>cn.itcast.cd.servlet.BuyServlet</servlet-class>
  17. </servlet>
  18. <servlet-mapping>
  19. <servlet-name>BuyServlet</servlet-name>
  20. <url-pattern>/buy</url-pattern>
  21. </servlet-mapping>
  22. <servlet>
  23. <servlet-name>ListServlet</servlet-name>
  24. <servlet-class>cn.itcast.cd.servlet.ListServlet</servlet-class>
  25. </servlet>
  26. <servlet-mapping>
  27. <servlet-name>ListServlet</servlet-name>
  28. <url-pattern>/list</url-pattern>
  29. </servlet-mapping>
  30. </web-app>