7

I can get cookie in JSP like this:

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 id="myCookie" class="javax.servlet.http.Cookie" scope="request" beanName="cookieName"/>
...
<div class="${myCookie.value == "true" ? "class1" : "class2"}"></div>

Thx for your answers!

Vitaliy Borisok
  • 822
  • 3
  • 11
  • 21

2 Answers2

24

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

${cookie['name']}
Pankaj Sharma
  • 1,833
  • 1
  • 17
  • 22
2

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

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

OR

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

OR

${cookie.<cookiename>.name}
${cookie.<cookiename>.value}
The Hunter
  • 155
  • 2
  • 8
Prabhu Anand
  • 530
  • 4
  • 10