java servlet操作cookie
一些细节
一个Cookie只能保存一个信息;
一个web站点可以给浏览器发送多个cookie,最多存放20个cookie;
Cookie大小有限制4kb;
300个cookie浏览器上限
删除Cookie;
不设置有效期,关闭浏览器,自动失效;
设置有效期时间为 0 ;
核心方法
Cookie[] cookies = req.getCookies(); //获得Cookie
cookie.getName(); //获得cookie中的key
cookie.getValue(); //获得cookie中的vlaue
new Cookie("lastLoginTime", System.currentTimeMillis()+""); //新建一个cookie
cookie.setMaxAge(24*60*60); //设置cookie的有效期
resp.addCookie(cookie); //响应给客户端一个cookie
Demo01
package com.myweb;
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
public class CookieDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求和响应的编码方式
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
//从请求中拿到一个cookie并输出到页面
PrintWriter out = resp.getWriter();
//1.获得cookie
Cookie[] cookies = req.getCookies();
//判断cookie是否为空(由于获取的是数组,遍历前进行非空判断。否则容易500)
if (cookies != null)
{
//遍历cookie
for (int i = 0; i <cookies.length ; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("lastLoginTime"))//equals比较字符串 ==
{
long lastLoginTime= Long.parseLong(cookie.getValue()) ;//字符串转Long
Date date = new Date(lastLoginTime);//Long转date对象
out.println("您上次登陆的时间是"+date.toLocaleString()); //data 对象转日期格式字符串
}
}
}
//更新一个cookie
Cookie lastLoginTime = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
//设置cookie有效期30天
lastLoginTime.setMaxAge(30*24*60*60);
resp.addCookie(lastLoginTime);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Demo02
package com.myweb;
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
public class CookieDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求和响应的编码方式
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
//从请求中拿到一个cookie并输出到页面
PrintWriter out = resp.getWriter();
//1.获得cookie
Cookie[] cookies = req.getCookies();
//判断cookie是否为空(由于获取的是数组,遍历前进行非空判断。否则容易500)
if (cookies != null)
{
//遍历cookie
for (int i = 0; i <cookies.length ; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("username"))//equals比较字符串 ==
{
out.println("欢迎回来"+cookie.getValue());
}
}
}
//更新一个cookie
Cookie username = new Cookie("username", "王大力");
//设置cookie有效期30天
username.setMaxAge(30*24*60*60);
resp.addCookie(username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}