Extend WebSession and add your custom getters/setters:

public final class MySession extends WebSession {

	private Object myObject;

	public PerManSession(Request request) {
		super(request);
	}

	public final Object getMyObject() {
		return myObject;
	}

	public final void setMyObject(Object myObject) {
		this.myObject = myObject;
	}

	// if you use java >= 1.5 you can make use of covariant return types
	public static MySession get() {
		return (MySession)Session.get();
	}
}

In your WebApplication you need to override the "newSession" method and return your session instance:

public final class MyApplication extends WebApplication {
...
	@Override
	public final Session newSession(Request request, Response response) {
		return new MySession(request);
	}
...
}

In your WebPage you can get/set your object:

...

((MySession)Session.get()).setMyObject(myObject);
// or java >= 1.5:
MySession.get().setMyObject(myObject);

...
  • No labels