Cookie:显示商品浏览历史记录
package cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
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 Cookie2 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();
//1、输出网站的虽有商品
pw.write("网站有如下商品:"+"<br/>");
Map<String,Book> m=Db.getAll();
for(Map.Entry<String, Book> entry :m.entrySet()){
Book book=entry.getValue();
pw.print("<a href='/servletdemo/servlet/Cookie3?id="+book.getId()+"' target='_blank' >"+book.getName()+"</a>\r\r<br/>");
}
//显示用户看过的商品
pw.print("<br/> 您曾经看过如下商品: <br/>");
Cookie [] co= request.getCookies();
for(int i=0;co!=null && i<co.length;i++){
if(co[i].getName().equals("bookHistory")){
String ids[]=co[i].getValue().split("\\,");
for(String id:ids){
Book boo=(Book)Db.getAll().get(id);
pw.print(boo.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();
static{
map.put("1", new Book("1","java开发","战三","报错的书籍"));
map.put("2", new Book("2","servlet开发","李四","破玩意"));
map.put("3", new Book("3","web开发","万物","没有评价"));
map.put("4", new Book("4","appium开发","麻子","一阿班一阿班"));
map.put("5", new Book("5","ios开发","哈哈","还好吧"));
}
public static Map getAll(){
return map;
}
}
class Book{
private String id;
private String name;
private String author;
private String description;
public Book() {
super();
}
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 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;
//显示商品的详细信息的servlet
public class Cookie3 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.print(book.getId() + "<br/>");
pw.print(book.getAuthor() + "<br/>");
pw.print(book.getDescription() + "<br/>");
pw.print(book.getName() + "<br/>");
// 构建cookie,会写给浏览器
String cookievalue = buildCookie(id, request);
Cookie cookie = new Cookie("bookHistory", cookievalue);
cookie.setMaxAge(1 * 30 * 24 * 60 * 60);
cookie.setPath("/servletdemo");
response.addCookie(cookie);
}
private String buildCookie(String id, HttpServletRequest request) {
// bookHistory=null
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;
}
LinkedList<String> list= new LinkedList(Arrays.asList(bookHistory.split("\\,")));
if(list.contains(id)){
list.remove(id);
list.addFirst(id);
}else{
if(list.size()>=3){
list.removeLast();
list.addFirst(id);
}else{
list.addFirst(id);
}
}
StringBuffer sb=new StringBuffer();
for(String bid:list){
sb.append(bid+",");
}
return sb.deleteCharAt(sb.length()-1).toString();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}