Session案例:简易的购物车

Posted G-&-D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Session案例:简易的购物车相关的知识,希望对你有一定的参考价值。

三个jsp和两个Servlet组成:在WebContent下边建立一个shoppingcart文件夹,将三个jsp文件放在里面:

 

1.建立一个step1.jsp文件,出现一个表格,是一个复选框,可以选择要购买的书籍,完毕后,点击Submit,跳转到Servlet类 ProcessStep1Servlet里面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h2>step1: 选择要购买的书籍!</h2>
    
    <form action="<%= request.getContextPath() %>/processStep1Servlet" method="post">
    
        <table border="1" cellpadding="10" cellspacing="0">
            <tr>
                <th>书名</th>  
                <th>点击购买</th>
            </tr>    
            
            <tr>
                <td>Oracle</td>  
                <td><input type="checkbox" name="book" value="Oracle"/></td>
            </tr>
            
            <tr>
                <td>java</td>  
                <td><input type="checkbox" name="book" value="java"/></td>
            </tr>
            
            <tr>
                <td>C++</td>  
                <td><input type="checkbox" name="book" value="C++"/></td>
            </tr>
            
            <tr>
                <td>mysql</td>  
                <td><input type="checkbox" name="book" value="mysql"/></td>
            </tr>
            
            <tr>
                <td>WEB</td>  
                <td><input type="checkbox" name="book" value="WEB"/></td>
            </tr>
            
            <tr>
                <td colspan="2"> <input type="submit" value="Submit"/></td>
            </tr>
            
        </table>
    
    </form>
</body>
</html>

 

2.建立一个Servlet类 ProcessStep1Servlet,里面有doPost方法,可实现session方法(好处就是获取用户选择书名的id,保存到后边再使用),获取用户选择的书名,使用相对路径的方法重定向到step1.jsp页面; response.sendRedirect(request.getContextPath()+"/shoppingcart/step2.jsp");

package com.lanqiao.javatest;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProcessStep1Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //System.out.println("jflskdflasdk");
        String [] books=request.getParameterValues("book");
        
        request.getSession().setAttribute("books", books);
        //System.out.println(request.getContextPath() + "/shoppingcart/step2.jsp");
        response.sendRedirect(request.getContextPath()+"/shoppingcart/step2.jsp");
    }

}

 

3.在step2.jsp页面,是用户输入个人信息页面,付款,发送货物需要的信息,然后跳转到Servlet类 processStep2Servlet 对用户的信息封装到customer类里面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h2>step2: 请输入寄送地址和信用卡</h2>
    
    <form action="<%= request.getContextPath() %>/processStep2Servlet" method="post">
        <table border="1" cellpadding="10" cellspacing="0">
            <tr>
                <td colspan="2">寄送信息:</td>
            </tr>
            
            <tr>
                <td>姓名:</td>
                <td><input type="text" name="name"/></td>
            </tr>
            
            <tr>
                <td>地址:</td>
                <td><input type="text" name="address"/></td>
            </tr>
            
            <tr>
                <td>联系电话:</td>
                <td><input type="text" name="phone"/></td>
            </tr>
        
            <tr >
                <td colspan="2">信用卡信息:</td>
            </tr>
            
            <tr>
                <td>信用卡种类</td>
                <td>
                    <input type="radio" name="cardType" value="nong"/>nong
                    <input type="radio" name="cardType" value="gong"/>gong
                </td>
                
            </tr>
            
            <tr>
                <td>信用卡号:</td>
                <td><input type="text" name="card"/></td>
            </tr>
            
            <tr>
                <td colspan="2"> <input type="submit" value="Submit"/></td>
            </tr>
        </table>
    
    </form>
</body>
</html>

 

4.建立一个Servlet类,里面有doPost方法,对用户的信息封装到customer类里面,session实例,保存用户的id信息,便于后边输出,然后重定向到confirm.jsp页面 ,跳转一面使用了相对路径:response.sendRedirect(request.getContextPath()+"/shoppingcart/confirm.jsp");

package com.lanqiao.javatest;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ProcessStep2Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        String address=request.getParameter("address");
        String phone=request.getParameter("phone");
        String cardType=request.getParameter("cardType");
        String card=request.getParameter("card");
        
        Customer customer=new Customer(name,address,phone,cardType,card);
        //System.out.println("jiajai");
        HttpSession session=request.getSession();
        session.setAttribute("customer", customer);
        response.sendRedirect(request.getContextPath()+"/shoppingcart/confirm.jsp");
    }

}

 

5.confirm.jsp页面,使用了session实例,将前边的用户选择的书籍和用户输入的个人信息,输出到这个页面

<%@page import="com.lanqiao.javatest.Customer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        Customer customer=(Customer)session.getAttribute("customer");
        String [] books=(String [])session.getAttribute("books");
    %>
     
     <h2>信息表单</h2>
    <table>
        <tr>
            <td>顾客姓名:</td>
            <td><%=customer.getName() %></td>
        </tr>
        
        <tr>
            <td>顾客地址:</td>
            <td><%=customer.getAddress() %></td>
        </tr>
        
        <tr>
            <td>顾客联系电话:</td>
            <td><%=customer.getPhone() %></td>
        </tr>
        
        <tr>
            <td>顾客卡类型:</td>
            <td><%=customer.getCardType() %></td>
        </tr>
        
        <tr>
            <td>顾客的卡号:</td>
            <td><%=customer.getCard() %></td>
        </tr>
        
        <tr>
            <td>您购买的书:</td>
            <td>
                <%
                    for(String book:books){
                        out.print(book);
                        out.print("\t\t");
                    }
                %>
            </td>
        
        </tr>
    
    </table>
</body>
</html>

 

6.lib下边的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <servlet>
    <description></description>
    <display-name>ProcessStep1Servlet</display-name>
    <servlet-name>ProcessStep1Servlet</servlet-name>
    <servlet-class>com.lanqiao.javatest.ProcessStep1Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ProcessStep1Servlet</servlet-name>
    <url-pattern>/processStep1Servlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>ProcessStep2Servlet</display-name>
    <servlet-name>ProcessStep2Servlet</servlet-name>
    <servlet-class>com.lanqiao.javatest.ProcessStep2Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ProcessStep2Servlet</servlet-name>
    <url-pattern>/processStep2Servlet</url-pattern>
  </servlet-mapping>
</web-app>

 

以上是关于Session案例:简易的购物车的主要内容,如果未能解决你的问题,请参考以下文章

基于session的简易购物车引发的问题

DOM操作相关案例 模态对话框,简易留言板,js模拟选择器hover,tab选项卡,购物车案例

购物车案例 session

案例28-清空购物车

ASP.NET购物车问题

JAVA EE中如何使用session实现购物车的功能?