java-Cookie的操作
程序员文章站
2024-03-20 14:16:58
...
1.Cookie的操作
/*
1)服务器创建cookie对象,把会话存储到cookie对象中
2)服务器发送cookie信息到浏览器(隐藏发送一个set-cookie名称的相应头)
Set-Cookie:name1=eric1
3)浏览器得到服务器发送的cookie,然后保存在浏览器端
4)浏览器在下次访问服务器时,会带着cookie信息(隐藏发送一个cookie名称的请求头)
5)服务器接收浏览器带来的cookie信息
request.getCookies()
*/
@SuppressWarnings("serial")
public class demo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("<title>Cookie</title>");
out.print("</head>");
out.print("<body>");
//1.创建Cookie
Cookie cookie1 = new Cookie("name1", "eric1");
Cookie cookie2 = new Cookie("name2", "eric2");
/*
4.设置cookie的有效路径
cookie2.setPath("/day11");
*/
/*
5.设置Cookie的有效时间
正整数:表示cookie数据保存浏览器的缓存目录(硬盘中),数值表示保存时间
负整数:表示cookie数据保存浏览器的内存中,浏览器关闭cookie就丢失
零:表示删除同名的cookie数据
*/
cookie1.setMaxAge(1*24*60*60*30); //一个月
//2.把cookie数据发送到浏览器(通过响应头发送:set-cookie名称)
//response.setHeader("set-cookie", cookie1.getName()+"="+cookie1.getValue());
//推荐使用这种方法,避免手动发送cookie信息
response.addCookie(cookie1);
response.addCookie(cookie2);
//3.接收浏览器发送的Cookie
/*String cookie = request.getHeader("cookie");
System.out.println(cookie);*/
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie c : cookies){
String name = c.getName();
String value = c.getValue();
out.print("<html><body>"+name+"="+value+"<br></body></html>");
}
}
out.print("</body>");
out.print("</html>");
}
}
2.用户上次访问的时间
@SuppressWarnings("serial")
public class demo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
//获取当前时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String curTime = format.format(new Date());
//1.第一次访问(没有cookie或有cookie,但没有名为lastTime的Cookie)
Cookie[] cookies = request.getCookies();
String lastTime = null;
if(cookies!=null){
for(Cookie cookie:cookies){
if(cookie.getName().equals("lastTime")){
//有lastTime的cookie,已经是第n次访问
lastTime = cookie.getValue();
//1.第n次访问,把上次显示时间显示到浏览器
out.print("欢迎回来,你上次访问的的时间为:"+lastTime+",<br>当前时间为:"+curTime);
//2.更新cookie
cookie.setValue(curTime);
cookie.setMaxAge(1*30*24*60*60);
//3.更新后的cookie发送到浏览器
response.addCookie(cookie);
break;
}
}
}
if(cookies==null||lastTime==null){
//1.显示当前时间
out.print("你是首次访问本网站,当前时间为:"+curTime);
//2.创建Cookie对象
Cookie cookie = new Cookie("lastTime", curTime);
cookie.setMaxAge(1*30*24*60*60); //保存一个月
//3.把cookie发送到浏览器保存
response.addCookie(cookie);
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
3.删除cookie
public class DeleteCookie extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie cookie = new Cookie("name","xxxx");
cookie.setMaxAge(0);
response.addCookie(cookie);
System.out.println("删除成功");
}
}