1. 程式人生 > >session 案例實現簡單的購物

session 案例實現簡單的購物

使用session完成簡單的購物功能。

大型的上午網站一般使用cookie來實現,目的是減少伺服器的壓力。

package cn.itcast.shoping;

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

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


//代表首頁的servlet
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 writer = response.getWriter();
		//1.輸出網頁所有商品
		writer.println("本網站所有商品:<br>");
		Map<String, Book> map = DB.getall();
		for(Map.Entry<String, Book> entry :map.entrySet()){
			Book book = entry.getValue();
			writer.print("<a href='/web2/servlet/BuyServlet?id=" + book.getId()+"' target='_blank\'>" + book.getName()+"</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();
	static {
		map.put("1", new Book("1","java web開發","老張","一本好書"));
		map.put("2", new Book("2","java jdbc開發","老張","一本好書"));
		map.put("3", new Book("3","java spring開發","老a","一本好書"));
		map.put("4", new Book("4","java struts開發","老v","一本好書"));
		map.put("5", new Book("5","java android開發","老b","一本好書"));
	}
	
	public static Map getall(){
		return map;
	}
	
	
}



class Book implements Serializable{
	private String id;
	private String name;
	private String author;
	private String description;
	
	
	
	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Book(String id, String name, String author, String description) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.description = description;
	}
	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 getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	
	
	
}
package cn.itcast.shoping;

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

public class BuyServlet extends HttpServlet {

//完成購買
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		Book book = (Book) DB.getall().get(id);
		HttpSession  session = request.getSession();
		//手動cookie 形式傳送sessionid,以解決關閉瀏覽器後上次買的東西還在 
		String sessionid = session.getId();
		
		Cookie cookie = new Cookie("JSESSIONID",sessionid);
		cookie.setMaxAge(30*60);
		System.out.println(request.getContextPath());
		cookie.setPath(request.getContextPath());
		response.addCookie(cookie);
		
		//從session中得到使用者用於儲存所有書的集合(購物車)
		List list = (List) session.getAttribute("list");
		if(list==null){
			list = new ArrayList();
			session.setAttribute("list", list);
		}
		list.add(book);
		System.out.println(book.getName());
		//request.getRequestDispatcher("/servlet/ListCarServlet").forward(request, response);
		response.sendRedirect(request.getContextPath()+"/servlet/ListCarServlet");
		
		
		
	}


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

	doGet(request, response);
	}

}

 

package cn.itcast.shoping;

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 writer = response.getWriter();
		
		HttpSession session =  request.getSession(false);
		if(session==null){
			writer.println("您沒有購買任何商品");
			return;
		}
		writer.println("您購買了如下商品:<br/>");
		List<Book> list = (List) session.getAttribute("list");
		for (Book book:list){
			writer.println(book.getName());
		}
	}


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

}