Unlike sessions, it appears that cookies are a bit of an orphaned feature in Struts2. There are ways of reading them using the framework, but no way to write them. Since you have to go directly to the ServletResponse to write cookies, you may as well use the ServletRequest directly to read them while you're at it (IMO); so that's the approach i'm spruiking here. Firstly, you need access to the ServletRequest and ServletResponse. To get Struts 2 to inject these into your action, you need to implement ServletRequestAware and ServletResponseAware, like so:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

public class MyAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {

  protected HttpServletResponse servletResponse;
  public void setServletResponse(HttpServletResponse servletResponse) {
    this.servletResponse = servletResponse;

  protected HttpServletRequest servletRequest;
  public void setServletRequest(HttpServletRequest servletRequest) {
    this.servletRequest = servletRequest;
Once you've got those interfaces implemented as above, you can use them as below:
public String execute() {
  int division;

  // Save to cookie
  Cookie div = new Cookie("division", String.format("%d",division));
  div.setMaxAge(60*60*24*365); // Make the cookie last a year!
  // Load from cookie
  for(Cookie c : servletRequest.getCookies()) {
    if (c.getName().equals("division"))

  return "success";
This method does not require you to do any configuration in your web.xml nor struts.xml, so don't worry about those. Thanks for reading, hope this helps! References: http://stackoverflow.com/questions/3350554/the-struts2-cookies-map-does-not-seem-to-be-getting-set http://omkarp.blogspot.com/2007/08/working-with-cookies-in-struts-2-part-2.html

Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.

Chris Hulbert

(Comp Sci, Hons - UTS)

iOS Developer (Freelancer / Contractor) in Australia.

I have worked at places such as Google, Cochlear, Assembly Payments, News Corp, Fox Sports, NineMSN, FetchTV, Coles, Woolworths, Trust Bank, and Westpac, among others. If you're looking for help developing an iOS app, drop me a line!

Get in touch:
[email protected]

 Subscribe via RSS