在主页中显示商品,点击商品可以查看商品的具体信息,带用户浏览完之后,回到主页,就会看到自己曾经浏览过什么商品。
商品主页Servlet
CookieDemo4.java是每件商品的详细信息Servlet,在这里,cookie中的"BookHistory"就是存储的浏览记录,记录的是
浏览每件商品的id记录顺序的字符串。组建该字符串的时候需要注意一下四种情况:
1.当一件商品都没有浏览过时,即BookHistory=null
此时加入的商品直接组成字符串CookieValue即可,假设此时浏览过id=1的商品,
CookieValue="1";
2.当浏览的商品是以前浏览过的,即BookHistory="2,5,1"的时候此时又浏览了id=1的
3.由于我们的商品浏览记录有限,只记录前三个,所以每当新的记录加入的时候,要判断
是否满三个,满了的话,删除最后一个,将最新的加入到最前面。
如BookHistory=2,5,4,当又浏览了1商品,要改成BookHistory=1,2,5
4.如果不满3个,又不和以前的重复,直接将记录加在最前面即可
如BookHistory=2,5的时候,又浏览了1商品,此时BookHistory=1,2,5
CookieDemo4.java
商品主页Servlet
- CookieDemo3.java:
- package cn.edu.cookie;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import javax.servlet.ServletException;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- //代表首页的Servlet
- public class CookieDemo3 extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setCharacterEncoding("UTF-8");
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out=response.getWriter();
- //1.输出网站所有商品
- out.write("本网站有如下商品:<br/>");
- Map<String,Book> map = Db.getAll();
- for(Map.Entry<String, Book> entry :map.entrySet())
- {
- Book book=entry.getValue();
- out.print("<a href='/day07/CookieDemo4?id="+book.getId()+"' terget='_blank'>"+book.getName()+"</a><br/>");
- }
- //2.输出用户曾经看过的商品
- 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=(Book)Db.getAll().get(id);
- out.print(book.getName()+"<br/>");
- }
- }
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request,response);
- }
- }
- //如果有检索数据的需求,用双列(Map)的容器,若没有用单列的(List)
- //模拟数据库
- class Db{
- private static Map<String,Book> map=new LinkedHashMap();//HashMap取出来和存进去的顺序不是一致的(自己测试)
- //类初始化代码(写在静态代码块里)
- static{
- map.put("1",new Book("1","JAVAWEB开发","老张","一本好书!"));
- map.put("2",new Book("2","JDBC开发","老张","一本好书!"));
- map.put("3",new Book("3","Spring开发","老黎","一本好书!"));
- map.put("4",new Book("4","struts开发","老毕","一本好书!"));
- map.put("5",new Book("5","android开发","老黎","一本好书!"));
- }
- public static Map getAll(){
- return map;
- }
- }
- class Book{
- private String id;
- private String name;
- private String author;
- private String description;
- public Book(){
- super();
- }
- public Book(String id, String name, String author, String description) {
- super();
- this.id = id;
- this.name = name;
- this.author = author;
- this.description = description;
- }
- 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 getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- }
CookieDemo4.java是每件商品的详细信息Servlet,在这里,cookie中的"BookHistory"就是存储的浏览记录,记录的是
浏览每件商品的id记录顺序的字符串。组建该字符串的时候需要注意一下四种情况:
1.当一件商品都没有浏览过时,即BookHistory=null
此时加入的商品直接组成字符串CookieValue即可,假设此时浏览过id=1的商品,
CookieValue="1";
2.当浏览的商品是以前浏览过的,即BookHistory="2,5,1"的时候此时又浏览了id=1的
商品,这个时候,就要找到原来的那个id,删除它,将此次浏览的记录加入到最前面。
即BookHistory="2,5,1"变成了BookHistory="1,2,5"
3.由于我们的商品浏览记录有限,只记录前三个,所以每当新的记录加入的时候,要判断
是否满三个,满了的话,删除最后一个,将最新的加入到最前面。
如BookHistory=2,5,4,当又浏览了1商品,要改成BookHistory=1,2,5
4.如果不满3个,又不和以前的重复,直接将记录加在最前面即可
如BookHistory=2,5的时候,又浏览了1商品,此时BookHistory=1,2,5
CookieDemo4.java
- package cn.edu.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;
- //显示商品详细信息的Servlet
- public class CookieDemo4 extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setCharacterEncoding("UTF-8");
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out=response.getWriter();
- //根据用户带过来的id,显示相应商品的详细信息
- //getParameter是拿到客户机带过来的数据(通过表单和链接传递的参数使用getParameter)
- String id =request.getParameter("id");//当两个Web组件之间为转发关系时采用getAttribute
- Book book=(Book) Db.getAll().get(id);
- out.write(book.getId()+"<br/>");
- out.write(book.getAuthor()+"<br/>");
- out.write(book.getName()+"<br/>");
- out.write(book.getDescription()+"<br/>");
- //构建cookie,回写给浏览器
- /*一个web应用最多能被浏览器记录20个左右的cookie
- 所以这里要把大量的cookie记录到一起,记录成一个cookie*/
- String CookieValue =buildCookie(id,request);
- Cookie cookie=new Cookie("BookHistory",CookieValue);
- cookie.setMaxAge(1*30*24*3600);
- cookie.setPath("/day07");
- response.addCookie(cookie);
- }
- private String buildCookie(String id, HttpServletRequest request) {
- //四种cookie情况
- //BookHistory=null 1 1
- //BookHistory=2,5,1 1 1,2,5
- //BookHistory=2,5,4 1 1,2,5
- //BookHistory=2,5 1 1,2,5
- 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("\\,")));
- //分情况分配新的cookie值
- if(list.contains(id)){
- //BookHistory=2,5,1 1 1,2,5
- list.remove(id);
- list.addFirst(id);
- }else{
- if(list.size()>=3){
- //BookHistory=2,5,4 1 1,2,5
- list.removeLast();
- list.addFirst(id);
- }else{
- //BookHistory=2,5 1 1,2,5
- //BookHistory=null 1 1
- 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);
- }
- }