1. 程式人生 > >顯示商品的瀏覽記錄

顯示商品的瀏覽記錄

詳細 blank put super() tps static javaweb light ati

package com.cn.cookie;

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

public class CookieDemo extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 設置亂碼問題
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		// 輸出網站所有商品
		response.getWriter().write("本網站有商品如下:<br/>");
		Map<String, Book> map = DB.getAll();
		for (Map.Entry<String, Book> entry : map.entrySet()) {
			Book book = entry.getValue();
			response.getWriter()
					.write("<a  href =‘/Day1/servlet/CookieDemo2?id="
							+ book.getId() + "‘target=_blank‘>"
							+ book.getName() + "</a><br/>");
		}
		response.getWriter().write("<br/>你曾經看過如下商品:</br>");
		Cookie cookies[] = request.getCookies();
		for (int i = 0; cookies != null && i < cookies.length; i++) {
			if (cookies[i].getName().equals("BookHistory")) {
				String ids[] = cookies[i].getValue().split("\\,");
				for (String id : ids) {
					Book book = (Book) DB.getAll().get(id);
					response.getWriter().write(book.getName() + "<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.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;

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

public class CookieDemo2 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();
		// 根據用戶帶過來的id,顯示對應商品的詳細信息。
		String id = request.getParameter("id");
		Book book = (Book) DB.getAll().get(id);
		pw.write(book.getId() + "<br/>");
		pw.write(book.getName() + "<br/>");
		pw.write(book.getAuthor() + "<br/>");
		pw.write(book.getDesc() + "<br/>");
		// 構建cookie,回寫給瀏覽器
		String cookieValue = buildCookie(id, request);
		Cookie cookie = new Cookie("BookHistory", cookieValue);
		cookie.setMaxAge(1 * 60 * 60 * 24 * 30);
		cookie.setPath("/Day1");
		response.addCookie(cookie);
	}

	private String buildCookie(String id, HttpServletRequest request) {
		String BookHistory = null;
		Cookie cookies[] = request.getCookies();
		for (int i = 0; cookies != null && i < cookies.length; i++) {
			if (cookies[i].getName().equals("BookHistory")) {
				BookHistory = cookies[i].getValue();
			}
		}
		// 表示沒有瀏覽過
		if (BookHistory == null) {
			return id;
		}
		/*
		 * asList():數組轉成集合 由於list方法查詢慢,所以用linkedList
		 */
		LinkedList<String> list = new LinkedList<String>(
				Arrays.asList(BookHistory.split("\\,")));
		// 由於是按照查看商品的最近記錄進行顯示的,所以如果以前有瀏覽過,現在又重新瀏覽,那麽把以前的刪掉,然後把現在瀏覽的放在前面
		if (list.contains(id)) {
			list.remove(id);
			// 因為最多只能顯示3個商品,所以把以前瀏覽過的最後一個商品刪除
		} else {
			if (list.size() >= 3) {
				list.removeLast();
			}
		}
		list.addFirst(id);

		StringBuffer sb = new StringBuffer();
		for (String id2 : list) {
			sb.append(id2 + ",");
		}
		return sb.deleteCharAt(sb.length() - 1).toString();

	}

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

}

  

顯示商品的瀏覽記錄