集册 Java实例教程 从HttpServletRequest获取Cookie

从HttpServletRequest获取Cookie

欢马劈雪     最近更新时间:2020-01-02 10:19:05

940
从HttpServletRequest获取Cookie


import java.io.UnsupportedEncodingException;

import java.text.SimpleDateFormat;//来自 nowjava - 时代Java

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public class Main{

    /**

     * Method getCookie.

     *

     * @param request

     * @param cookieName

     * @return String

     * @throws Exception

     */

    public static String getCookie(HttpServletRequest request,

            String cookieName) {

        Cookie[] cookies = request.getCookies();

        if (cookies == null) {

            return "";

        }

        String value = "";

        for (int i = 0; i < cookies.length; i++) {

            if (cookieName.equals(cookies[i].getName())) {/** from NowJava.com - 时代Java**/

                try {

                    value = java.net.URLDecoder.decode(

                            cookies[i].getValue(), "utf-8");

                }
展开阅读全文