本文主要是介绍使用cookie技术实现,缓存最近浏览过详细信息的三本书的书名列表,并进行相应显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
(1)显示书名列表,及最近阅读的最多三本书的书名
public class ShowGoods extends HttpServlet {/*** */private static final long serialVersionUID = 978235855242977508L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");PrintWriter writer = resp.getWriter();writer.write("所有的书籍是:");writer.write("</br>");for( Entry<String, MyBook> entry: OfferMap.getBookInfo().entrySet()){writer.write("<a href = '"+req.getContextPath()+"/ShowGoodsDetails?id="+entry.getKey()+"'>"+entry.getValue().getName()+"</a>");writer.write("</br>");}writer.write("最近浏览的三本书是:");writer.write("</br>");Cookie[] cookies = req.getCookies();if(cookies != null)for(Cookie cookie : cookies){if("lastVisitBook".equals(cookie.getName()) && cookie.getValue() != null){String[] ids = cookie.getValue().split(",");for(String id : ids){writer.write("<a href = '"+req.getContextPath()+"/ShowGoodsDetails?id="+id+"'>"+OfferMap.getBookInfo().get(id).getName()+"</a>");writer.write("</br>");}}}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubsuper.doPost(req, resp);}}
(2)显示书籍的详细信息,并将阅览的书名信息存到cookie中
public class ShowGoodsDetails extends HttpServlet {/*** */private static final long serialVersionUID = -46163301904321918L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");LinkedHashMap<String, MyBook> map = OfferMap.getBookInfo();PrintWriter writer = resp.getWriter();String id = req.getParameter("id");if(id != null){MyBook myBook = map.get(id);writer.write(myBook.getName()+"的详细信息是:");writer.write("</br>");writer.print("ID是:"+myBook.getId()+"<br/>");writer.print("书本名是:"+myBook.getName()+"<br/>");writer.print("书本价格是:"+myBook.getPrice()+"<br/>");}LinkedList<String> list = new LinkedList<>();StringBuffer sb = new StringBuffer();Cookie[] cookies = req.getCookies();boolean flag = false;if(cookies != null){for(Cookie cookie : cookies){if("lastVisitBook".equals(cookie.getName()) && cookie.getValue() != null){String[] ids = cookie.getValue().split(",");for(String idname : ids){list.add(idname);}//1、如果list中已经有3个,那么删除第一个,(存在一种情况,那就是原先已经有的三个里,这次又重复点击了,那么正常情况下,其位置应该需要变动)if(list.size() >= 3){list.removeFirst();list.addLast(id);}else {//2、如果list中不足3个,不是最后一个,那么就放在后边一个list.add(id);}for(String name : list){sb.append(name+",");}flag = true;}}if(flag){sb = sb.deleteCharAt(sb.length()-1);Cookie[] cookiess = req.getCookies();for(Cookie cookie : cookiess){if("lastVisitBook".equals(cookie.getName()) && cookie.getValue() != null){cookie.setValue(sb.toString());}}//putCookieToResp(resp,sb.toString());}else {putCookieToResp(resp,sb.append(id).toString());}}else {putCookieToResp(resp,sb.append(id).toString());}}private void putCookieToResp(HttpServletResponse resp, String string) {Cookie cookie = new Cookie("lastVisitBook", string);cookie.setMaxAge(3600*24*30);cookie.setPath("/webAndAjax");//cookie.setDomain("localhost");resp.addCookie(cookie);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubsuper.doPost(req, resp);}}
出现的问题:
一开始的时候,在原来有cookie的情况下,修改原来cookie内容的时候,使用的是new的方法,然后在显示时,一直只显示初始cookie的key值对应的内容。
经过查询资料, Cookie不仅仅有名字和值两个属性,还有域(domain),过期时间(expires),路径(path)等属性。其中,不同的域、不同的路径下可以存在同样名字的cookie。可以推测,相同路径和域下不能存在两个相同key值的cookie,所以,new两次,系统在获取的时候,第二次new的会出现错乱,而导致不能获取正确的cookie内容。
解决方法:采用 cookie 值修改的方式。这篇关于使用cookie技术实现,缓存最近浏览过详细信息的三本书的书名列表,并进行相应显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!