Java 在 JSP 中获取 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19116040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:15:52  来源:igfitidea点击:

Getting cookie in JSP

javajspcookies

提问by Vitaliy Borisok

I can get cookie in JSP like this:

我可以像这样在 JSP 中获取 cookie:

Cookie[] cookies = request.getCookies();
if (cookies != null) {
    for (Cookie cookie : cookies) {
        //work with cookies
    }
}

But I wonder if I can get it with jsp:useBean (or something else)? May be like this:

但是我想知道我是否可以通过 jsp:useBean (或其他东西)获得它?可能是这样的:

<jsp:useBean id="myCookie" class="javax.servlet.http.Cookie" scope="request" beanName="cookieName"/>
...
<div class="${myCookie.value == "true" ? "class1" : "class2"}"></div>

Thx for your answers!

谢谢你的回答!

采纳答案by Pankaj Sharma

use jsp expression language it has implicit map of cookies. may be it can resolve your issue.

使用jsp表达式语言它具有cookie的隐式映射。也许它可以解决您的问题。

${cookie['name']}

回答by Prabhu Anand

hope this one could help you to get name and value of your cookie

希望这个可以帮助您获得 cookie 的名称和价值

${cookie['cookiename'].getName()}
${cookie['cookiename'].getValue()}

OR

或者

${cookie.cookiename.getName()}
${cookie.cookiename.getValue()}

OR

或者

${cookie.<cookiename>.name}
${cookie.<cookiename>.value}