Java 怎么从httpservletrequest获取cookie

2019年8月15日22:58:08Java 怎么从httpservletrequest获取cookie已关闭评论 222 439字阅读1分27秒

1.简介

Java 怎么从httpservletrequest获取cookie?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

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

public class Main{
    public static Cookie get(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(cookieName)) {
                    return cookie;
                }
            }
        }
        return null;
    }
}

 
编程十万个怎么办