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)

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.

View Comments

  • 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.

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

5 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

5 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

5 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

5 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

5 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago