2.9. Stateful Pages

PLEASE NOTE: stateful pages have been deprecated in Click 2.3.0 and will be removed in a future release. Do not use stateful pages in your applications. Instead use stateful controls or HttpSession to store state between requests.

Click supports stateful pages where the state of the page is saved between the users requests. Stateful pages are useful in a number of scenarios including:

To make a page stateful you simply need to set the page stateful property to true, have the page implement the Serializable interface and set the serialVersionUID indicator. For example:

package com.mycorp.page;

import java.io.Serializable;

import org.apache.click.Page;

public class SearchPage extends Page implements Serializable {

    private static final long serialVersionUID = 1L;

    public SearchPage() {

Stateful page instances are stored in the user's HttpSession using the pages class name as the key. In the example above the page would be stored in the users session using the class name: com.mycorp.page.SearchPage

2.9.1. Page Creation

With stateful pages they are only created once, after which they are retrieved from the session. However page event handlers are invoked for each request, including the onInit() method.

When you are creating stateful pages you typically place all your control creation code in the Pages constructor so it is invoked only once. It is important not to place control creation code in the onInit() method which will be invoked with each request.

If you have dynamic control creation code you would typically place this in the onInit() method, but you will need to take care that controls and or models are not already present in the page.

2.9.2. Page Execution

The default Click page execution model is thread safe as a new Page instance is created for each request and thread. With stateful pages a user will have a single page instance which is reused in multiple requests and threads. To ensure page execution is thread safe, users page instances are synchronized so only one request thread can execute a page instance at any one time.

2.9.3. Page Destruction

After normal page instances have been executed, they are de-referenced and garbage collected by the JVM. However with stateful pages they are stored in the users HttpSession so care needs to be take not to store too many objects in stateful page instances which may cause memory and performance issues.

When pages have completed their execution, all the Page's controls onDestroy() methods are invoked, and then the Page's onDestroy() method is invoked. This is your opportunity to de-reference any large sets or graphs. For example the Table control by default de-references its rowList in its onDestory() method.