I am trying to create a cookie using Grails 2 (RC3). I am using this for a Facebook canvas app, which means that on every browser refresh, the session is lost.
I have tried using the cookies plugin, but it seems that it's not compatible with Grails 2.
Any help would be GREATLY appreciated!
You can retrieve the value of a cookie in a GSP using the <g:cookie>
tag
Hello <g:cookie name="myCookie" />
You can also use this tag from a controller:
String name = g.cookie(name: 'myCookie')
You can set a cookie using the Servlet API
Cookie cookie = new Cookie("myCookie","Cookie Monster")
cookie.maxAge = 100
response.addCookie(cookie)
You can use Cookie Plugin:
// Inject service
def cookieService
...
// This sets a cookie with the name `username` to the value `admin` with a expiration set to a week, defined in seconds
cookieService.setCookie('username', 'admin', 7 * 24 * 60)
cookieService.getCookie('username') // returns 'admin'
cookieService.deleteCookie('username')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With