Cheug's Blog

当前位置:网站首页 / JAVA / 正文

Cookie模拟查看书的历史记录

2019-05-22 / JAVA / 813 次围观 / 0 次吐槽 /

image.png

创建一个书的模型

image.png

模仿数据库写一个模型

public class DBUtil {
	private static Map<Integer,Book> bookMap = new HashMap<Integer,Book>();
	//添加书资源
	static{
		bookMap.put(1, new Book(1,"book1","作者","888"));
		bookMap.put(2, new Book(2,"book2","作者","888"));
		bookMap.put(3, new Book(3,"book3","作者","888"));
		bookMap.put(4, new Book(4,"book4","作者","888"));
		bookMap.put(5, new Book(5,"book5","作者","888"));
	}
	//获取全部书籍
	public static Map<Integer,Book> getAllBook(){
		return bookMap;
	}
	//通过ID获取书籍
	public static Book findBookById(Integer id) {
		return bookMap.get(id);
	}
	
}

搜索全部书籍:showAllBookServelt

@WebServlet("/showAllBookServlet")
public class showAllBookServlet extends HttpServlet{
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			resp.setContentType("text/html;charset=utf-8");
			resp.getWriter().write("图书列表 <br/>");
			
			//显示书籍列表
			for(Entry<Integer, Book> entry :DBUtil.getAllBook().entrySet()) {
				String html = "<a href='"+req.getContextPath()+"/ViewBookServlet?id="+entry.getKey()+"'>"+entry.getValue().getName()+"</a><br/>";
				resp.getWriter().write(html);
			}
			
			//显示最近访问记录
			resp.getWriter().write("最近访问 记录<br/>");
			Cookie[] cookies = req.getCookies();
			if(cookies != null) {
				for(Cookie cookie : cookies) {
					if("historyBookIds".equals(cookie.getName())) {
						String strId = cookie.getValue();
						String[] ids = strId.split("-");
						for(String id : ids) {
							String html = "<a href='"+req.getContextPath()+"/ViewBookServlet?id="+id+"'>"+DBUtil.findBookById(Integer.parseInt(id)).getName()+"</a><br/>";
							resp.getWriter().write(html);
						}
					}
				}
			}
		}
}

书籍信息:ViewBookServlet

@WebServlet("/ViewBookServlet")
public class ViewBookServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		//获取ID
		String Str_id = req.getParameter("id");
		resp.getWriter().write("书籍ID:"+Str_id+"<br/>");
		
		//查书籍
		Book book = DBUtil.findBookById(Integer.parseInt(Str_id));
		resp.getWriter().write(book.toString());
		
		//书ID存cookie 
		Cookie[] cookies = req.getCookies();
		if(cookies == null) {
			//未传cookie,生成一个cookie
			Cookie cookie = new Cookie("historyBookIds",Str_id);
			cookie.setMaxAge(60*60);
			resp.addCookie(cookie);
		}else {
			//有传cookie,拼接bookid
			for(Cookie cookie : cookies) {
				if("historyBookIds".equals(cookie.getName())) {
					String historyBookIds = cookie.getValue();
					//判断是否有当前ID
					if(historyBookIds.equals(Str_id)) return;//如果第一个相同,则不执行下面代码
					if(historyBookIds.startsWith(Str_id)) {
						historyBookIds = historyBookIds.replaceAll(Str_id + "-", "");
					}else {
						historyBookIds = historyBookIds.replaceAll("-" + Str_id, "");
					}
					
					//拼接
					historyBookIds += "-" + Str_id;
					
					System.out.println("访问过ID"+historyBookIds);
					
					//响应客户端
					cookie.setValue(historyBookIds);
					cookie.setMaxAge(60*60);
					resp.addCookie(cookie);
					
				}
			}
		}
		
	}
}
额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Cheug's Blog

Copyright Cheug Rights Reserved.