## 6.2.2.1 Variables and Scopes

Variables can be defined within a GSP using the set tag:

    <g:set var="now" value="${new Date()}" />

Here we assign a variable called `now` to the result of a GSP expression (which simply constructs a new `java.util.Date` instance). You can also use the body of the `<g:set>` tag to define a variable:

    <g:set var="myHTML">
       Some re-usable code on: ${new Date()}
    </g:set>

Variables can also be placed in one of the following scopes:

- `page` - Scoped to the current page (default)
- `request` - Scoped to the current request
- `flash` - Placed within flash scope and hence available for the next request
- `session` - Scoped for the user session
- `application` - Application-wide scope.

To select which scope a variable is placed into use the `scope` attribute:

    <g:set var="now" value="${new Date()}" scope="request" />