zoukankan      html  css  js  c++  java
  • cookie

    package com.jyd.web;

    import java.util.HashMap;
    import java.util.Map;

    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class WebCookies {
    /**
    * 设置cookie
    *
    * @param response
    * @param name
    * cookie名字
    * @param value
    * cookie值
    * @param maxAge
    * cookie生命周期 以秒为单位
    */
    public static void addCookie(HttpServletResponse response, String name, String value, int maxAge ,String domain) {
    value = java.net.URLEncoder.encode(value);
    Cookie cookie = new Cookie(name, value);
    cookie.setPath(domain);
    if (maxAge > 0)
    cookie.setMaxAge(maxAge);
    response.addCookie(cookie);
    }

    public static void deleteCookie(String name, HttpServletResponse response) {
    Cookie cookie = new Cookie(name, null);
    cookie.setMaxAge(0);
    cookie.setPath("/");
    response.addCookie(cookie);
    }
    /**
    * 根据名字获取cookie
    *
    * @param request
    * @param name
    * cookie名字
    * @return
    */
    public static Cookie getCookieByName(HttpServletRequest request, String name) {
    Map<String, Cookie> cookieMap = readCookieMap(request);
    if (cookieMap.containsKey(name)) {
    Cookie cookie = (Cookie) cookieMap.get(name);
    return cookie;
    } else {
    return null;
    }
    }
    /**
    * 将cookie封装到Map里面
    *
    * @param request
    * @return
    */
    public static Map<String, Cookie> readCookieMap(HttpServletRequest request) {
    Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
    Cookie[] cookies = request.getCookies();
    if (null != cookies) {
    for (Cookie cookie : cookies) {
    cookieMap.put(cookie.getName(), cookie);
    }
    }
    return cookieMap;
    }

    /**
    * 将cookie的 name和value封装到Map里面
    * @Title: readCookieAndValueMap
    * @param request
    * @return
    * @date 2015-12-1 下午4:38:09
    * @version 1.0
    */
    public static Map<String, Object> readCookieAndValueMap(HttpServletRequest request) {
    Map<String, Object> cookieMap = new HashMap<String, Object>();
    Cookie[] cookies = request.getCookies();
    if (null != cookies) {
    for (Cookie cookie : cookies) {
    cookieMap.put(cookie.getName(), cookie.getValue());
    }
    }
    return cookieMap;
    }

    }

  • 相关阅读:
    CodeIgniter框架对数据库查询结果进行统计
    PHP的内存回收(GC)
    使用ajax请求后端程序时,关于目标程序路径问题
    JavaScript中的各种X,Y,Width,Height
    Qt编写气体安全管理系统7-设备监控
    Qt编写气体安全管理系统6-地图监控
    Qt编写气体安全管理系统5-数据监控
    Qt编写气体安全管理系统4-通信协议
    Qt编写气体安全管理系统3-用户模块
    Qt编写气体安全管理系统2-界面框架
  • 原文地址:https://www.cnblogs.com/jkwang/p/5710124.html
Copyright © 2011-2022 走看看