javaweb学习7_Session案例之购物车

1. 简易session版购物车:创建一个简单的购物车模型,由三个 jsp 和两个 Servlet 组成:

javaweb学习7_Session案例之购物车_第1张图片

 

 

 

 

checkbox 是一组的话 name必须一致

代码:

 

  1)step-1.jsp

 

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 
 4 
 5 
 6 
 7 step-1.jsp
 8 
 9 
10     

Step1:选择要购买的图书

11 12
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 38 39 40 41 42
书名购买
Java
Oracle
Struts
36 37
43
44 45 46

 

 

        javaweb学习7_Session案例之购物车_第2张图片

 

  2)step-2.jsp

 

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 
 4 
 5 
 6 
 7 Step-2.jsp
 8 
 9 
10     
11

step2: 请输入寄送的地址和信用卡信息

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 38 39 40 41 42 43 44 45 46 48 49
寄送信息
姓名:
寄送地址:
信用卡信息
种类: 34 Visa 35 Master 36 37
卡号:
47
50
51 52

 

 

      javaweb学习7_Session案例之购物车_第3张图片

  3)confim.jsp

 

 

 1 <%@page import="com.jason.shoopingcart.bean.Customer"%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 
 5 
 6 
 7 
 8 Insert title here
 9 
10 
11 
12     <%
13         Customer customer = (Customer)session.getAttribute("customer");
14         String[] books = (String[])session.getAttribute("books");
15     %>
16     
17         
18             
19             
20         
21         
22         
23             
24             
25         
26         
27             
28             
29         
30         
31             
32             
33         
34         
35             
36             
44         
45     
顾客姓名:<%= customer.getName() %>
地址:<%= customer.getAddress() %>
卡号:<%= customer.getCard()%>
卡的类型:<%= customer.getCardType()%>
买的书: 37 <% 38 for(String book : books ){ 39 out.print(book); 40 out.print("
"); 41 } 42 %> 43
46 47 48

 

 

  

  4)ProcessStep1Servlet.java

 

 

 1  1 package com.jason.shoopingcart.servlet;
 2  2 
 3  3 import java.io.IOException;
 4  4 import javax.servlet.ServletException;
 5  5 import javax.servlet.annotation.WebServlet;
 6  6 import javax.servlet.http.HttpServlet;
 7  7 import javax.servlet.http.HttpServletRequest;
 8  8 import javax.servlet.http.HttpServletResponse;
 9  9 
10 10 /**
11 11  * Servlet implementation class ProcessStep1Servlet
12 12  */
13 13 @WebServlet("/processStep1")
14 14 public class ProcessStep1Servlet extends HttpServlet {
15 15     private static final long serialVersionUID = 1L;
16 16 
17 17     
18 18     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19 19 
20 20         //1.获取选中的图书的信息
21 21         String[] books = request.getParameterValues("book");
22 22         
23 23         //2.把图书信息放入到HttpSession 中
24 24         request.getSession().setAttribute("books", books);
25 25         
26 26         //3.重定向到shoppingcart/step-2.jsp
27 27         response.sendRedirect(request.getContextPath() + "/shoppingcart/step-2.jsp");
28 28         
29 29     }
30 30 
31 31 }

 

 

 

  

  5)ProcessStep2Servlet.java

 

 1 1 package com.jason.shoopingcart.servlet;
 2  2 
 3  3 import java.io.IOException;
 4  4 
 5  5 import javax.servlet.ServletException;
 6  6 import javax.servlet.annotation.WebServlet;
 7  7 import javax.servlet.http.HttpServlet;
 8  8 import javax.servlet.http.HttpServletRequest;
 9  9 import javax.servlet.http.HttpServletResponse;
10 10 import javax.servlet.http.HttpSession;
11 11 
12 12 import com.jason.shoopingcart.bean.Customer;
13 13 
14 14 /**
15 15  * Servlet implementation class ProcessStep2Servlet
16 16  */
17 17 @WebServlet("/processStep2")
18 18 public class ProcessStep2Servlet extends HttpServlet {
19 19     private static final long serialVersionUID = 1L;
20 20 
21 21     
22 22     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23 23         //1. 获取请求参数 name ,address ,cardType,card
24 24         request.setCharacterEncoding("UTF-8");
25 25         String name = request.getParameter("name");
26 26         String address = request.getParameter("address");
27 27         String cardType = request.getParameter("cardType");
28 28         String card = request.getParameter("card");
29 29         
30 30         Customer customer = new Customer(name, address, cardType, card);
31 31         
32 32         //2.把请求存储到Httpsession中
33 33          HttpSession  session = request.getSession();
34 34          session.setAttribute("customer", customer);
35 35         
36 36         //3.重定向到confirm.jsp
37 37          response.sendRedirect(request.getContextPath() + "/shoppingcart/confirm.jsp");
38 38     }
39 39 
40 40 }

 

 

 

   

  6)Customer.java

 

 

 1 package com.jason.shoopingcart.bean;
 2 
 3 public class Customer {
 4     private String name;
 5     private String address;
 6     private String cardType;
 7     private String card;
 8 
 9     public String getName() {
10         return name;
11     }
12 
13     public void setName(String name) {
14         this.name = name;
15     }
16 
17     public String getAddress() {
18         return address;
19     }
20 
21     public void setAddress(String address) {
22         this.address = address;
23     }
24 
25     public String getCardType() {
26         return cardType;
27     }
28 
29     public void setCardType(String cardType) {
30         this.cardType = cardType;
31     }
32 
33     public void setCard(String card) {
34         this.card = card;
35     }
36     public String getCard() {
37         return card;
38     }
39     
40     
41 
42     public Customer(String name, String address, String cardType, String card) {
43         super();
44         this.name = name;
45         this.address = address;
46         this.cardType = cardType;
47         this.card = card;
48     }
49 
50     public Customer() {
51         super();
52     }
53 
54 }

 

你可能感兴趣的:(javaweb)