Java_乔晓松_使用cookie显示曾经访问过的商品
package com.csdn.cookie;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.csdn.BookDao.BookDao;import com.csdn.doMain.Book;public class CookieDemo2 extends HttpServlet {/****/private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter pw = response.getWriter();pw.print("所有的书:<br />");BookDao bd = new BookDao();List<Book> list = bd.getAll();for(Book book:list){pw.print("<a href='/Cookie/CookieDemo3?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a><br/>");}pw.print("你浏览过的商品:<br/>");Cookie[] cookies = request.getCookies();for(int i=0;cookies!=null && i<cookies.length;i++){if(cookies[i].getName().equals("bookhistory")){String[] ids = cookies[i].getValue().split(",");for(String id:ids){Book book = bd.getBook(id);pw.print(book.getName()+"<br/>");}}}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}package com.csdn.cookie;import java.io.IOException;import java.io.PrintWriter;import java.util.Arrays;import java.util.LinkedList;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.csdn.BookDao.BookDao;import com.csdn.doMain.Book;public class CookieDemo3 extends HttpServlet {/****/private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter pw = response.getWriter();String id = request.getParameter("id");BookDao bd = new BookDao();Book book = bd.getBook(id);//System.out.println(book);pw.print("书号:"+book.getId()+"<br/>");pw.print("书名:"+book.getName()+"<br/>");pw.print("作者:"+book.getAuthor()+"<br/>");pw.print("描述:"+book.getAssess()+"<br/>");String cookieValue = buildCookValue(id,request);Cookie cookie = new Cookie("bookhistory",cookieValue);cookie.setMaxAge(30*24*3600);cookie.setPath("/Cookie");response.addCookie(cookie);}public String buildCookValue(String id,HttpServletRequest request){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();}}if(bookhistory == null){return id;}LinkedList<String> list = new LinkedList<String>(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);}}StringBuffer sb= new StringBuffer();for(String bid: list){sb.append(bid+",");}return sb.deleteCharAt(sb.length()-1).toString();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
查询谷 - www.chaxungu.com
Java_乔晓松_施用cookie显示曾经访问过的商品
编辑:chaxungu时间:2022-10-03 04:54:15分类:web开发