1. 程式人生 > >session的典型案例一:使用session實現購物車

session的典型案例一:使用session實現購物車

使用session實現購物車

step1.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>商品列表</title>
    


  </head>
  
  <body>
    <h4>商品列表</h4>
    <form action="<%=basePath%>servlet/step2" method="post">
       <table border="1px">
         <tr>
           <td>商品名稱</td>
           <td>選擇</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>c++</td>
           <td><input type="checkbox" name="book" value="c++"></td>
         </tr>
        
       </table>
       <input type="submit" value="確認">
    </form>
  </body>
</html>
step2.java:
package com.sa.shopping;
import java.io.IOException;
import java.io.PrintWriter;

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 step2 extends HttpServlet {


	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
          //1.獲取選中的商品資訊
		String[] books=request.getParameterValues("book");
		  //2.將商品放入session
		HttpSession session=request.getSession();
		session.setAttribute("book", books);
		//3.重定向到step2
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
		response.sendRedirect(basePath+"/shopping/step2.jsp");
	}

}
step2.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'step1.jsp' starting page</title>


  </head>
  
  <body>
    <h4>填寫收件人,地址</h4>
    <form action="<%=basePath%>servlet/step3" method="post">
        收件人:<input type="text" name="receiver"><br>
        地址:<input type="text" name="addr"><br>
        <input type="submit" value="提交">
    </form>
  </body>
</html>
step3.java:
package com.sa.shopping;

import java.io.IOException;
import java.io.PrintWriter;

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 step3 extends HttpServlet {


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

             String receiver=request.getParameter("receiver");
             String addr=request.getParameter("addr");
             HttpSession session=request.getSession();
             session.setAttribute("receiver", receiver);
             session.setAttribute("addr", addr);
     		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    		response.sendRedirect(basePath+"/shopping/step3.jsp");
	}

}
step3.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'step1.jsp' starting page</title>
    

  </head>
  
  <body>
    <h4>資訊總覽</h4>
    <form action="<%=basePath%>servlet/pay" method="post">
      <%String[] books=(String[])session.getAttribute("book"); %>
        商品:<%=Arrays.asList(books) %><br>
   收件人:<%=session.getAttribute("receiver") %><br>
   地址:<%=session.getAttribute("addr") %><br>
       <input type="submit" value="付款">
    </form>
  </body>
</html>
測試: