cookie小实例第二弹
cookie小实例第二弹——浏览记录
这是一个简陋的小实例,用来记录浏览过的图书信息。主要三个类。
1. DBUtils 存储图书数据用的;
2. ShowAllBooks 展示所有图书信息;
3. ShowBookDetails 显示单本图书详细信息;
一切都在注释中
package com.samblack323.util;import java.util.HashMap;
import java.util.Map;import com.samblack323.entity.Book;public class DBUtils {private static Map<String,Book> books = new HashMap<String,Book>();//直接搞四本书,简单粗暴static {books.put("1", new Book("1", "金瓶梅", 20, "王瑞鑫"));books.put("2", new Book("2", "葵花宝典", 20, "杨成毅"));books.put("3", new Book("3", "九阴真经", 30, "陈光"));books.put("4", new Book("4", "玉女心经", 10, "陈志家"));}//得到所有书,便于展示所有商品public static Map<String,Book> getAllBooks() {return books;}//根据ID等到书,便于展示详细信息public static Book getBookById(String id) {return books.get(id);}
}
package com.samblack323.service;import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;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.samblack323.entity.Book;
import com.samblack323.util.DBUtils;public class ShowAllBooks extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//老样子,先把编码表弄好req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8");PrintWriter out = resp.getWriter();//先拿到书再说Map<String, Book> books = DBUtils.getAllBooks();out.write("下面有四本好书,快来看看!" + "<br>");//把书名都po出来,给上超链接,用get方式提交书的id到处理detail的servletfor (Entry<String, Book> book : books.entrySet()) {String id = book.getKey();out.write("<a href='" + req.getContextPath() + "/cookie/showbookdetails?id=" + id + "' target='_blank'>" + book.getValue().getName()+ "</a>" + "<br>");}out.write("-------------------------------" + "<br>");out.write("最近浏览商品如下:" + "<br>");//搞到所有cookieCookie[] cookies = req.getCookies(); Book book = null;//别忘了判断cookie是否为空for (int i = 0; cookies != null && i < cookies.length; i++) { if ("BookId".equals(cookies[i].getName())) { //得到cookie中的value,即图书的idString value = cookies[i].getValue(); if (value != "") {//图书ID是用'-'拼接起来的字符串,咱们分割一下String[] booksId = value.split("-"); //,总共四本书,倒着取值。j>1表示只浏览最近的3本(因为取不到缓存中下标为0的那本书)for (int j = booksId.length; j > 1; j--) {//注意角标不要越界book = DBUtils.getBookById(booksId[j-1]); //屏幕上走你out.write(book.getName() + "<br>"); }}}}}
}
package com.samblack323.service;import java.io.IOException;
import java.io.PrintWriter;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.samblack323.entity.Book;
import com.samblack323.util.DBUtils;public class ShowBookDetails extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//老样子,先把编码表弄好req.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8");PrintWriter out = resp.getWriter();//得到传来的id参数值String id = req.getParameter("id");//搞到这本书Book book = DBUtils.getBookById(id);//展示出来out.write(book.toString());//搞一个方法,拼接好图书id的值以便存储String historyId = getId(id,req);//搞一个cookieCookie cookie = new Cookie("BookId",historyId);//设置路径cookie.setPath("/");//设置存货时长cookie.setMaxAge(60*60);//把cookie怼回客户端resp.addCookie(cookie);}private String getId(String id, HttpServletRequest req) {Cookie[] cookies = req.getCookies();//如果还没有cookie就直接传当前图书idif (cookies == null) {return id;}String value = "";StringBuffer sb = new StringBuffer();for (int i = 0; i < cookies.length; i++) {if ("BookId".equals(cookies[i].getName())) { //找到这个cookievalue = cookies[i].getValue(); //搞到value,一个图书id与'-'拼接起来的字符串if(value != "") {String[] booksId = value.split("-"); //value有值得话,切割成字符串数组for (int j = 0; j < booksId.length; j++) { //已经存在的记录中,不和当前图书id相同的,都给加到StringBuffer里面,并且后面跟一个'-'if(!id.equals(booksId[j])) { sb.append(booksId[j]+"-"); }}} else { //如果有BookId的这个cookie,但是没值,一样返回当前id即可return id;}}}sb.append(id); //循环结束,别忘了把最新的图书id加到Buffer里面return sb.toString(); //怼回拼接完成的字符串}}
cookie小实例第二弹
cookie小实例第二弹——浏览记录
这是一个简陋的小实例,用来记录浏览过的图书信息。主要三个类。
1. DBUtils 存储图书数据用的;
2. ShowAllBooks 展示所有图书信息;
3. ShowBookDetails 显示单本图书详细信息;
一切都在注释中
package com.samblack323.util;import java.util.HashMap;
import java.util.Map;import com.samblack323.entity.Book;public class DBUtils {private static Map<String,Book> books = new HashMap<String,Book>();//直接搞四本书,简单粗暴static {books.put("1", new Book("1", "金瓶梅", 20, "王瑞鑫"));books.put("2", new Book("2", "葵花宝典", 20, "杨成毅"));books.put("3", new Book("3", "九阴真经", 30, "陈光"));books.put("4", new Book("4", "玉女心经", 10, "陈志家"));}//得到所有书,便于展示所有商品public static Map<String,Book> getAllBooks() {return books;}//根据ID等到书,便于展示详细信息public static Book getBookById(String id) {return books.get(id);}
}
package com.samblack323.service;import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;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.samblack323.entity.Book;
import com.samblack323.util.DBUtils;public class ShowAllBooks extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//老样子,先把编码表弄好req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8");PrintWriter out = resp.getWriter();//先拿到书再说Map<String, Book> books = DBUtils.getAllBooks();out.write("下面有四本好书,快来看看!" + "<br>");//把书名都po出来,给上超链接,用get方式提交书的id到处理detail的servletfor (Entry<String, Book> book : books.entrySet()) {String id = book.getKey();out.write("<a href='" + req.getContextPath() + "/cookie/showbookdetails?id=" + id + "' target='_blank'>" + book.getValue().getName()+ "</a>" + "<br>");}out.write("-------------------------------" + "<br>");out.write("最近浏览商品如下:" + "<br>");//搞到所有cookieCookie[] cookies = req.getCookies(); Book book = null;//别忘了判断cookie是否为空for (int i = 0; cookies != null && i < cookies.length; i++) { if ("BookId".equals(cookies[i].getName())) { //得到cookie中的value,即图书的idString value = cookies[i].getValue(); if (value != "") {//图书ID是用'-'拼接起来的字符串,咱们分割一下String[] booksId = value.split("-"); //,总共四本书,倒着取值。j>1表示只浏览最近的3本(因为取不到缓存中下标为0的那本书)for (int j = booksId.length; j > 1; j--) {//注意角标不要越界book = DBUtils.getBookById(booksId[j-1]); //屏幕上走你out.write(book.getName() + "<br>"); }}}}}
}
package com.samblack323.service;import java.io.IOException;
import java.io.PrintWriter;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.samblack323.entity.Book;
import com.samblack323.util.DBUtils;public class ShowBookDetails extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//老样子,先把编码表弄好req.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8");PrintWriter out = resp.getWriter();//得到传来的id参数值String id = req.getParameter("id");//搞到这本书Book book = DBUtils.getBookById(id);//展示出来out.write(book.toString());//搞一个方法,拼接好图书id的值以便存储String historyId = getId(id,req);//搞一个cookieCookie cookie = new Cookie("BookId",historyId);//设置路径cookie.setPath("/");//设置存货时长cookie.setMaxAge(60*60);//把cookie怼回客户端resp.addCookie(cookie);}private String getId(String id, HttpServletRequest req) {Cookie[] cookies = req.getCookies();//如果还没有cookie就直接传当前图书idif (cookies == null) {return id;}String value = "";StringBuffer sb = new StringBuffer();for (int i = 0; i < cookies.length; i++) {if ("BookId".equals(cookies[i].getName())) { //找到这个cookievalue = cookies[i].getValue(); //搞到value,一个图书id与'-'拼接起来的字符串if(value != "") {String[] booksId = value.split("-"); //value有值得话,切割成字符串数组for (int j = 0; j < booksId.length; j++) { //已经存在的记录中,不和当前图书id相同的,都给加到StringBuffer里面,并且后面跟一个'-'if(!id.equals(booksId[j])) { sb.append(booksId[j]+"-"); }}} else { //如果有BookId的这个cookie,但是没值,一样返回当前id即可return id;}}}sb.append(id); //循环结束,别忘了把最新的图书id加到Buffer里面return sb.toString(); //怼回拼接完成的字符串}}
发布评论