您的位置:首页 > 其它

cookie实例-最近浏览记录

2014-10-06 23:19 441 查看
该程序实现显示最近的浏览记录,当点击书名链接时可查看关于该书的详细信息,返回后可在最近的浏览列表中查看浏览记录,最后点击的出现在最前面。
1.创建基本类Book存储基本数据
1: package net.csing;
2:
3: public class Book {
4:   public Book(){}
5:
6:   public Book(String id,String name, String auth, String price, String publish,
7:       String description) {
8:     super();
9:     this.id=id;
10:     this.name = name;
11:     this.auth = auth;
12:     this.price = price;
13:     this.publish = publish;
14:     this.description = description;
15:   }
16:   private String id;
17:   private String name;
18:   private String auth;
19:   private String price;
20:   private String publish;
21:   private String description;
22:
23:   public String getId() {
24:     return id;
25:   }
26:
27:   public void setId(String id) {
28:     this.id = id;
29:   }
30:   public String getName() {
31:     return name;
32:   }
33:   public void setName(String name) {
34:     this.name = name;
35:   }
36:   public String getAuth() {
37:     return auth;
38:   }
39:   public void setAuth(String auth) {
40:     this.auth = auth;
41:   }
42:   public String getPrice() {
43:     return price;
44:   }
45:   public void setPrice(String price) {
46:     this.price = price;
47:   }
48:   public String getPublish() {
49:     return publish;
50:   }
51:   public void setPublish(String publish) {
52:     this.publish = publish;
53:   }
54:   public String getDescription() {
55:     return description;
56:   }
57:   public void setDescription(String description) {
58:     this.description = description;
59:   }
60:
61:   @Override
62:   public String toString() {
63:     return "[name=" + name + ", auth=" + auth + ", price=" + price
64:         + ", publish=" + publish + ", description=" + description + "]";
65:   }
66: }
67:


2.创建BookDao类来操作Book数据并初始化要显示的book列表:

1: package net.csing;
2:
3: import java.util.LinkedHashMap;
4: import java.util.Map;
5:
6: public class BookDao {
7:   private static Map<String, Book> map=new LinkedHashMap<String, Book>();
8:   public BookDao(){};
9:   static{
10:     map.put("1", new Book("1","三国演义","99.0","朴乾","黑马出版社","一群男人纠结不清的故事...."));
11:     map.put("2", new Book("2","西游记","10.0","曹睿","传智出版社","一个和尚一个猴子一头猪和一个秃子去西天的故事..."));
12:     map.put("3", new Book("3","水浒传","2.0","奥巴马","人民教育出版社","105个男人和3个女人闯荡江湖的故事"));
13:     map.put("4", new Book("4","金瓶梅","200.0","哈利波特","科技出版社","混乱不堪的故事..."));
14:     map.put("5", new Book("5","逆行","3.0","高深","人民教育出版社","我们逆行"));
15:   }
16:   //根据id得到书;
17:   public static Book getBookById(String id){
18:     return map.get(id);
19:   }
20:   //获得所有的书的集合;
21:   public static Map<String,Book> getAllBook(){
22:     return map;
23:   }
24: }
25:


3.显示全部书籍的列表以及最近的浏览记录

