Accessing application, session, request objects

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

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

Accessing from Java

All the JEE scope attribute maps can be accessed via ActionContext.

Code Block
titleAccess session via a context entryAccessing servlet scopes
Map sessionattr = (Map) ActionContext.getContext().get("sessionattr");
sessionattr.put("myId",myProp);
Code Block
titleAccess session via a helper method (avoids cast)
ServletActionContext.getRequest().getSession()
Warning

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 Interceptors).

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

Code Block
titleAccessing the Session or Request from a JSP

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

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

All the servlet scopes can be accessed via the ActionContext.

Code Block
titleAccessing servlet scopes

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

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

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

Map attr = (Map) 
Warning

Do not use ActionContext.getContext()

.get("attr"); attr.

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

We can also access the HttpServletRequest and HttpServletResponse objects themselves through ServletActionContext. In general this isn't recommended as it will tie our action to the servlet specification.

Code Block
titleSetting session attribute through session object

ServletActionContext.getRequest().getSession().put("myId", myProp);

The attr map Implementing ServletRequestAware or ServletResponseAware, combined with the "servletConfig" interceptor, is an alternative way to access the request and response objects, with the same caveat.

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

Request and session attributes are accessed via OGNL using the #session and #request stack values.

The #attr stack value will search the javax.servlet.jsp.PageContext for the specified key. If the PageContext doean't exist, it will search the request}, {{ session, and application respectively.scopes, in that order.

Code Block
titleAccessing the Session or Request from a JSP

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

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

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