Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting cookie in JSP

Tags:

java

jsp

cookies

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!

like image 221
Vitaliy Borisok Avatar asked Oct 01 '13 12:10

Vitaliy Borisok


2 Answers

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

${cookie['name']}
like image 159
Pankaj Sharma Avatar answered Oct 01 '22 22:10

Pankaj Sharma


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}
like image 33
Prabhu Anand Avatar answered Oct 01 '22 22:10

Prabhu Anand