20160328 javaweb Cookie 小練習

破玉發表於2016-03-28

利用cookie實現歷史記錄瀏覽:

由於是簡單演示,所以直接用javabean 取代資料庫了

資料儲存類:

package com.dzq.dao;

import java.util.*;

import com.dzq.domain.BookBean;

public class BookDao {
	private static Map<String, BookBean> bookMap=new LinkedHashMap<String, BookBean>();
private BookDao(){
	
}
static {
	
	bookMap.put("1", new BookBean("1","三國演義","99.0","肚肚","河馬出版","那人的故事"));
	bookMap.put("2", new BookBean("2","西御街","99.0","肚吱吱","河馬出版","那人的故事"));
	bookMap.put("3", new BookBean("3","疏忽轉","99.0","肚吱子","河馬出版","那人的故事"));
	bookMap.put("4", new BookBean("4","啪啪啪","99.0","藺澤春","河馬出版","那人的故事"));
	
}
public static Map<String,BookBean> getBooks(){
	return bookMap;
}
public static BookBean getBook(String id){
	return 	bookMap.get(id);
	
}
}

  javaBean 類:

package com.dzq.domain;

import java.io.Serializable;

public class BookBean  implements Serializable{
private String id;
private String name;
private String price;
private String auth;
private String publish;
private String discribe;

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 getPrice() {
	return price;
}
public void setPrice(String price) {
	this.price = price;
}
public String getAuth() {
	return auth;
}
public void setAuth(String auth) {
	this.auth = auth;
}
public String getPublish() {
	return publish;
}
public void setPublish(String publish) {
	this.publish = publish;
}

public String getDiscribe() {
	return discribe;
}
public void setDiscribe(String discribe) {
	this.discribe = discribe;
}
public BookBean(){
	
	
}
public BookBean(String id, String name, String price, String auth,
		String publish, String discribe) {
	this.id = id;
	this.name = name;
	this.price = price;
	this.auth = auth;
	this.publish = publish;
	this.discribe = discribe;
}



}

  顯示歷史圖書資訊和圖書概覽的servlet

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

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

import com.dzq.dao.BookDao;
import com.dzq.domain.BookBean;


@WebServlet("/BookListServlet")
public class BookListServlet 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");
		//查詢所有書並顯示,
		Map<String,BookBean> map=BookDao.getBooks();
		for(Map.Entry<String, BookBean> entry:map.entrySet()){
			BookBean book=entry.getValue();
			response.getWriter().write("<a href='BookInfoServlet?id="+book.getId()+"'>"+book.getName()+"</a><br/>");
		}
		response.getWriter().write("<hr>");
		//顯示之前瀏覽的書
		Cookie [] cs=request.getCookies();
		Cookie findc=null;
		if(cs!=null){
		for(Cookie c:cs){
			if("last".equals(c.getName())){
				findc=c;
			}
		}
		}
		if(findc==null){
			response.getWriter().write("沒有看過任何書");
		}else{
			response.getWriter().write("你最後看過的書:<br>");
			String[] ids=findc.getValue().split(",");
			
			for(String id:ids){
				BookBean book=BookDao.getBook(id);
				response.getWriter().write(book.getName()+"<br/>");
			}
		}
		
		
	}

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

}

  顯示詳細圖書資訊的servlet

package com.dzq.servlet;

import java.io.IOException;

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

import com.dzq.dao.BookDao;
import com.dzq.domain.BookBean;


@WebServlet("/BookInfoServlet")
public class BookInfoServlet 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");
		String id=request.getParameter("id");
		BookBean book=BookDao.getBook(id);
		if(book==null){
			response.getWriter().write("找不到該書");
		}else{
			
			response.getWriter().write("<h1>書名:"+book.getName()+"<h1/>");
			response.getWriter().write("<h3>作者:"+book.getAuth()+"<h3/>");
			response.getWriter().write("<h3>價格:"+book.getPrice()+"<h3/>");
			response.getWriter().write("<h3>出版社:"+book.getPublish()+"<h3/>");
			response.getWriter().write("<h3>描述:"+book.getDiscribe()+"<h3/>");
		}
		//顯示之前的書
		String ids="";
		Cookie [] cs=request.getCookies();
		Cookie findc=null;
		if(cs!=null){
		for(Cookie c:cs){
			if("last".equals(c.getName())){
				findc=c;
			}
		}
		}
		if(findc==null){
			//說明之前沒看過書
			ids+=book.getId();
		}else{
			
			//說明之前看過書
			String[] olds=findc.getValue().split(",");
			StringBuffer buffer=new StringBuffer();
			buffer.append(book.getId()+",");
			for(int i=0;i<olds.length&&buffer.toString().split(",").length<3;i++){
				String old=olds[i];
				if(!old.equals(book.getId())){
					buffer.append(old+",");
				}
			}
			ids=buffer.substring(0, buffer.length()-1);
		}
		
		
		Cookie lastC=new Cookie("last", ids);
		lastC.setMaxAge(3600*24*30);
		lastC.setPath(request.getContextPath());
		response.addCookie(lastC);
	}

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

}

  功能:顯示歷史瀏覽的三本書資訊,並按照瀏覽順序排序,最新瀏覽的排在最前面

 

相關文章