购物车案例 session

Posted

tags:

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

package com.cn.session;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

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

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();

		HttpSession hs = request.getSession();

		pw.write("本网站有如下商品:<br/>");
		// 获取所有商品的数据
		Map<String, Book> map = DB.getAll();
		for (Map.Entry<String, Book> entry : map.entrySet()) {
			Book book = entry.getValue();
			/*
			 * 重写url地址,传递session的id号 。所以session要存在 原因:cookie可能被禁止,所以要重写
			 */
			String url = response.encodeURL("/Day1/servlet/BuyServlet?id="
					+ book.getId());
			pw.write(book.getName() + "<a href = ‘" + url
					+ "‘ target=‘_blank‘>购买</a><br/>");
		}
	}

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

}

// 商品的数据库
class DB {
	private static Map<String, Book> map = new LinkedHashMap<String, Book>();
	static {
		map.put("1", new Book("1", "javaweb开发", "老张", "一本好书"));
		map.put("2", new Book("2", "jdbc", "老林", "一本好书"));
		map.put("3", new Book("3", "java", "老萧", "一本好书"));
		map.put("4", new Book("4", "tomcat", "老哥", "一本好书"));
		map.put("5", new Book("5", "mysql", "老李", "一本好书"));
	}

	public static Map getAll() {
		return map;
	}
}

class Book {
	private String id;
	private String name;
	private String author;
	private String desc;

	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Book(String id, String name, String author, String desc) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.desc = desc;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}
}

  

package com.cn.session;

import java.io.IOException;
import java.util.ArrayList;
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;

public class BuyServlet extends HttpServlet {
	// 商品购买界面
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id"); // getParameter()是获取POST/GET传递的参数值
		Book book = (Book) DB.getAll().get(id);// 获取商品的id值
		// 这里的session只要查询,所以用false
		HttpSession hs = request.getSession(false);
		// 通过集合的方式获取商品 getAttribute()是获取SESSION的值
		List<Book> list = (List<Book>) hs.getAttribute("list");
		/*
		 * 判断是否有购买过,如果没有买过东西,就设置下值 如果买过东西,就获取值
		 */
		if (list == null) {
			list = new ArrayList();
			hs.setAttribute("list", list);
		}
		// 无论是否有购买过,把id值进行添加
		list.add(book);
		String url = response.encodeRedirectURL(request.getContextPath()
				+ "/servlet/ListCarServlet");
		// 重定向到购物车的界面
		response.sendRedirect(url);
	}

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

}

  

package com.cn.session;

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;

public class ListCarServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();

		HttpSession session = request.getSession(false);
		if (session == null) {
			pw.write("你还没有购买任何商品");
			return;
		}
		List<Book> list = (List<Book>) session.getAttribute("list");
		for (Book book : list) {
			pw.write(book.getName());
		}
	}

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

}

  

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

案例28-清空购物车

Session机制二(案例)

16_会话技术_Session案例

session和el表达式

第11章WEB11-Cookie&Session篇

php之购物车类思路及代码