在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java使用cookie显示最近查看过的书

2016-7-29 15:37| 发布者: zhangjf| 查看: 666| 评论: 0

摘要: 本文实例为大家分享了Java使用cookie显示最近查看过的书的相关方法,供大家参考,具体内容如下 1.ben包 import java.io.Serializable; public class Book implements Serializable { private String id; priva ...

本文实例为大家分享了Java使用cookie显示最近查看过的书的相关方法,供大家参考,具体内容如下

1.ben包

  1. import java.io.Serializable;
  2. public class Book implements Serializable {
  3. private String id;
  4. private String name;
  5. private String price;
  6. private String auth;
  7. private String publish;
  8. private String description;
  9. public Book() {
  10. }
  11. public Book(String id, String name, String price, String auth,
  12. String publish, String description) {
  13. super();
  14. this.id = id;
  15. this.name = name;
  16. this.price = price;
  17. this.auth = auth;
  18. this.publish = publish;
  19. this.description = description;
  20. }
  21. public String getDescription() {
  22. return description;
  23. }
  24. public void setDescription(String description) {
  25. this.description = description;
  26. }
  27. public String getId() {
  28. return id;
  29. }
  30. public void setId(String id) {
  31. this.id = id;
  32. }
  33. public String getName() {
  34. return name;
  35. }
  36. public void setName(String name) {
  37. this.name = name;
  38. }
  39. public String getPrice() {
  40. return price;
  41. }
  42. public void setPrice(String price) {
  43. this.price = price;
  44. }
  45. public String getAuth() {
  46. return auth;
  47. }
  48. public void setAuth(String auth) {
  49. this.auth = auth;
  50. }
  51. public String getPublish() {
  52. return publish;
  53. }
  54. public void setPublish(String publish) {
  55. this.publish = publish;
  56. }
  57. }
复制代码

2.Dao包

  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import cn.huiyu.ben.Book;
  4. public class BookDao {
  5. private static Map<String,Book> bookMap = new LinkedHashMap<String, Book>();
  6. private BookDao() {
  7. }
  8. static{
  9. bookMap.put("1", new Book("1","1111","11.0","zqwang","111出版社","111111111"));
  10. bookMap.put("2", new Book("2","2222","22.0","zqwang","222出版社","222222222"));
  11. bookMap.put("3", new Book("3","3333","33.0","zqwang","333出版社","333333333"));
  12. }
  13. public static Map<String,Book> getBooks(){
  14. return bookMap;
  15. }
  16. public static Book getBook(String id){
  17. return bookMap.get(id);
  18. }
  19. }
复制代码

3.servlet

  1. public void doGet(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. response.setContentType("text/html;charset=utf-8");
  4. //1.查询数据库中所有的书展示
  5. Map<String,Book> map = BookDao.getBooks();
  6. for(Map.Entry<String , Book> entry : map.entrySet()){
  7. Book book = entry.getValue();
  8. response.getWriter().write("<a href='"+request.getContextPath()+"/servlet/BookInfoServlet?id="+book.getId()+"'>"+book.getName()+"</a><br>");
  9. }
  10. response.getWriter().write("<hr>");
  11. //2.显示之前看过的书
  12. Cookie [] cs = request.getCookies();
  13. Cookie findC = null;
  14. if(cs!=null){
  15. for(Cookie c : cs){
  16. if("last".equals(c.getName())){
  17. findC = c;
  18. }
  19. }
  20. }
  21. if(findC == null){
  22. response.getWriter().write("没有看过任何书!");
  23. }else{
  24. response.getWriter().write("您曾经浏览过的书:<br>");
  25. String[] ids = findC.getValue().split(",");
  26. for(String id : ids){
  27. Book book = BookDao.getBook(id);
  28. response.getWriter().write(book.getName()+"<br>");
  29. }
  30. }
  31. }
复制代码

4.servlet

  1. public void doGet(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. response.setContentType("text/html;charset=utf-8");
  4. //1.获取要看的书的id,查询数据库找出书,输出书的详细信息
  5. String id = request.getParameter("id");
  6. Book book = BookDao.getBook(id);
  7. if(book==null){
  8. response.getWriter().write("找不到这本书!");
  9. return;
  10. }else{
  11. response.getWriter().write("<h1>书名:"+book.getName()+"</h1>");
  12. response.getWriter().write("<h3>作者:"+book.getAuth()+"</h3>");
  13. response.getWriter().write("<h3>售价:"+book.getPrice()+"</h3>");
  14. response.getWriter().write("<h3>出版社:"+book.getPublish()+"</h3>");
  15. response.getWriter().write("<h3>描述信息:"+book.getDescription()+"</h3>");
  16. }
  17. //2.发送cookie保存最后看过的书
  18. // --- 1 --> 1
  19. // 1 --2,1 --> 2,1
  20. // 2,1--3,2,1 --> 3,2,1
  21. // 3,2,1 -- 4,3,2 --> 4,3,2
  22. // 4,3,2 --3,4,2 --> 3,4,2
  23. String ids = "";
  24. Cookie [] cs = request.getCookies();
  25. Cookie findC = null;
  26. if(cs!=null){
  27. for(Cookie c : cs){
  28. if("last".equals(c.getName())){
  29. findC = c;
  30. }
  31. }
  32. }
  33. if(findC == null){
  34. //说明之前没有看过书的记录
  35. ids += book.getId();
  36. }else{
  37. //说明之前有历史看过的书的记录,需要根据历史记录算一个新的记录出来
  38. String [] olds = findC.getValue().split(",");
  39. StringBuffer buffer = new StringBuffer();
  40. buffer.append(book.getId()+",");
  41. for(int i = 0;i<olds.length && buffer.toString().split(",").length<3 ;i++){
  42. String old = olds[i];
  43. if(!old.equals(book.getId())){
  44. buffer.append(old+",");
  45. }
  46. }
  47. ids = buffer.substring(0, buffer.length()-1);
  48. }
  49. Cookie lastC = new Cookie("last",ids);
  50. lastC.setMaxAge(3600*24*30);
  51. lastC.setPath(request.getContextPath());
  52. response.addCookie(lastC);
  53. }
复制代码

以上就是本文的全部内容,希望对大家学习Java使用cookie显示最近查看过的书的方法有所帮助。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 10:30

Copyright 2015-2025 djqfx

返回顶部