zoukankan      html  css  js  c++  java
  • cookie的使用

    1. 添加cookie

    public static boolean addCookie(String str, HttpServletResponse response) {
            String name = null;
            try {
                name = URLEncoder.encode(str, "utf-8"); // 转码,解决中文保存的问题
            } catch (Exception e) {
                e.printStackTrace();
            }
            Cookie cookie = new Cookie("user", name);
            cookie.setMaxAge(60 * 60 * 24 * 14);// cookie保存两周
            response.addCookie(cookie);
            System.out.println("添加cookie成功");
            return true;
        }

    2. 取得cookie

    public static String getCookie(HttpServletRequest request) {
            String name;
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    System.out.println("cookie: " + cookie.getName());
                    if ("user".equals(cookie.getName())) {
                        name = cookie.getValue();
                        return name;
                    }
                }
            }
            return null;
        }
  • 相关阅读:
    自定义转化
    asp.net JSON(一)
    做一个会偷懒的码农
    活动和监视器
    linq 分组求和
    sql语句查询列的说明
    chartControl
    LayOutControl
    sql 给表结构增加说明
    我的单件模式
  • 原文地址:https://www.cnblogs.com/ljmin/p/2588939.html
Copyright © 2011-2022 走看看