zoukankan      html  css  js  c++  java
  • [原创]java WEB学习笔记33:Session 案例 之 购物车

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

    内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

    本人互联网技术爱好者,互联网技术发烧友

    微博:伊直都在0221

    QQ:951226918

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

    checkbox 是一组的话 name必须一致

    代码:

      1)step-1.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>step-1.jsp</title>
     8 </head>
     9 <body>
    10     <h4>Step1:选择要购买的图书</h4>
    11     
    12     <form action="<%= request.getContextPath() %>/processStep1" method="post" >
    13         <table border="1" cellpadding="10" cellspacing="0">
    14             <tr>
    15                 <td>书名</td>
    16                 <td>购买</td>
    17             </tr>
    18             
    19             <tr>
    20                 <td>Java</td>
    21                 <td><input type="checkbox" name="book" value="Java"/></td>
    22             </tr>
    23             
    24             <tr>
    25                 <td>Oracle</td>
    26                 <td><input type="checkbox" name="book" value="Oracle"/></td>
    27             </tr>
    28         
    29             <tr>
    30                 <td>Struts</td>
    31                 <td><input type="checkbox" name="book" value="Struts"/></td>
    32             </tr>
    33 
    34             <tr>
    35                 <td colspan="2" >
    36                     <input align="center" type="submit" value="Submit"/>
    37                 </td>
    38                 
    39             </tr>
    40         
    41         
    42         </table>
    43     </form>
    44 
    45 </body>
    46 </html>

            

      2)step-2.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Step-2.jsp</title>
     8 </head>
     9 <body>
    10     <form action="<%= request.getContextPath() %>/processStep2" method="post">
    11     <h4>step2: 请输入寄送的地址和信用卡信息</h4>
    12     <table border="1" cellpadding="10" cellspacing="0">
    13         <tr>
    14             <td colspan="2" align="center">寄送信息</td>
    15         </tr>
    16         
    17         <tr>
    18             <td>姓名:</td>
    19             <td><input type="text" name="name"/></td>
    20         </tr>
    21         
    22         <tr>
    23             <td>寄送地址:</td>
    24             <td><input type="text" name="address"/></td>
    25         </tr>
    26         
    27         <tr>
    28             <td colspan="2" align="center">信用卡信息</td>
    29         </tr>
    30         
    31         <tr>
    32             <td>种类:</td>
    33             <td>
    34                 <input type="radio" name="cardType" value="Visa"/>Visa
    35                 <input type="radio" name="cardType" value="Master"/>Master 
    36             
    37             </td>
    38         </tr>
    39         
    40         <tr>
    41             <td>卡号:</td>
    42             <td><input type="text" name="card"/></td>
    43         </tr>
    44         
    45         <tr>
    46             <td colspan="2">
    47             <input align="center" type="submit" value="Submit"/></td>
    48         </tr>
    49 </table>
    50     </form>
    51 </body>
    52 </html>

          

      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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here</title>
     9 </head>
    10 
    11 <body>
    12     <%
    13         Customer customer = (Customer)session.getAttribute("customer");
    14         String[] books = (String[])session.getAttribute("books");
    15     %>
    16     <table border="1" cellpadding="10" cellspacing="0">
    17         <tr>
    18             <td>顾客姓名:</td>
    19             <td><%= customer.getName() %></td>
    20         </tr>
    21         
    22         <tr>
    23             <td>地址:</td>
    24             <td><%= customer.getAddress() %></td>
    25         </tr>
    26         <tr>
    27             <td>卡号:</td>
    28             <td><%= customer.getCard()%></td>
    29         </tr>
    30         <tr>
    31             <td>卡的类型:</td>
    32             <td><%= customer.getCardType()%></td>
    33         </tr>
    34         <tr>
    35             <td>买的书:</td>
    36             <td>
    37                 <%
    38                     for(String book : books ){
    39                         out.print(book);
    40                         out.print("<br>");
    41                     }
    42                 %>
    43             </td>
    44         </tr>
    45     </table>
    46 
    47 </body>
    48 </html>

      

      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 }
  • 相关阅读:
    eclipse如何与git 配合工作。
    git托管代码(二)
    PPC2003 安装 CFNET 3.5成功
    我的Window Mobile WCF 項目 第三篇 WM窗体设计
    我的Window Mobile WCF 項目 第一篇Mobile开发和WinForm开发的区别
    我的Window Mobile WCF 項目 第七天
    我的Window Mobile WCF 項目 第二篇 WindowsMobile访问WCF
    WCF 用vs2010 和 vs2008的简单对比测试
    vs2010beta1 和 搜狗输入法 冲突,按下 Ctrl 键就报错,重装搜狗解决
    我的Window Mobile WCF 項目 第六天 (二)
  • 原文地址:https://www.cnblogs.com/jasonHome/p/5550188.html
Copyright © 2011-2022 走看看