zoukankan      html  css  js  c++  java
  • 工具类_java 操作cookie

    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.net.URLEncoder;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class CookieUtil {
            private static String default_path
                 ="/";
            private static int default_age =365*24*3600;
        /**
         *@Function: 添加cookie 可設置時間
         *@Author: zzp
         *@param name
         *@param value
         *@param response
         *@param age
         *@throws UnsupportedEncodingException  void
         *@Date: 2014-2-19
         *@Modifications:
         *@Modifier Name; Date; The Reason for Modifying
         *
         */
            public static void addCookie(String name,String value,
                            HttpServletResponse response,int age) throws UnsupportedEncodingException{
                    Cookie cookie =
                        new Cookie(name,URLEncoder.encode(value,"utf-8"));
                    cookie.setMaxAge(age);
                    cookie.setPath(default_path);
                    response.addCookie(cookie);
            }
            /**
             *@Function: 添加cookie 默認時間
             *@Author: zzp
             *@param name
             *@param value
             *@param response
             *@throws UnsupportedEncodingException  void
             *@Date: 2014-2-19
             *@Modifications:
             *@Modifier Name; Date; The Reason for Modifying
             *
             */
            public static void addCookie(String name,String value,HttpServletResponse
                            response) throws UnsupportedEncodingException{
                    addCookie(name,value,response,default_age);
            }
            /**
             *@Function: 展示所有cookie
             *@Author: zzp
             *@param name
             *@param request
             *@return
             *@throws UnsupportedEncodingException  String
             *@Date: 2014-2-19
             *@Modifications:
             *@Modifier Name; Date; The Reason for Modifying
             *
             */
            public static String findCookie(String name,HttpServletRequest request)
                   throws UnsupportedEncodingException{
                    String value = null;
                    Cookie[] cookies = request.getCookies();
                    if(cookies!=null){
                            for(int i=0;i<cookies.length;i++){
                                    Cookie cookie = cookies[i];
                                    if(cookie.getName().equals(name)){
                                            value = URLDecoder.decode(cookie.getValue(), "utf-8");
                                    }
                            }
                    }
                    return value;
            }
            /**
             *@Function: 刪除cookie
             *@Author: zzp
             *@param name
             *@param response  void
             *@Date: 2014-2-19
             *@Modifications:
             *@Modifier Name; Date; The Reason for Modifying
             *
             */
            public static void deleteCookie(String name,HttpServletResponse
                            response){
                    Cookie cookie = new Cookie(name,"");
                    cookie.setMaxAge(0);
                    cookie.setPath(default_path);
                    response.addCookie(cookie);
            }
    }

  • 相关阅读:
    ApplicationContext之getBean方法详解
    Windows10终端优化方案:Ubuntu子系统+cmder+oh-my-zsh
    向 Windows 高级用户进阶,这 10 款效率工具帮你开路 | 新手问号
    Ditto —— windows 剪贴板增强小工具(复制粘贴多条记录)
    Service Mesh服务网格:是什么和为什么
    正确理解Spring事务和数据库事务和锁
    Spring中@Transactional事务回滚(含实例详细讲解,附源码)
    五分钟搞清楚MySQL事务隔离级别
    事务并发的问题场景图解
    Spring的事务管理和数据库事务相关知识
  • 原文地址:https://www.cnblogs.com/mxcy/p/3958059.html
Copyright © 2011-2022 走看看