Check if Cookie exists with JSP EL
Asked Answered
C

4

8

I am trying to check if a cookie exists on a JSP page using the Expression Language.

I have a cookie called persist which is set to either empty string or "checked".

If would like to check if the persist cookie exists.

I have tried the following:

<c:if test="${cookie.persist == null}">

<c:if test="${empty cookie.persist}">

Both of the above statements are true when the value of the persist cookie is the empty string and false when the value of the cookie is checked.

How do I distinguish between a cookie with the empty string as its value, and a cookie that does not exist.

(Note: I can easily work around this problem by assigning a non empty value to the cookie instead of the empty string.)

Cimabue answered 12/9, 2011 at 23:24 Comment(0)
I
8

Closest what you can get is to check the cookie name in the request cookie header.

<c:if test="${fn:contains(header.cookie, 'persist=')}">

However, when there's another cookie with name foopersist, it fails.

If your container supports EL 2.2 (all Servlet 3.0 containers like Tomcat 7, Glassfish 3, etc do) then you could just use Map#containsKey().

<c:if test="${cookie.containsKey('persist')}">

If yours doesn't, best what you can do is to create an EL function (more concrete declaration example can be found somewhere near bottom of this answer):

<c:if test="${util:mapContainsKey(cookie, 'persist')}">

with

public static boolean mapContainsKey(Map<String, Object> map, String key) {
    return map.containsKey(key);
}
Intertype answered 13/9, 2011 at 15:23 Comment(2)
Correct me if I am wrong, but the 2nd approach did not seem to work for me. I got the same results as using <c:if test="${empty cookie.persist}">Cimabue
The first approach works fine. My cookies have an application prefix on them so I should be ok.Cimabue
A
0

If I understand correctly, you want to detect that it either doesn't exist or is empty.

EDIT: ah. To verify that it doesn't exist, it must be null and not empty.

    <c:if test="${cookie.persist == null && cookie.persist != ''}">
   Cookie doesn't exist
    </c:if>
Attached answered 12/9, 2011 at 23:33 Comment(2)
I am just trying to check that the cookie does NOT exist.Cimabue
When I use ${cookie.persist == null} it returns true if the cookie exists AND is set to '' (empty string)Cimabue
P
0

use the cookie map to check that cookie exists or not ${cookie["persist"] == null}

I hope it works

Pterosaur answered 12/9, 2011 at 23:37 Comment(1)
Sorry did not work. ${cookie["persist"] == null} gave the same result as ${cookie.persist == null}Cimabue
V
0

if using Tomcat 6+

<c:if test="${ ! empty cookie['persist']}"> 
Cookie doesn't exist
</c:if>
Vulgate answered 15/5, 2012 at 17:21 Comment(1)
This expression doesn't check if there is a cookie, but if there is a cookie value. OP's concrete problem is that it would also pass when the cookie exists but contains an empty value. Please re-read the question and all the answers.Intertype

© 2022 - 2024 — McMap. All rights reserved.