`
xxtianxiaxing
  • 浏览: 691012 次
  • 性别: Icon_minigender_1
  • 来自: 陕西
社区版块
存档分类
最新评论

CookieUtils 字符串常用工具

阅读更多

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

public class CookieUtils {

    /**
     * Returns the specified cookie, or <tt>null</tt> if the cookie
     * does not exist. Note: because of the way that cookies are implemented
     * it's possible for multiple cookies with the same name to exist (but with
     * different domain values). This method will return the first cookie that
     * has a name match.
     *
     * @param request the servlet request.
     * @param name the name of the cookie.
     * @return the Cookie object if it exists, otherwise <tt>null</tt>.
     */
    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie cookies[] = request.getCookies();
        // Return null if there are no cookies or the name is invalid.
        if (cookies == null || name == null || name.length() == 0) {
            return null;
        }
        // Otherwise, we  do a linear scan for the cookie.
        Cookie cookie = null;
        for (int i = 0; i < cookies.length; i++) {
            // If the current cookie name matches the one we're looking for, we've
            // found a matching cookie.
            if (cookies[i].getName().equals(name)) {
                cookie = cookies[i];
                // The best matching cookie will be the one that has the correct
                // domain name. If we've found the cookie with the correct domain name,
                // return it. Otherwise, we'll keep looking for a better match.
                if (request.getServerName().equals(cookie.getDomain())) {
                    break;
                }
            }
        }
        return cookie;
    }

    /**
     * Deletes the specified cookie.
     *
     * @param request the servlet request.
     * @param response the servlet response.
     * @param cookie the cookie object to be deleted.
     */
    public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, Cookie cookie){
        if (cookie != null) {
            // Invalidate the cookie
            String path = request.getContextPath() == null ? "/" : request.getContextPath();
            if ("".equals(path)) {
                path = "/";
            }
            cookie.setPath(path);
            cookie.setValue("");
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
    }

    /**
     * Stores a value in a cookie. By default this cookie will persist for 30 days.
     *
     * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String,int)
     * @param request the servlet request.
     * @param response the servlet response.
     * @param name a name to identify the cookie.
     * @param value the value to store in the cookie.
     */
    public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) {
        // Save the cookie value for 1 month
        setCookie(request, response, name, value, 60*60*24*30);
    }

    /**
     * Stores a value in a cookie. This cookie will persist for the amount
     * specified in the <tt>saveTime</tt> parameter.
     *
     * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String)
     * @param request the servlet request.
     * @param response the servlet response.
     * @param name a name to identify the cookie.
     * @param value the value to store in the cookie.
     * @param maxAge the time (in seconds) this cookie should live.
     */
    public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, int maxAge){
        // Check to make sure the new value is not null (appservers like Tomcat
        // 4 blow up if the value is null).
        if (value == null) {
            value = "";
        }
        String path = request.getContextPath() == null ? "/" : request.getContextPath();
        if ("".equals(path)) {
            path = "/";
        }
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAge);
        cookie.setPath(path);
        response.addCookie(cookie);
    }
}



分享到:
评论

相关推荐

    时间,字符串常用工具类

    字符串处理是编程中非常常见的任务,因此,一个字符串工具类会包含许多对字符串进行操作的方法。这可能包括字符串的格式化、分割、连接、查找和替换、去除空白字符、大小写转换、检查是否符合特定模式(如邮箱格式...

    Strings字符串分析工具

    《Strings字符串分析工具详解》 在计算机领域,字符串分析是一个重要的技术手段,尤其在病毒分析、逆向工程和软件调试中扮演着不可或缺的角色。Strings工具就是这样一个强大的实用程序,它能够从二进制文件中提取出...

    字符串拼接工具

    "字符串拼接工具"就是针对这类需求而设计的小型实用程序。它能够帮助开发者快速、有效地组合多个字符串,添加前缀或后缀,使得多行文本的格式化变得简单。 在编程中,字符串拼接通常是通过字符串连接运算符或者使用...

    字符串转换工具

    在IT领域,字符串转换工具是一种常见的实用程序,它允许用户在不同的编码系统之间转换文本数据。这个名为"字符串转换工具"的应用程序,正如其标题所示,专注于ASCII码、字符串以及十六进制之间的转换。这样的工具在...

    被爱可被爱以字符串处理工具 1.7.1

    除了常用的字符处理功能外,软件还带有简易利息计算工具、综合运算计算器、进制转换和编码转换的独立窗体界面功能,可对字符串做进一步操作处理。  推荐特色功能:综合运算计算器、利息计算器、WAP编码转换器、...

    Java 字符串常用方法

    以下是一些Java `String`类中常用的方法,这些方法对于理解和操作字符串至关重要。 1. **创建字符串** - `new String()`: 使用此构造函数创建一个新的字符串对象,可以传入字符数组或另一个字符串作为参数。 - `...

    被爱可以字符串处理工具注册机

    在IT行业中,字符串处理工具是程序员和数据分析师日常工作中不可或缺的一部分。这些工具帮助用户进行文本操作,例如查找、替换、格式化、加密和解密等。"被爱可以字符串处理工具"显然是一款专为此目的设计的软件,它...

    超好用的字符串查找工具

    在IT领域,字符串查找工具是一种极其实用的软件,它能够帮助用户快速、高效地在大量文本数据中定位特定的字符串。这种工具对于开发者、数据分析师、程序员以及任何需要处理大量文本信息的人来说,都是不可或缺的助手...

    字符串工具类

    字符串工具类,格式字符串、随机数生成、ip区间判断!

    批量字符串替换工具

    《批量字符串替换工具详解》 在日常的计算机操作中,我们常常需要对大量文本文件中的特定字符串进行查找和替换,以实现数据的统一或者格式的调整。这时,一款高效的批量字符串替换工具就显得尤为重要。本文将详细...

    rf.rar_RF 字符串截取_Rf字符串比较_rf字符串切割

    RF(Regular Expression,正则表达式)是一种强大的文本处理工具,广泛应用于字符串截取、比较和切割等任务。在这个"rf.rar"压缩包中,我们看到涉及到RF字符串处理的三个关键知识点:RF字符串截取、RF字符串比较以及...

    字符串过滤工具.

    字符串过滤工具.字符串过滤工具.字符串过滤工具.

    string字符串自动格式化单引号分隔工具

    "string字符串自动格式化单引号分隔工具"就是一个专为简化这一任务而设计的应用。它能够自动将一个字符串按照指定的单引号进行分隔,以便于在SQL的IN查询语句中更方便地使用。 首先,我们要理解什么是字符串。在...

    字符串查找工具

    字符串查找工具字符串查找工具

    MyReplace字符串替换工具

    《MyReplace字符串替换工具详解与应用》 在日常的编程工作和文本处理中,我们经常需要对字符串进行各种操作,其中替换字符是一项基础且频繁的任务。传统的字符串替换方法可能需要手动进行十六进制转换,这无疑增加...

    从源代码中抽取中文字符串的工具的源代码

    本资源提供了一个用于从源代码中抽取中文字符串的工具的源代码,这是一个非常实用的功能,尤其对于多语言软件的开发和维护来说。下面我们将深入探讨这个工具涉及到的相关知识点。 首先,"源代码"是程序的基础,它是...

    json转化工具,字符串格式化工具,方便又快捷

    本工具专注于JSON的转化和格式化,旨在提供一个方便快捷的方式来处理JSON字符串,便于开发者进行数据分析和问题排查。 1. JSON的结构与基本元素: JSON由对象(Object)和数组(Array)两种基本类型构成。对象由...

    16进制字符串显示图片工具

    "16进制字符串显示图片工具" 是一个专门用来将16进制字符串转换为可视图像的应用程序。这个工具基于C#(简称CS)语言构建,利用其丰富的库函数和面向对象的特性,实现了从16进制数据到图像的转换功能。C#中的System....

    c++ 加密字符串 工具 代码 工具一键生成

    C++作为一种强大的编程语言,提供了丰富的工具和库来处理各种安全需求,其中包括字符串的加密和解密。本篇文章将详细探讨C++中用于加密字符串的技术,以及如何利用提供的代码工具进行一键生成。 一、C++中的字符串...

Global site tag (gtag.js) - Google Analytics