How to create a cookie without quotes around the value?

I need to create a cookie with an email address as a value - but when I try - then I have the result:

" someone@example.com "

but I would like to:

someone@example.com

A cookie must be created without double quotes - because another application uses it in this format. How to force java not to add double quotes? Java adds them because there is a special char "at".

I create a cookie this way:

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); Cookie cookie = new Cookie("login", " someone@example.com "); cookie.setMaxAge(2592000); cookie.setDomain("domain.com"); cookie.setVersion(1); response.addCookie(cookie); 

Thanks for any help.

+4
source share
1 answer

This is really caused by the @ sign. This is not allowed in version 0 cookies. The container will implicitly force it to become version 1 cookie (which breaks in MSIE browsers). Do you want the URL encode cookie value when creating the cookie

 Cookie cookie = new Cookie("login", URLEncoder.encode(" someone@example.com ", "UTF-8")); cookie.setMaxAge(2592000); cookie.setDomain("domain.com"); response.addCookie(cookie); 

and URL decoding on cookie readings

 String value = URLDecoder.decode(cookie.getValue(), "UTF-8"); 

Please note that you probably should not explicitly set the cookie version to 1.

See also:


Unrelated to a specific problem, cookies are visible and manipulated by the end user or person-in-the-middle. Transferring an email address in a cookie is a bad smell. What if the end user changes it to a different address? Whatever functional requirement (remembering the login?) That you decide to solve with the transfer of the email address into the cookie will most likely be solved differently.

See also:

+11
source

All Articles