How do I set and get cookies in Grails?
I am trying to use cookies in a Grails application with no success. Specifically, in the show () method of the test case controller, I want to set a cookie as the last test suite shown; that is, the params.id value available to the show () method. Then, in the list () method, I want to provide a shortcut to show the most recent test suite (the value I'm trying to set using a cookie).
However, the cookie I have set (named "tcCookie") only shows up in the show () method, not the list () method. Here's the code:
in show ():
request.cookies.each { println "show: cookie ${it.name} <${it.value}>" }
in list ():
request.cookies.each { println "list: cookie ${it.name} <${it.value}>" }
I set a cookie in the show () method:
Cookie cookie = new Cookie( "tcCookie", params.id )
cookie.maxAge = 315360000000L
response.addCookie( cookie )
When I step back and forth between the list and display the views (after show () with params.id = 277), I get:
show: cookie tcCookie <277>
show: cookie JSESSIONID <9DEBFB40F78B5E24A92C750157342069>
list: cookie JSESSIONID <9DEBFB40F78B5E24A92C750157342069>
show: cookie tcCookie <277>
show: cookie JSESSIONID <9DEBFB40F78B5E24A92C750157342069>
list: cookie JSESSIONID <9DEBFB40F78B5E24A92C750157342069>
which I think indicates that the "tcCookie" cookie is only displayed in the show () method, which is useless to me. I need to set it up so that it displays in other ways.
In addition, the tcCookie cookie survives multiple sessions - as you would expect, but still only visible in the show () method.
Perhaps my understanding of cookies is wrong, but I'm still stuck. Any help would be greatly appreciated. Also, there must be hundreds of other cookies in the browser, none of which are displayed using the code I used, so is there a way to get to the other cookies?
source to share
The problem is a side effect of setting a cookie without setting the path explicitly. Assuming the name of the Grails application is "application", it will be referenced by
http://hostname.com/app
This will cause the JSESSIONID cookie to be set /app/JSESSIONID
to appear anywhere in the application. Since the application name is available in the application as grailsApplication.metadata['app.name']
, the actual value determination app
can be done automatically.
However, in a separate view, such as a view list
in an object xxx_domain
, which is accessed as
http://hostname.com/app/xxx_domain/list
setting cookie with code
def newCookie = new Cookie( "myCookie", "cookieValue" )
response.addCookie newCookie
will set the path from newCookie
to /app/xxx_domain/list/
where it will only be visible in the method list()
and in the view. For the cookie to be visible anywhere in the application, the path must be set as shown in the above answer, but not in /
, not in /app
, as in
def newCookie = new Cookie( "myCookie", "cookieValue" )
newCookie.path = "/${grailsApplication.metadata['app.name']}/"
response.addCookie newCookie
source to share
You should try using this plugin: Cookies plugin
Documentation can be found here
The following code shows how to set a cookie:
cookieService.setCookie('username', 'cookieUser123')
And here's how to get the value inside your cookie:
cookieService.get('username')
Hope it helps
EDIT
For your last question, I think you can only access cookies from your domain.
source to share