Cookie[] cookies = req.getcookies(); //获得cookie
Cookie.getName(); //获得cookie中的key
Cookie.getvalue(); //获得cookie中的vlaue
new Cookie("lastLoginTime",system.currentTimeMi11is()+""); //新建一个cookie
Cookie.setMaxAge(24*60*60); //设置cookie的有效期
resp.addcookie(cookie); //响应给客户端一个cookie
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.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//cookie,服务器端从客户端获取;
Cookie[] cookies = req.getCookies();//这里返回数组,说明cookie可能存在多个
// 判断cookie是否存在
if (cookies !=null){
//如果存在怎么办
out.write("你上一次访问的时间是:");
for (int i=0;i<cookies.length;i++){
Cookie cookie = cookies[i];
//获取cookie的名字
if (cookie.getName().equals("lastLogintime")){
//获取cookie中的值
long lastLogintime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLogintime);
out.write(date.toLocaleString());
}
}
}else {
out.write("这是您第一次访问本站");
}
//服务给客户端响应一个cookie;
Cookie cookie = new Cookie("lastLogintime",System.currentTimeMillis()+"");
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}