• JavaWeb--会话与状态管理2--cookie 显示最近浏览商品


    ps:不能以books作为标识符(冒号),但是可以用books,好像还可以用--,不知道为什么

    books.jsp

    <%--
      Created by IntelliJ IDEA.
      User: Skye
      Date: 2017/12/12
      Time: 10:23
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h4>Book Page</h4>
        <a href="book.jsp?book=JavaWeb">JavaWeb</a><br>
        <a href="book.jsp?book=Java">Java</a><br>
        <a href="book.jsp?book=JavaScript">JavaScript</a><br>
        <a href="book.jsp?book=MySQL">MySQL</a><br>
        <a href="book.jsp?book=Ajax">Ajax</a><br>
        <a href="book.jsp?book=TCPIP">TCPIP</a><br>
        <a href="book.jsp?book=Spring">Spring</a><br>
        <a href="book.jsp?book=Hibernate">Hibernate</a><br>
        <a href="book.jsp?book=Struts">Struts</a><br>
    
        <br>
    <%
        //显示最近浏览的 5 本书
        Cookie[] cookies = request.getCookies();
    
        //获取所有的 Cookie  
        if(cookies != null && cookies.length > 0){
            //从中筛选出 Book 的 Cookie:
            for(Cookie cc: cookies){
                String bookName = cc.getName();
                //如果 cookieName 为books 开头的即符合条件
                if(bookName.startsWith("books")){
                    //显示 cookieValue
                    out.print(cc.getValue());
                    out.print("<br>");
                }
            }
        }
    %>
    </body>
    
    
    </html>
    

      book.jsp

    <%@ page import="java.util.List" %>
    <%@ page import="java.util.ArrayList" %><%--
      Created by IntelliJ IDEA.
      User: Skye
      Date: 2017/12/12
      Time: 10:22
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h4>Book Detail Page</h4>
        Book: <%= request.getParameter("book") %>
        <a href="books.jsp">Return</a>
    <%
        //把书的信息以 Cookie 方式传回给浏览器,删除一个 Cookie
        String book = request.getParameter("book");
    
        //每次都重新读取浏览器中Cookie的信息,进行存储
        List<Cookie> cookies = new ArrayList<Cookie>();
    
        Cookie temp = null;
        Cookie[] c = request.getCookies();
        if(c!= null && c.length > 0){
            for(Cookie cc : c){
                String bookName = cc.getName();
                 //确定要被删除的 Cookie:books开头的 Cookie
                if(bookName.startsWith("books")){
                    cookies.add(cc);
    
                    //若已经存储在浏览器的Cookie信息中
                    if(cc.getValue().equals(book)){
                        temp = cc;
                    }
                }
            }
        }
    
        //若List中的书的信息超过了5本,且本次读取的书不在List中,则删除最早的一本
        if(cookies.size() >= 5 && temp == null){
            temp = cookies.get(0);
        }
    
        //删除该书的信息
        if(temp != null){
            temp.setMaxAge(0);
            //删除时用的是先设置最大时效为0,再加入Cookie
            response.addCookie(temp);
        }
    
        //把从 books.jsp 传入的 book 作为一个 Cookie 返回
        Cookie cookie = new Cookie("books" + book, book);
        response.addCookie(cookie);
    %>
    </body>
    </html>
  • 相关阅读:
    System lock
    skip_slave_start
    慢查询日志分析
    wait_timeout 、interactive_timeout、slave_net_timeout、master_heartbeat_period
    reset slave,reset slave all,reset master都干了些啥?
    强制删除有外键约束的数据
    集群拓扑结构变更
    在线开启gtid与在线关闭gtid
    less
    pg流复制
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8027946.html
Copyright © 2020-2023  润新知