Cookie Plug-in
The cookie plug-in extends the request and response objects found in controllers and filters by adding methods to easily set, get, and delete cookies. It also provides access to these methods via a service, CookieService.
Installation
To install the cookie plug-in just run the following command
grails install-plugin cookie
Usage
The cookie plug-in extends the request and response objects found in controllers, filters, etc to allow the following:
Example of setting a new cookie: (this sets a cookie with the name "username" to the value "cookieUser123" with a expiration set to a week, defined in seconds)
response.setCookie("username","cookieUser123",604800)To get the cookie value:
request.getCookie("username") // returns "cookieUser123"To delete the cookie:
response.deleteCookie("username") // deletes the "username" cookieUsage by calling CookieService directly
The cookie plug-in provides a CookieService that can be used anywhere in your Grails application.
To use CookieService, declare a dependency injection:
Setting a new cookie:
cookieService.set(response,"username","cookieUser123",604800)
Getting the cookie value:
cookieService.get("username") // returns "cookieUser123"Deleting the cookie:
cookieService.delete(response,"username") // deletes the "username" cookie
Tag Lib
You can also get a cookie value with a tag:
<cookie:get name='username' />
Plugin version history
*2009-08-010 0.1
Added methods to request and response objects to get, set, and delete cookies
*2009-08-07 0.1
Initial release
TODO
- Investigate if there's a way to do the set and delete methods from CookieService without having to pass in the response object