YuFeng

cookies
使用cookie的相关知识点:注意:cookie的相关内容只能是英文 从打开一个浏览器访问某个站点,到关闭这个浏...
扫描右侧二维码阅读全文
22
2019/06

cookies

使用cookie的相关知识点:

cookie的购物.png

注意:cookie的相关内容只能是英文

从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,成为一次会话。会    话技术就是记录这次会话中客户端的状态与数据的。
会话技术分为Cookie和Session:
Cookie:数据存储在客户端本地,减少服务器端的存储的压力,安全性不好,客户端    可以清除cookie
Session:将数据存储到服务器端,安全性相对好,增加服务器的压力


cookie.setMaxAge(int seconds); ---时间秒
注意:如果不设置持久化时间,cookie会存储在浏览器的内存中,浏览器关闭  cookie信息销毁(会话级别的cookie),如果设置持久化时间,cookie信息会     被持久化到浏览器的磁盘文件里


服务器端怎么接受客户端携带的Cookie:
cookie信息是以请求头的方式发送到服务器端的:
1)通过request获得所有的Cookie:
Cookie[] cookies = request.getCookies();
2)遍历Cookie数组,通过Cookie的名称获得我们想要的Cookie
for(Cookie cookie : cookies){
if(cookie.getName().equal(cookieName)){
String cookieValue = cookie.getValue();
}
}


cookies的设置与使用:
package cn;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookiesServlet extends HttpServlet {
    
    private static final long serialVersionUID = -8633564208416167796L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建一个cookie
        Cookie cookie = new Cookie("yufeng", "yeah");
        //设置cookie的有效时间,这个时间是缓存在浏览器中的,浏览器最后存储在硬盘上,时间是以秒为单位的
        cookie.setMaxAge(5*60);
        //设置cookie所归属的路径,如果不设置只在当前项目上有效,且以当前路径的父目录下的所有路径都会携带此cookie
        //cookie.setPath("/cn.cookies");
        //整个tomecat上都有效
        //cookie.setPath("/");
        //发送cookie
        response.addCookie(cookie);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    }
}


消除cookies:
消除cookies需要相同的cookie的名称并且设置有效时长为0

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RemoveCookie extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("yufeng", "");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
    }
}



使用cookie显示上次的访问时间

package accesstime;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LastAccessTime extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //添加访问时间cookie
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(date);
        Cookie cookie = new Cookie("lastTime", now);
        cookie.setMaxAge(10);
        response.addCookie(cookie);
        //读取上次访问的cookie
        //获取cookies
        Cookie[] cookies = request.getCookies();
        //设定从cookies读取到时间参数
        String accessTime = null;
        if(cookies != null) {
            for(Cookie c : cookies) {
                if("lastTime".equals(c.getName())) {
                    accessTime = c.getValue();
                }
            }
        }
        response.setContentType("text/html;charset=UTF-8");
        if(accessTime != null) {
            response.getWriter().write("登陆成功,上次登陆时间为:" + accessTime);
        }else {
            response.getWriter().write("您是第一次登陆");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
Last modification:June 22nd, 2019 at 07:49 pm
If you think my article is useful to you, please feel free to appreciate

Comment here is closed