How to Access Cookies in JSP Expression Language

Below snippet is just for your reference. We can print a cookie value on JSP page using JSP Expression language.

The standard syntax to access Http Cookie value in JSP is:

${cookie.<cookie name>.value}
Code language: JavaScript (javascript)

So if you want to print value of cookie named “foo” on JSP page, you might wanna write something like:

${cookie.foo.value}
Code language: JavaScript (javascript)

In previous tutorial How to access Cookies in Spring MVC, I used JSP expression language to print value of hitCounter cookie.

Display List of All Cookies in JSP

In JSP expression language ${cookie} gives a list of all cookies set for current webpage. This list can be iterated using JSTL <c:forEach> to print each cookie. Here is a small JSTL code snippet that prints list of all cookies for current page.

<h3>List of all the available Cookies</h3> <ul> <c:forEach var="cookies" items="${cookie}"> <li> <c:out value="${cookies.key}" />: Object= <c:out value="${cookies.value}" />, value= <c:out value="${cookies.value.value}" /> </li> </c:forEach> </ul>
Code language: HTML, XML (xml)
http-cookies-jsp-expression-language

Once you run this code, you’ll see list of all http cookies for given page as below.

Hope you’ll remember this little trick next time you want something like this.

Get our Articles via Email. Enter your email address.

You may also like...

1 Comment

  1. Dinesh Krishnan says:

    Its good thanks for sharing.. Good work keep up..

    is there any shortcut keyword to generate to setters and getters in eclipse? Thank you in Advance.

Leave a Reply

Your email address will not be published. Required fields are marked *