Access the session, request, or response from a Struts 2 action class

To access the session, request, or response from within a Struts 2 action, you can use the ActionContext or implement the SessionAware, ServletRequestAware, or ServletResponseAware interfaces. Implementing the interfaces is preferred because it can make it easier to develop unit tests.

Following is an example action class that implements all three interfaces. Struts will handle setting the appropriate objects for you as long as the the interceptors are defined for the package in struts.xml where the action is defined. Note that these interceptors are already defined for struts-default.

package com.base22.struts2.actions;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

 * Abstract base action for Struts 2 Web applications.
 * <p>
 * As an abstract class, this action cannot be instantiated. Instead, it is
 * intended to be extended by custom actions. Because this class implements the
 * SessionAware, ServletRequestAware, and ServletResponseAware interfaces,
 * actions that extend this base will have access to the session Map, the
 * HttpServletRequest, and the HttpServletResponse. Actions extending this class 
 * will only have access to those objects if the interceptor stack for the package 
 * of the action includes the SessionAware, ServletRequestAware, and ServletResponseAware 
 * interceptors. The 'struts-default' package defines these interceptors in its 
 * stack by default. 
 * </p>
 * @author Cody Burleson
 * @version 1.0.0
 * @see
public abstract class WebAppBaseAction extends ActionSupport implements
		SessionAware, ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 1L;

	protected Map session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;

	public String execute() throws Exception {
		return doExecute();

	protected abstract String doExecute() throws Exception;

	public Map getSession() {
		return session;

	public void setSession(Map sess) {
		this.session = sess;

	public HttpServletRequest getServletRequest() {
		return request;

	public void setServletRequest(HttpServletRequest req){
		this.request = req;

	public HttpServletResponse getServletResponse() {
		return response;

	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;



If I did not name the method parameter variable distinctly different than the action's representative field name, my session object remained null. This was my faulty case:

public void setSession(Map session) {
    session = session;

Renaming the parameter variable fixed the issue as shown below:

public void setSession(Map sess) {
    session = sess;

You will likely want to put these in some abstract base action and extend it - rather than defining these things for every action class you create.

To unit test, you'll need to create and set mock objects. Take the following for example:

public void testGetSelectedOntModelKey() throws Exception {	
	Map session = new HashMap(1);
	session.put("selectedOntModelKey", SW.getURI());
	OntRegistryShow action = (OntRegistryShow) TestCaseUtil


In the example above, I create a new mock session map. I then put an object in it that I expect to be there. I'm using Spring, so I get a handle on my action class from Spring's application context. Once, I have the action, I call setSession() - passing in my mock session map.

Enterprise Solution Architecture
Cody Burleson is an Enterprise Web Architect, entrepreneur, and writer. He designs and builds solutions with his team at Base22 using IBM WebSphere Portal, IBM Web Content Manager, IBM Connections, and Java/J2EE. He is a tireless student of information technology who loves to invent things, improve things, and share what he learns. You can find more at his blog,