1: package net.csing;
2:
3: import java.io.IOException;
4: import java.io.Writer;
5: import java.util.Iterator;
6: import java.util.Map;
7: import java.util.Map.Entry;
8: import java.util.Set;
9:
10: import javax.servlet.ServletException;
11: import javax.servlet.http.Cookie;
12: import javax.servlet.http.HttpServlet;
13: import javax.servlet.http.HttpServletRequest;
14: import javax.servlet.http.HttpServletResponse;
15:
16: public class Index extends HttpServlet {
17:
18:   public void doGet(HttpServletRequest request, HttpServletResponse response)
19:       throws ServletException, IOException {
20:
21:     //解决中文乱码问题;
22:     response.setContentType("text/html;charset=utf-8");
23:
24:     Writer outer=response.getWriter();
25:    outer.write("本站的所有书籍如下:<br>");
26:     //从类的方法中获得所有的书,并遍历;
27:     Map<String, Book> map=BookDao.getAllBook();
28:     Set<Entry<String, Book>> set=map.entrySet();
29:     Iterator<Entry<String,Book>> iterator=set.iterator();
30:     while(iterator.hasNext()){
31:       Entry<String, Book> entry=iterator.next();
32:       //String idString=entry.getKey();
33:       Book book=entry.getValue();
34:       //将取得书以链接列表的形式打印到浏览器页面中。
35:       //在链接页面的地址中返回该链接书的id
36:       outer.write("<a href='"+request.getContextPath()+"/servlet/Info?id="+book.getId()+"'>"+book.getName()+"</a><br>");
37:     }
38:     outer.write("<hr>");
39:     //获得cookie信息,以便查看之前的浏览记录。
40:     Cookie[] cookie=request.getCookies();
41:     //设置对应的浏览记录;
42:     Cookie findCookie=null;
43:     //判断cookie是否为空。
44:     if(cookie==null){
45:       outer.write("您尚未浏览过本站的图书...");
46:     }
47:     //cookie不为空时查看是否有符合条件的浏览记录
48:     else if(cookie!=null){
49:       for(Cookie c:cookie){
50:         if("history".equals(c.getName())){
51:           findCookie=c;
52:           break;
53:         }
54:       }
55:     }
56:     //如果有浏览记录,则分拆该值得到id并获取书名。
57:     if(findCookie!=null){
58:       outer.write("您最近浏览过的书籍如下:"+"<br><br>");
59:       String[] bookid=findCookie.getValue().split("-");
60:       for(String id:bookid){
61:         outer.write(BookDao.getBookById(id).getName()+"<br>");
62:       }
63:     }
64:   }
65:
66:   public void doPost(HttpServletRequest request, HttpServletResponse response)
67:      throws ServletException, IOException {
68:
69:     response.setContentType("text/html");
70:
71:   }
72:
73: }
74:


4.对于每一本书籍,显示该书的详细信息:

1: package net.csing;
2:
3: import java.io.IOException;
4: import java.io.Writer;
5:
6: import javax.servlet.ServletException;
7: import javax.servlet.http.Cookie;
8: import javax.servlet.http.HttpServlet;
9: import javax.servlet.http.HttpServletRequest;
10: import javax.servlet.http.HttpServletResponse;
11:
12: public class Info extends HttpServlet {
13:
14:   public void doGet(HttpServletRequest request, HttpServletResponse response)
15:       throws ServletException, IOException {
16:
17:     response.setContentType("text/html;charset=utf-8");
18:     Writer outer=response.getWriter();
19:
20:     //根据请求信息中的id获得请求的书名id
21:     String idString=request.getParameter("id");
22:     //打印书信息;
23:     outer.write(BookDao.getBookById(idString).toString()+"<a href='"+request.getContextPath()+"/servlet/Index"+"'>返回</a>");
24:     //获得所有的cookie
25:    Cookie[] cookies=request.getCookies();
26:     Cookie findCookie=null;
27:     //创建stringbuffered存储临时的id字段串;
28:     StringBuffer idBuffer=new StringBuffer();
29:     //只有当cookie不为空时的判断才有意义。
30:     if(cookies!=null){
31:       //遍历并查找对应的cookie浏览记录。
32:       for(Cookie c:cookies){
33:         if("history".equals(c.getName())){
34:           findCookie=c;
35:           break;
36:         }
37:       }
38:     }
39:     //不管cookies是否为空,我们都需要判断是否查找到对应的浏览历史cookie。
40:     if(findCookie==null){
41:       idBuffer.append(idString);
42:     }
43:     else{
44:       String[] find=findCookie.getValue().split("-");
45:       //将最近的书的id放在stringbuffered的最前面,现存的依次添加到后面,这个方法简直太棒了。
46:       idBuffer.append(idString);
47:       //依次将现存的id添加到buffered中,并且限制了id的长度。
48:       for(int x=0;(x<find.length)&&(idBuffer.toString().split("-").length<3);x++){
49:         //之前浏览的id值和当前浏览的值不相同时才添加。
50:         if(!idString.equals(find[x])){
51:           idBuffer.append("-"+find[x]);
52:         }
53:       }
54:     }
55:     //将获取的id浏览记录字段串添加到对应的cookie中。
56:     Cookie cookie=new Cookie("history", idBuffer.toString());
57:     //设置cookie存活时间以及存储路径。
58:     cookie.setMaxAge(Integer.MAX_VALUE);
59:     cookie.setPath(request.getContextPath());
60:     //将获取好的cookie存储到客户端浏览器中。
61:     response.addCookie(cookie);
62:   }
63:
64:   public void doPost(HttpServletRequest request, HttpServletResponse response)
65:       throws ServletException, IOException {
66:
67:    response.setContentType("text/html");
68:
69:   }
70:
71: }
72:


5.最终效果图如下:

刚打开时的界面:





浏览后的界面:



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