1. 程式人生 > >購物車案例 session

購物車案例 session

brush rect path https end oid tor per id號

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