在JSP中获取cookie

时间:2013-10-01 12:21:28

标签: java jsp cookies

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

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

但是我想知道我是否可以使用jsp:useBean(或其他东西)来获取它? 可能是这样的:

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

谢谢你的回答!

2 个答案:

答案 0 :(得分:18)

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

${cookie['name']}

答案 1 :(得分:0)

希望这可以帮助您获取Cookie的名称和价值

amtArray = [2000, 500, 100];  // the denomination you want to find.

 for (let i = 0; i < this.amtArray.length; i++) {
            this.resultArray.push(Math.floor(total / this.amtArray[i]));
            // Get the new total
            total = total % this.amtArray[i];
        }
        var twothousands_notes = this.resultArray[0];
        var fivehundred_notes = this.resultArray[1];
        var hundred_notes = this.resultArray[2];

        console.log('calculated amt : ' + '100 : ' +
            hundred_notes + '   500 :  ' +
            fivehundred_notes + '  2000 :  ' +
            twothousands_notes);

OR

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

OR

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