Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > Accessing application, session, request objects |

Apache Struts 2 Documentation
Accessing application, session, request objects
Edit Page Browse Space Add Page Add News
Added by casey, last edited by Dave Newton on Feb 04, 2007 (view change)

The framework provides several access helpers to access Session, Application, Request scopes.
Web agnostic (independent of the servlet API) with a single line of code.

Access session via a context entry

    Map session = (Map) ActionContext.getContext().get("session");
    session.put("myId",myProp);

Access session via a helper method (avoids cast)

    ServletActionContext.getRequest().getSession()

| Do not use `ActionContext.getContext()` in the constructor of your Action class. The values may not be set up, and the call may return null for `getSession()`.

If you must have get access to the HttpSession, use the ServletConfigInterceptor (see test:Interceptors).

In your views, you can access with your JavaServer Pages with calls to the implicit properties `session` and `request`.

Accessing the Session or Request from a JSP

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

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

All the servlet scopes can be accessed via the ActionContext.

Accessing servlet scopes

    Map request = (Map) ActionContext.getContext().get("request");
    request.put("myId",myProp);

    Map application = (Map) ActionContext.getContext().get("application");
    application.put("myId",myProp);

    Map session = (Map) ActionContext.getContext().get("session");
    session.put("myId", myProp);

    Map attr = (Map) ActionContext.getContext().get("attr");
    attr.put("myId",myProp);

The `attr` map will search the `javax.servlet.jsp.PageContext` for the specified key. If the PageContext doean't exist, it will search `request}, {{session`, and `application` respectively.