您的位置:首页 > 其它

cookie应用——显示用户浏览记录

2012-10-15 20:43 507 查看
第一个servlet:向用户显示所有商品和用户浏览过的商品

//首页  day07/index.jsp

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 out = response.getWriter();

out.write("本网站有如下书:<br/>");
//向用户列出网站所有书
Map<String,Book> map = Db.getAll();
for(Map.Entry<String, Book> me : map.entrySet()){
Book book = me.getValue();
out.write("<a href='/day07/servlet/CookieDemo3?id="+book.getId()+"' target='_blank'>" + book.getName() + "</a><br/>");
}

out.write("<br/><br/>");

//显示用户曾经浏览过的商品
out.write("您曾经浏览过:<br/>");
Cookie cookies[] = request.getCookies();
for(int i=0;cookies!=null && i<cookies.length;i++){
if(cookies[i].getName().equals("bookHistory")){  //bookHistory=1_2_5
String ids[] =  cookies[i].getValue().split("\\_");
for(String id : ids){
Book book = (Book) Db.getAll().get(id);
out.write(book.getName() + "<br/>");
}
break;
}
}
}

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

}

class Db{
//单列、双列
public 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","struts开发","老佟","一本好书"));
map.put("4", new Book("4","hibernate开发","老毕","一本好书"));
map.put("5", new Book("5","spring开发","老李","一本好书"));
}

public static Map getAll(){
return map;
}

}

class Book{
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;
}

}

第二个servlet

//显示商品详细信息

public class CookieDemo3 extends HttpServlet {

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

response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

//根据用户带过来的id号,显示用户想查看的商品的详细信息 
String id = request.getParameter("id");
Book book = (Book) Db.getAll().get(id);
out.write("您所查看的书的详细信息是:<br/>");
out.write(book.getId() + "<br/>");
out.write(book.getName() + "<br/>");
out.write(book.getAuthor() + "<br/>");
out.write(book.getDescription() + "<br/>");

//给用户浏览器回送包含浏览历史纪录的cookie
String bookHistory = makeHistory(request,id);  //2_1_3
Cookie cookie = new Cookie("bookHistory",bookHistory);
cookie.setMaxAge(60*100);
cookie.setPath("/day07");
response.addCookie(cookie);
}

private String makeHistory(HttpServletRequest request, String id) {  //5

//bookHistory=null  5      bookHistory=5
//bookHistory=1_5_2 5      bookHistory=5_1_2
//bookHistory=1_2_3 5      bookHistory=5_1_2
//bookHistory=1     5      bookHistory=5_1

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();
break;
}
}

if(bookHistory==null){
bookHistory = id;
return bookHistory;
}

//if(bookHistory.contains(id))      //bookHistory=21_2_51  5
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);
}
}*/

if(list.contains(id)){
list.remove(id);
}else{
if(list.size()>=3){
list.removeLast();
}
}
list.addFirst(id);

//list[3,1,5]   //3_1_5
StringBuffer sb = new StringBuffer();
for(String bookid : list){
sb.append(bookid + "_");
}
bookHistory = sb.deleteCharAt(sb.length()-1).toString();
return bookHistory;
}

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息