zoukankan      html  css  js  c++  java
  • Session机制二(简易购物车案例)

    一:案例一(简易购物车)

    1.目录结构

      

    2.step1.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=ISO-8859-1">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <h2>Step1:选择要购买的图书</h2>
    11     <form action="<%=request.getContextPath() %>/processStep1" method="post">
    12         <table border="1" cellpadding="10" cellspacing="0">
    13             <tr>
    14                 <td>书名</td>
    15                 <td>购买</td>
    16             </tr>
    17             <tr>
    18                 <td>JAVA</td>
    19                 <td><input type="checkbox" name="book" value="JAVA"></td>
    20             </tr>
    21             <tr>
    22                 <td>Oracle</td>
    23                 <td><input type="checkbox" name="book" value="Oracle"></td>
    24             </tr>
    25             <tr>
    26                 <td>Struct2</td>
    27                 <td><input type="checkbox" name="book" value="Struct2"></td>
    28             </tr>
    29             <tr>
    30                 <td colspan="2">
    31                     <input type="submit" value="Submit"/>
    32                 </td>
    33             </tr>
    34         </table>
    35     </form>
    36 </body>
    37 </html>

    3.ProcessStep1Servlet.java

     1 package javaweb;
     2 
     3 import java.io.IOException;
     4 import javax.servlet.ServletException;
     5 import javax.servlet.annotation.WebServlet;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 /**
    11  * Servlet implementation class ProcessStep1Servlet
    12  */
    13 @WebServlet("/processStep1")
    14 public class ProcessStep1Servlet extends HttpServlet {
    15     private static final long serialVersionUID = 1L;
    16 
    17     /**
    18      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    19      */
    20     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    21         //获取选中的图书的信息
    22         String[] books=request.getParameterValues("book");
    23         
    24         //将图书放进HttpSession中
    25         request.getSession().setAttribute("books", books);
    26         
    27         //重定向页面到shopcart/step2.jsp
    28         //这里使用的绝对路径,同理,在jsp里也需要使用相对路径
    29         response.sendRedirect(request.getContextPath()+"/shopCart/step2.jsp");
    30     }
    31 
    32 }

    4.step2.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=ISO-8859-1">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <h2>Step2:请输入信用卡的信息</h2>
    11     <form action="<%=request.getContextPath()%>/processStep2" method="post">
    12         <table border="1" cellpadding="10" cellspacing="0">
    13             <tr>
    14                 <td colspan="2">寄送信息</td>
    15             </tr>
    16             <tr>
    17                 <td>姓名</td>
    18                 <td><input type="text" name="name" /></td>
    19             </tr>
    20             <tr>
    21                 <td>地址</td>
    22                 <td><input type="text" name="address" /></td>
    23             </tr>
    24             <tr>
    25                 <td colspan="2">信用卡信息</td>
    26             </tr>
    27             <tr>
    28                 <td>种类</td>
    29                 <td><input type="radio" name="cardType" value="Visa" />Visa <input
    30                     type="radio" name="cardType" value="Master" />Master</td>
    31             </tr>
    32             <tr>
    33                 <td>卡号</td>
    34                 <td><input type="text" name="cardNum" /></td>
    35             </tr>
    36             <tr>
    37                 <td colspan="2"><input type="submit" value="Submit" /></td>
    38             </tr>
    39         </table>
    40     </form>
    41 </body>
    42 </html><%@ page language="java" contentType="text/html; charset=utf-8"
    43     pageEncoding="utf-8"%>
    44 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    45 <html>
    46 <head>
    47 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    48 <title>Insert title here</title>
    49 </head>
    50 <body>
    51     <h2>Step2:请输入信用卡的信息</h2>
    52     <form action="<%=request.getContextPath()%>/processStep2" method="post">
    53         <table border="1" cellpadding="10" cellspacing="0">
    54             <tr>
    55                 <td colspan="2">寄送信息</td>
    56             </tr>
    57             <tr>
    58                 <td>姓名</td>
    59                 <td><input type="text" name="name" /></td>
    60             </tr>
    61             <tr>
    62                 <td>地址</td>
    63                 <td><input type="text" name="address" /></td>
    64             </tr>
    65             <tr>
    66                 <td colspan="2">信用卡信息</td>
    67             </tr>
    68             <tr>
    69                 <td>种类</td>
    70                 <td><input type="radio" name="cardType" value="Visa" />Visa <input
    71                     type="radio" name="cardType" value="Master" />Master</td>
    72             </tr>
    73             <tr>
    74                 <td>卡号</td>
    75                 <td><input type="text" name="cardNum" /></td>
    76             </tr>
    77             <tr>
    78                 <td colspan="2"><input type="submit" value="Submit" /></td>
    79             </tr>
    80         </table>
    81     </form>
    82 </body>
    83 </html>

    5.ProcessStep2Servlet.java

     1 package javaweb;
     2 
     3 import java.io.IOException;
     4 import javax.servlet.ServletException;
     5 import javax.servlet.annotation.WebServlet;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 /**
    11  * Servlet implementation class ProcessStep2Servlet
    12  */
    13 @WebServlet("/processStep2")
    14 public class ProcessStep2Servlet extends HttpServlet {
    15     private static final long serialVersionUID = 1L;
    16 
    17     /**
    18      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    19      */
    20     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    21         //获取请求参数name address cardType cardNum
    22         String name=request.getParameter("name");
    23         String address=request.getParameter("address");
    24         String cardType=request.getParameter("cardType");
    25         String cardNum=request.getParameter("cardNum");
    26         
    27         //将参入放进一个对象中
    28         Customer customer=new Customer(name, address, cardType, cardNum);
    29                 
    30         //将请求信息存入HtppSession中        
    31         request.getSession().setAttribute("customer", customer);
    32         
    33         //重定向到confirm.jsp
    34         response.sendRedirect(request.getContextPath()+"/shopCart/confirm.jsp");
    35     }
    36 
    37 }

    6.conform.jsp

     1 <%@page import="javaweb.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=ISO-8859-1">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11     <%
    12         Customer customer=(Customer)session.getAttribute("customer");
    13         String[] books=(String[])session.getAttribute("books");
    14     %>
    15     <h2>Step3:请确认</h2>
    16     <table>
    17         <tr>
    18             <td>顾客姓名</td>
    19             <td><%=customer.getName() %></td>
    20         </tr>
    21         <tr>
    22             <td>顾客地址</td>
    23             <td><%=customer.getAddress() %></td>
    24         </tr>
    25         <tr>
    26             <td>顾客卡号</td>
    27             <td><%=customer.getCardNum() %></td>
    28         </tr>
    29         <tr>
    30             <td>顾客类型</td>
    31             <td><%=customer.getCardType() %></td>
    32         </tr>
    33         <tr>
    34             <td>Books</td>
    35             <td>
    36                 <%
    37                     for(String book:books){
    38                         out.print(book);
    39                         out.print("<br>");
    40                     }
    41                 %>
    42             </td>        
    43         </tr>
    44     </table>
    45 </body>
    46 </html>

    7.补充简单bean,Customer.java

     1 package javaweb;
     2 
     3 public class Customer {
     4     private String name;
     5     private String address;
     6     private String cardType;
     7     private String cardNum;
     8     public Customer() {
     9         
    10     }
    11     public Customer(String name, String address, String cardType, String cardNum) {
    12         super();
    13         this.name = name;
    14         this.address = address;
    15         this.cardType = cardType;
    16         this.cardNum = cardNum;
    17     }
    18     public String getName() {
    19         return name;
    20     }
    21     public void setName(String name) {
    22         this.name = name;
    23     }
    24     public String getAddress() {
    25         return address;
    26     }
    27     public void setAddress(String address) {
    28         this.address = address;
    29     }
    30     public String getCardType() {
    31         return cardType;
    32     }
    33     public void setCardType(String cardType) {
    34         this.cardType = cardType;
    35     }
    36     public String getCardNum() {
    37         return cardNum;
    38     }
    39     public void setCardNum(String cardNum) {
    40         this.cardNum = cardNum;
    41     }
    42     
    43 }

    8.展示结果

      

    二:

  • 相关阅读:
    初识你Swift【上篇】
    初识你Swift【下篇】
    单元测试基础
    时间都去哪了?
    iOS App上线的秘密
    mysql系列——DQL常见操作汇总(四)
    Get和Post请求有什么区别?
    SpringBoot2+WebSocket之聊天应用实战
    OCR识别
    mysql系列——子查询(非常重要)(八)
  • 原文地址:https://www.cnblogs.com/juncaoit/p/7471833.html
Copyright © 2011-2022 走看看