Java教程

实现购物车的Session

本文主要是介绍实现购物车的Session,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
                        [图-1]整体结构图
package cn.itcast;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ListBookServlet
 */
public class ListBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置中文编码。
		response.setContentType ( "text/html;charset=utf-8");
		
		//提示
		PrintWriter out = response.getWriter();
		Collection<Book> books = BookDB.getAll();
		out.write("本站提供的图书有:<br />");
		
		//把数目列出来
		for (Book book : books){
			String url = "/chapter5/PurchaseServlet?id=" + book. getId ();
			System.out.println(url);
			//地址是/chapter5/PurchaseServlet?id=1
			out.write (book.getName ()+ "<a href='"+ url+"'>点击购买</a><br />");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
package cn.itcast;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class CartServlet
 */
public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    List<Book> cart=null;
	    boolean purFlag=true;
	    HttpSession session=request.getSession(false);
	    
	    
	    if(session==null)
	    {
	    	purFlag=false;
	    }
	    else
	    {
	    	cart=(List) session.getAttribute("cart");
	    	if(cart==null)
	    	{
	    		purFlag=false;
	    	}
	    }
	    
	    //页面展示
	    if(!purFlag)
	    {
	    	out.write("对不起");
	    }
	    else
	    {
	    	out.write("您购买的书有");
	    	double price=0;
	    	for(Book book:cart) {
	    		out.write(book.getName());
	    	}
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

package cn.itcast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class PurchaseServlet
 */
public class PurchaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//设置一个ID接受量
		String id=request.getParameter("id");
		
		//id没有,就到数目列表页面
		if(id==null)
		{
			String url="/chapter5/ListBookServlet";
			response.sendRedirect(url);
			return ;
		}
		
		
		Book book =BookDB.getBook(id);
		//创建或者获得session对象
		HttpSession session =request.getSession();
		
		//从session中获得购物车
		List<Book> cart=(List) session.getAttribute("cart");
		
		//创建购物车
		if(cart==null)
		{
			//首次购买,创建一个购物车
			cart=new ArrayList<Book>();
			//将购物车存入session对象
			session.setAttribute("cart", cart);
		}
		
		
		//将商品放入购物车
		cart.add(book);
		
		
		//创建cookie存放session的标识符
		Cookie cookie =new Cookie("jsess",session.getId());
		System.out.println(session.getId());
		cookie.setMaxAge(60*30);
		cookie.setPath("/chapter5");
		response.addCookie(cookie);
		
		
		
		
		//重定向到购物车界面
		String url="/chapter5/CartServlet";
		response.sendRedirect(url);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Session创建的时候会返回一个存放此session的id的cookie给浏览器保存在缓存中。
session id会由服务器端写入到cookie中,后期访问服务器端会以此id为标识。cookie失效时间可以服务器端自己设置。
图-2

案例中又手动创建了一个Cookie所以又两个Cookie

这篇关于实现购物车的Session的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!