使用cookie显示曾经访问过的商品
package com.hbsi.dao; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List; import com.hbsi.domain.Book;import com.hbsi.util.DBManager; public class BookDao { public List<Book> getAll() { Connection conn = null; PreparedStatement pt = null; ResultSet rs = null; try { conn = DBManager.getConnection(); String sql = "select * from book"; pt = conn.prepareStatement(sql); rs = pt.executeQuery(); List<Book> list = new ArrayList<Book>(); while (rs.next()) { Book b = new Book(); b.setId(rs.getString("id")); b.setName(rs.getString("name")); b.setAuthor(rs.getString("author")); b.setDescript(rs.getString("descript")); list.add(b); } return list; } catch (Exception e) { throw new RuntimeException(e); } finally { DBManager.closeDB(conn, pt, rs); } } public Book getBook(String id) { Connection conn = null; PreparedStatement pt = null; ResultSet rs = null; try { conn = DBManager.getConnection(); String sql = "select * from book where id=?"; pt = conn.prepareStatement(sql); pt.setString(1, id); rs = pt.executeQuery(); //Book b = null; if(rs.next()){ Book b = new Book(); b.setId(rs.getString("id")); b.setName(rs.getString("name")); b.setAuthor(rs.getString("author")); b.setDescript(rs.getString("descript")); return b; } return null; } catch (Exception e) { throw new RuntimeException(e); } finally { DBManager.closeDB(conn, pt, rs); } } public static void main(String [] args){ BookDao db = new BookDao(); Book b = db.getBook("2"); System.out.println(b.getName()); } } package com.hbsi.domain; public class Book { private String id; private String name; private String author; private String descript; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getDescript() { return descript; } public void setDescript(String descript) { this.descript = descript; } } package com.hbsi.servlet; 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.hbsi.dao.BookDao;import com.hbsi.domain.Book; public class CookieDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //显示所有书名 out.print("本网站的商品有:<br/>"); BookDao bd = new BookDao(); List<Book> list = bd.getAll(); for(Book book:list){ //System.out.println(book.getId()); //out.print("<a href='/BookHistory/servlet/CookieDemo2'?id="+book.getId()+">"+book.getName()+"</a><br/>"); out.print("<a href='/BookHistory/servlet/CookieDemo2?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a><br/>"); } //显示浏览过的书名 bookhistory=1,2,3 out.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); out.print(book.getName()+"<br/>"); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } package com.hbsi.servlet; 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.hbsi.dao.BookDao;import com.hbsi.domain.Book; public class CookieDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //显示书的详细信息,根据id获取 String id = request.getParameter("id"); System.out.println(id); BookDao bd = new BookDao(); Book book = bd.getBook(id); System.out.println(book); out.print("书号:"+book.getId()+"<br/>"); out.print("书名:"+book.getName()+"<br/>"); out.print("作者:"+book.getAuthor()+"<br/>"); out.print("描述:"+book.getDescript()+"<br/>"); //将书号写入cookie //bookhistory=null 1 1 //bookhistory=2,3,1 1 1,2,3 //bookhistory=2,3,5 1 1,2,3 //bookhistory=2,3 1 1,2,3 String cookieValue = buildCookValue(id,request); Cookie cookie = new Cookie("bookhistory",cookieValue); cookie.setMaxAge(30*24*3600); cookie.setPath("/BookHistory"); 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(Arrays.asList(bookhistory.split(","))); if(list.contains(id)){ list.remove(id); list.addFirst(id); }else{ //bookhistory=2,3,5 1 1,2,3 //bookhistory=2,3 1 1,2,3 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); } } package com.hbsi.util; import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties; public class DBManager { /** * @param args */ static String driver; static String url; static String username; static String password; static{ InputStream in=DBManager.class.getClassLoader().getResourceAsStream("db.properties"); Properties pro=new Properties(); try { pro.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } driver = pro.getProperty("driver"); url = pro.getProperty("url"); username = pro.getProperty("username"); password = pro.getProperty("password"); try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection(){ Connection con=null; try { con=DriverManager.getConnection(url,username,password); System.out.println("chenggongle"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public static void closeDB(Connection con,Statement st,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(st!=null){ try { st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { getConnection(); }}