Edit on GitHub << back to Core Developers Guide

# Accessing application, session, request objects

The framework provides several access helpers to access Request, Session, Application scopes. See Servlet Config Interceptor page to find all the supported interfaces.

## Accessing from Java

The best way to access Request, Session or Application scope is to use one of the following interfaces:

- `ServletRequestAware` - to access Request scope
- `ServletResponseAware` - to access Response scope
- `SessionAware` - to access Session scope
- `ApplicationAware` - to access Application scope

Example usage of the interfaces:

    public class MyAction implements ApplicationAware {

        private Map<String, Object> application;

        public void withApplication(Map<String, Object> application) {
            this.application = application;
        }

        public String execute() {
            application.set("myKey", "myValue");
            ...
            return "success";
        }

    }

Implementing `ServletRequestAware` or `ServletResponseAware` will tie your actions to Servlet objects. Yet using these interfaces and `SessionAware` or `ApplicationAware` combined with the `servletConfig` interceptor, is the best way to access these scopes.

### Avoid using ActionContext

Using `ActionContext` directly is a bad practice and should be avoided, instead of using

    ActionContext.getContext().getSession().put("myAttribute", "myValue");

use one of the `*Aware` interfaces above.

## Accessing from the view (JSP, FreeMarker, etc.)

Request and session attributes are accessed via OGNL using the `#session` and `#request` stack values. Page attributes are accessed via OGNL using the `#attr` stack value, and Application attributes via the `#application` stack value.

The `#attr` stack value will search the `javax.servlet.jsp.PageContext` for the specified key. If the `PageContext` doesn’t exist, it will search the request, session, and application scopes, in that order.

Accessing attributes in the Application, Session, Request, or Page scope from a JSP

    <p>Retrieve the attribute (property), with key myId, from the specified scope:</p>

    <s:property value="#application.myId" />

    <s:property value="#session.myId" />

    <s:property value="#request.myId" />

    <s:property value="#attr.myId" />

    <p>Reminder: #attr is for Page scope attributes first, but will search the remaining scopes, in order, seeking a match.</p>