How to get Servlet Session, Request, Response, Context Attributes in Struts 2 Action With Examples

Struts 2 Action classes doesn’t provide any methods to get Servlet API Request, Response, Session and attributes. But sometimes we need to access these in action classes, for example checking HTTP method or to work with session attributes or to set cookies or headers in response.

That’s why Struts 2 API provides a bunch of interfaces that we can implement in action classes to inject these objects in Action classes. All of these interfaces end with “Aware” and defined in org.apache.struts2.interceptor package.

All of these interfaces declares setter methods through which Struts 2 API injects Servlet API components in action classes. It’s a great example of Dependency Injection in Java EE frameworks.

These *Aware interfaces are:

  1. SessionAware: This interface provides access to session attributes in action classes and declare a single method setSession(Map<String, Object> sessionAttributes). Note that we can’t get HttpSession by implementing this interface, this is just to get access to the session attributes.
  2. ApplicationAware: This interface provides access to context attributes in action classes as Map. We can add attributes to application context by putting values in the context map. This interface declares single method setApplication(Map<String, Object> applicationAttributes).
  3. RequestAware: This interface provides access to request attributes in action classes, it contains single method setRequest(Map<String, Object> requestAttr). This interface is only applicable if Action classes are used in Servlet environment. Since this interface makes the Action tied to a servlet environment, so it should be avoided if possible since things like unit testing will become more difficult.
  4. ServletRequestAware: We can implement this interface in action classes to get access to HttpServletRequest object. This interface is only relevant if the Action is used in a servlet environment. It defines a single method as setServletRequest(HttpServletRequest request).
  5. ServletResponseAware: Struts 2 action classes can implement this interface to get access to the HttpServletResponse object. We can then use response object to add headers or cookies. It defines a single method as setServletResponse(HttpServletResponse response).
  6. CookiesAware: This interface is intended to provide access to cookies in request in the form of Map. It contains single method setCookiesMap(Map<String,String> cookies). To work with this interface, there are two interceptors defined in struts-default package as:

    But they are not part of defaultStack interceptors stack, so we need to include them for action class like below.

    However these interceptors are very new and in my testing with Struts 2 version 2.3.15.1, I didn’t find it to be working. I looked into Struts 2 API docs but didn’t find any help on this. I will update the post if I find anything or if you know what is missing, please let us know through comments. The workaround is to use ServletRequestAware and ServletResponseAware interface to get the request cookies or to set cookies in response.
  7. PrincipalAware: We can implement this interface in action class to get Principal information from HttpServletRequest object. This interface works with PrincipalProxy to provide user id, principal details.

Let’s see all these interfaces in action with a simple Struts 2 project. Our final project structure looks like below image.

Struts-2-Example-Project

Struts2 Configuration Files

web.xml

pom.xml

struts.xml

The configuration files are self explanatory and easy to understand.

Struts2 JSP Pages

login.jsp

home.jsp

JSP pages are also very simple and just used to send some data in request and then use them in result page.

Struts2 Action Class

HomeAction.java

A single action class implementing all the *Aware interfaces and using them to log some data in server log files.

Once we execute the login action and then hit home action multiple times, we get following logs.

Notice that request cookies are NULL, however I checked request and response in browser and it’s sending cookies in request.

That’s all for accessing Servlet API core components in Struts 2 Action classes. It’s simple and elegant to use.

By admin

Leave a Reply

%d bloggers like this: