码迷,mamicode.com
首页 > 其他好文 > 详细

会话-cookie

时间:2019-09-03 00:01:42      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:int   doget   dem   https   font   发送   res   消失   bbb   

package com.hopetesting.cookie;

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

/**
* @author newcityman
* @date 2019/9/2 - 21:35
*/
@WebServlet("/cookieDemo01")
public class CookieDemo04 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、创建cookie对象
Cookie c1 = new Cookie("msg", "hellocookie");
//2、设置cookie的存活时间
c1.setMaxAge(30);//持久化到硬盘,30秒后自动删除
        c1.setPath("/");  //设置跨项目访问cookie
//        c1.setMaxAge(-10);   //默认把数据存放到浏览器内存中,浏览器关闭后,cookie消失
//        c1.setMaxAge(0);   //删除cookie

//3、发送cookie
response.addCookie(c1);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}


package com.hopetesting.cookie;

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

/**
* @author newcityman
* @date 2019/9/2 - 21:36
*/
@WebServlet("/cookieDemo02")
public class CookieDemo02 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println(name + "==" + value);
}
}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}


会话-cookie

标签:int   doget   dem   https   font   发送   res   消失   bbb   

原文地址:https://www.cnblogs.com/newcityboy/p/11449490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!