Checking session expiration in GWT

Every web-application has mechanisms for checking session expiration. Most of them use standard mechanisms, which are provided by the application server. A GWT-based web application is an exception in this case and standard mechanisms will not work here.

In this post I’m describing, how to implement the mechanism for checking session status.

SessionExpirationException

First of all, let’s create Exception, which will be used for notifying the GWT client about session expiration.

It is a simple extension of an Exception-class, but it also implements IsSerializable (note that Serializable interface can not be used here).

Server side

After that some changes on the server side should be made.

For checking session’s state let’s override onBeforeRequestDeserialized-method of your service implementation:

Here I have wrapped SessionExpirationException into RuntimeException for throwing this exception from this method.

As a next step we need to extend the serialization mechanism – otherwise we will not be able to identify exception on the client side. So let’s override doUnexpectedFailure-method:

So this method will analyse sever exception and if it implements IsSerializable then it will serialize it.

Client side

So now the server serializes our exception and we should handle it:

Here I have checked exception’s type and showed session expiration pop-up.