Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create and retrieve Cookies in Grails 2.0?

Tags:

cookies

grails

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!

like image 303
coderberry Avatar asked Dec 15 '11 17:12

coderberry


2 Answers

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)
like image 127
Dónal Avatar answered Sep 16 '22 12:09

Dónal


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')
like image 34
Sergey Ponomarev Avatar answered Sep 20 '22 12:09

Sergey Ponomarev