Luckily (unlike cookies), Struts2 comes with features to give you access to sessions without dipping into the realms of the ServletRequest / ServletResponse. So here's how to use sessions with Struts 2: You need to make your action implement SessionAware to get access to the session:
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.SessionAware;

public class MyAction extends extends ActionSupport implements SessionAware {
  // For SessionAware
  Map<String, Object> session;
  public void setSession(Map<String, Object> session) {
    this.session = session;
Once you've done that, you can access the session with code like this:
public String execute() {
  int division = ...

  // Write to the session
  // Read from the session
  if (session.containsKey("division"))
  return "success";
And if you feel like configuring your session lifetimes manually, make the following edits to your web.xml:
  <!-- How long the sessions will stick around for -->
    <session-timeout>600</session-timeout> <!-- 600seconds = 10 mins -->

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