操作 Cookie 的Java工具类
来源:互联网 发布:java get set方法解释 编辑:程序博客网 时间:2024/05/18 01:09
- /**
- * cn.itcast.bbs.util.CookieUtil
- * 2014-5-26
- * Cookie操作工具类
- * gyy
- */
- package cn.itcast.bbs.util;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class CookieUtil {
- private staticfinal int COOKIE_MAX_AGE =7 * 24 *3600;
- public staticvoid removeCookie(HttpServletRequest request,
- HttpServletResponse response, String name) {
- if (null == name) {
- return;
- }
- Cookie cookie = getCookie(request, name);
- if(null != cookie){
- cookie.setPath("/");
- cookie.setValue("");
- cookie.setMaxAge(0);
- response.addCookie(cookie);
- }
- }
- /**
- * 根据Cookie名称得到Cookie对象,不存在该对象则返回Null
- *
- * @param request
- * @param name
- * @return
- */
- public static Cookie getCookie(HttpServletRequest request, String name) {
- Cookie[] cookies = request.getCookies();
- if (null == cookies ||null == name || name.length() == 0) {
- return null;
- }
- Cookie cookie = null;
- for (Cookie c : cookies) {
- if (name.equals(c.getName())) {
- cookie = c;
- break;
- }
- }
- return cookie;
- }
- /**
- * 添加一条新的Cookie,默认7天过期时间(单位:秒)
- *
- * @param response
- * @param name
- * @param value
- */
- public staticvoid setCookie(HttpServletResponse response, String name,
- String value) {
- setCookie(response, name, value, COOKIE_MAX_AGE);
- }
- /**
- * 添加一条新的Cookie,可以指定过期时间(单位:秒)
- *
- * @param response
- * @param name
- * @param value
- * @param maxValue
- */
- public staticvoid setCookie(HttpServletResponse response, String name,
- String value, int maxValue) {
- if (null == name) {
- return;
- }
- if (null == value) {
- value = "";
- }
- Cookie cookie = new Cookie(name, value);
- cookie.setPath("/");
- if (maxValue != 0) {
- cookie.setMaxAge(maxValue);
- } else {
- cookie.setMaxAge(COOKIE_MAX_AGE);
- }
- response.addCookie(cookie);
- }
- }
0 0
- 操作 Cookie 的Java工具类
- 操作 Cookie 的Java工具类
- JAVA操作Cookie工具类
- 关于cookie的操作-----Cookie工具类
- 关于cookie的操作-----Cookie工具类
- Servlet操作Cookie制作工具类以及Cookie的生命周期
- Java cookie的操作
- 关于cookie 类的Java操作
- Java Cookie工具类代码
- 关于Java的Cookie操作
- 关于Java的Cookie操作
- 关于Java的Cookie操作
- java对cookie的操作
- 关于Java的Cookie操作
- java对cookie的操作
- java对cookie的操作
- Java对Cookie的操作
- java对cookie的操作
- iOS的设计备忘录
- LA 5031 - Graph and Queries
- 图像操作工具类
- AHU计科(伪)新生指南
- XML-RPC
- 操作 Cookie 的Java工具类
- cookie用法总结 cookie.setPath
- Web开发中常见的java.lang.IllegalStateException错误
- 【WIN32程序设计从零开始】2.MessageBox
- Android:android应用签名打包详解
- 堆排序算法
- STL之红黑树容器:set,hash_set,multiset,hash_map,multimap
- Struts2 整合jQuery实现Ajax功能
- 使用Signature Tool自动生成P/Invoke调用Windows API的C#函数声明