Setting cookie attributes (expiration/domain) in page init
I'm able to create a new cookie on page init with
${http.response.addCookie(http.request.createCookie("name", "value"))}
while this does NOT work (but is also stated in the docs, slightly confusing =D):
${http.response.setCookie(http.request.createCookie("name","value"))}
SuzieH see section "http.request.createCookie("name", "value")" and there the example (which is the above code), this does not work, it needs to be addCookie(), not setCookie().
The cookie that is created has some attributes, mainly an expiration date and a domain to which it is scoped, I could not find anything in the docs or community that would tell me how to configure the cookie's attributes, SuzieH could you dig into your internal docs (or dev brains) and see if that is even possible? E.g. something like:
http.request.createCookie("name","value").setExpiration(<maxage in seconds>).setDomain('*.somedomain.tld')
I did try to use the methods for a java servlet httpCookie from: http://docs.oracle.com/javaee/6/api/javax/servlet/http/Cookie.html but with no success, the cookie is not created anymore (can't debug it and do not get any errors because page init...) when I do things like:
<#assign cookie = http.request.createCookie("name", "value") /> <#assign cookie = cookie.setMaxAge(3600) /> ${http.response.addCookie(cookie)} // note, it does not make a difference if everything is first assigned to a variable or not...e.g. this doesn't work either =): ${http.response.addCookie(http.request.createCookie("name", "value").setMaxAge(3600))}
// tried .setHttpOnly(<boolean>) and .setValue() as well, same result: no cookie is created anymore
thanks for clarifying!
I was close =)... setting the cookie attributes seems to only work when directly interpolating the variable... that means it has to be written in this way:
// THIS WILL NOT WORK <#assign cookie = http.request.createCookie("name", "value") /> <#assign cookie = cookie.setValue("some manually set value") /> <#assign cookie = cookie.setHttpOnly(true) /> // ... etc ${http.response.addCookie(cookie)} // WHILE THIS WILL WORK <#assign cookie = http.request.createCookie("name", "value") /> ${cookie.setMaxAge(24*60*60)} // 24 hours ${cookie.setDomain(".domain.tld")} ${cookie.setPath("/")} ${cookie.setValue("some manually set value")} ${cookie.setHttpOnly(true)} ${cookie.setSecure(true)} ${cookie.setComment("Describes what this cookie does")} ${http.response.addCookie(cookie)}
SuzieH I think that should be added to the docs, might save somebody a few hours of research in the future =)