20160328 javaweb Cookie 小练习
利用cookie实现历史记录浏览:
由于是简单演示,所以直接用javabean 取代数据库了
数据存储类:
package com.dzq.dao; import java.util.*; import com.dzq.domain.BookBean; public class BookDao { private static Map<String, BookBean> bookMap= new LinkedHashMap<String, BookBean>(); private BookDao(){ } static { bookMap.put( "1" , new BookBean( "1" , "三国演义" , "99.0" , "肚肚" , "河马出版" , "那人的故事" )); bookMap.put( "2" , new BookBean( "2" , "西御街" , "99.0" , "肚吱吱" , "河马出版" , "那人的故事" )); bookMap.put( "3" , new BookBean( "3" , "疏忽转" , "99.0" , "肚吱子" , "河马出版" , "那人的故事" )); bookMap.put( "4" , new BookBean( "4" , "啪啪啪" , "99.0" , "蔺泽春" , "河马出版" , "那人的故事" )); } public static Map<String,BookBean> getBooks(){ return bookMap; } public static BookBean getBook(String id){ return bookMap.get(id); } } |
javaBean 类:
package com.dzq.domain; import java.io.Serializable; public class BookBean implements Serializable{ private String id; private String name; private String price; private String auth; private String publish; private String discribe; 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 getPrice() { return price; } public void setPrice(String price) { this .price = price; } public String getAuth() { return auth; } public void setAuth(String auth) { this .auth = auth; } public String getPublish() { return publish; } public void setPublish(String publish) { this .publish = publish; } public String getDiscribe() { return discribe; } public void setDiscribe(String discribe) { this .discribe = discribe; } public BookBean(){ } public BookBean(String id, String name, String price, String auth, String publish, String discribe) { this .id = id; this .name = name; this .price = price; this .auth = auth; this .publish = publish; this .discribe = discribe; } } |
显示历史图书信息和图书概览的servlet
import java.io.IOException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dzq.dao.BookDao; import com.dzq.domain.BookBean; @WebServlet ( "/BookListServlet" ) public class BookListServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html;charset=utf-8" ); //查询所有书并显示, Map<String,BookBean> map=BookDao.getBooks(); for (Map.Entry<String, BookBean> entry:map.entrySet()){ BookBean book=entry.getValue(); response.getWriter().write( "<a href='BookInfoServlet?id=" +book.getId()+ "'>" +book.getName()+ "</a><br/>" ); } response.getWriter().write( "<hr>" ); //显示之前浏览的书 Cookie [] cs=request.getCookies(); Cookie findc= null ; if (cs!= null ){ for (Cookie c:cs){ if ( "last" .equals(c.getName())){ findc=c; } } } if (findc== null ){ response.getWriter().write( "没有看过任何书" ); } else { response.getWriter().write( "你最后看过的书:<br>" ); String[] ids=findc.getValue().split( "," ); for (String id:ids){ BookBean book=BookDao.getBook(id); response.getWriter().write(book.getName()+ "<br/>" ); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
显示详细图书信息的servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package com.dzq.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dzq.dao.BookDao; import com.dzq.domain.BookBean; @WebServlet ( "/BookInfoServlet" ) public class BookInfoServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html;charset=utf-8" ); String id=request.getParameter( "id" ); BookBean book=BookDao.getBook(id); if (book== null ){ response.getWriter().write( "找不到该书" ); } else { response.getWriter().write( "<h1>书名:" +book.getName()+ "<h1/>" ); response.getWriter().write( "<h3>作者:" +book.getAuth()+ "<h3/>" ); response.getWriter().write( "<h3>价格:" +book.getPrice()+ "<h3/>" ); response.getWriter().write( "<h3>出版社:" +book.getPublish()+ "<h3/>" ); response.getWriter().write( "<h3>描述:" +book.getDiscribe()+ "<h3/>" ); } //显示之前的书 String ids= "" ; Cookie [] cs=request.getCookies(); Cookie findc= null ; if (cs!= null ){ for (Cookie c:cs){ if ( "last" .equals(c.getName())){ findc=c; } } } if (findc== null ){ //说明之前没看过书 ids+=book.getId(); } else { //说明之前看过书 String[] olds=findc.getValue().split( "," ); StringBuffer buffer= new StringBuffer(); buffer.append(book.getId()+ "," ); for ( int i= 0 ;i<olds.length&&buffer.toString().split( "," ).length< 3 ;i++){ String old=olds[i]; if (!old.equals(book.getId())){ buffer.append(old+ "," ); } } ids=buffer.substring( 0 , buffer.length()- 1 ); } Cookie lastC= new Cookie( "last" , ids); lastC.setMaxAge( 3600 * 24 * 30 ); lastC.setPath(request.getContextPath()); response.addCookie(lastC); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
功能:显示历史浏览的三本书信息,并按照浏览顺序排序,最新浏览的排在最前面
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签