java_web之使用cookie显示曾经访问过的商品
package com.csdn.book;public class Book {private String id;private String name;private String author;private String state;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 getstate() {return state;}public void setstate(String state) {this.state = state;}}//创建工具类package com.csdn.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class JDBCDemo { public static Connection con; public static Connection getConnection(){ if(con==null){ try {Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book?user=root&password=root&useUnicode=true&characterEncoding=UTF-8");} catch (ClassNotFoundException e) { // TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} } return con;}public static void dbClose1(PreparedStatement st, Connection con) {if (st != null) {try {st.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (con != null) {try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void dbClose(ResultSet rs, PreparedStatement st, Connection con) {if (rs != null) {try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}dbClose1(st, con);}}//创建dao包里的链接文件package com.csdn.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.csdn.book.Book;import com.csdn.util.JDBCDemo;public class BookDao { public List<Book> getAll(){ Connection conn=null; PreparedStatement pt=null; ResultSet rs=null; conn=JDBCDemo.getConnection(); String sql="select * from users"; List<Book> list = new ArrayList<Book>(); try {pt=conn.prepareStatement(sql);rs=pt.executeQuery();while(rs.next()){Book book = new Book();book.setId(rs.getString("id"));book.setName(rs.getString("name"));book.setAuthor(rs.getString("author"));book.setstate(rs.getString("state"));list.add(book);}return list;} catch (SQLException e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}/*finally{JDBCDemo.dbClose(rs, pt, conn);}*/ } public Book getBook(String id){ //Book b=null; Connection conn=null; PreparedStatement pt=null; ResultSet rs=null; conn=JDBCDemo.getConnection(); String sql = "select * from users where id=?"; try {pt=conn.prepareStatement(sql);pt.setString(1, id);rs = pt.executeQuery();if(rs.next()){Book b=new Book();b.setId(rs.getString("id"));b.setName(rs.getString("name"));b.setAuthor(rs.getString("author"));b.setstate(rs.getString("state"));return b;}return null;} catch (SQLException e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}/*finally{JDBCDemo.dbClose(rs, pt, conn);}*/ } }//package com.csdn.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.csdn.book.Book;import com.csdn.dao.BookDao;public class BookServlet1 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 />");//new 一个dao对象BookDao bkb=new BookDao();//调用getALL方法List<Book> list = bkb.getAll();for(Book book:list){out.print("<a href='/book/BookServlet2?id="+book.getId()+"'target='_blank'>"+book.getName()+"</a><br />");} out.print("您浏览过的商品是:"+"<br />"); Cookie[] cookie=request.getCookies(); for(int i=0;cookie!=null && i<cookie.length;i++){ if(cookie[i].getName().equals("books")){ String[] ids=cookie[i].getValue().split(","); for(String id:ids){ Book book = bkb.getBook(id); out.print(book.getName()+"<br />"); } } }}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}package com.csdn.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.csdn.book.Book;import com.csdn.dao.BookDao;public class BookServlet2 extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter(); String id = request.getParameter("id"); System.out.println(id); BookDao bd= new BookDao(); Book b=bd.getBook(id); System.out.println(b); out.print("书号: "+b.getId()+"<br />"); out.print("书名: "+b.getName()+"<br />"); out.print("作者: "+b.getAuthor()+"<br />"); out.print("介绍:"+b.getstate()+"<br />"); String value =buildCookValue(id,request); Cookie cookie = new Cookie("books",value); cookie.setMaxAge(7*24*3600); cookie.setPath("/book"); 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,3if(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);}}