What is a session?
A session is a mechanism to identify a user across more than one page request or visit to a web-site.
Why do we need sessions?
Java’s Servlet API and by extension GWT’s RPC mechanism offers us the ability to use HTTPSession Interface to create session which can be used to identify users across multiple requests and visits to web-sites (in this case our GWT applications). Using HTTPSession Interface also allows us to bind objects to sessions, allowing user information to be persisted across multiple connections.
GWT and Sessions
In GWT, all GWT services are implemented in classes that extend the GWT RemoteServiceServlet class. The RemoteServiceServlet base class itself extends the HTTPServlet class and thus gives us access to HTTPSessions. The RemoteServiceServlet provides some really nice features outside of just automagically handling serializing incoming and outgoing requests from GWT clients. In this tutorial you will learn how to use a RemoteServiceServlet to get a session and how to use that session to store information on the server.
GWT sessions demo – The Weather Application
The Weather Application will be used to demonstrate how we can use sessions to store application information on the server. The Weather Application is an application that allows a user to enter a 5 digit zip-code and make a call to a GWT service to get the weather conditions at that zip-code. The service gets its weather data from weather.com’s XML data feed. Each successful weather query is saved to the server side session. The Weather Application displays the list of successful queries stored in the session as a list of GWT Hyperlinks. The Weather application is also History enabled. This means that users can use their browser’s back and forwards buttons in the Weather Application.
Accessing an HTTPSession in GWT
Server side sessions (HTTPSessions) can be accessed from inside of GWT remote service implementations. Before a GWT service implementation can get a hold of the current HTTPSession object, it needs to get a handle on the current request (HTTPServletRequest). The RemoteServiceServlet class makes actually getting the current request very simple. The following line of code must be called from inside of your remote service implementation class.
Once we have the HTTPServletRequest we can use it to get the current HTTPSession
Storing data in a session
Now that we have the session, we can use it to persist information on the server side. Each HTTPSession object has a built-in data store that allows you to store any number of key/value pairs. The Weather Application uses the server side session to store a HashMap of unique successful queries. Here is code for methods that can be used to get and put a HashMap into a session. This code can be found in Utils.java:
Putting it all together
Here is the implementation (WeatherServiceImpl.java) of the Weather Service. The Weather Service has two methods, one to get the current weather give a zip-code, and another to get the HashMap of successful queries so far from the session.
When a client makes a Weather Service call to get the current weather for a particular zip-code, the service implementation attempts to get the current from the weather.com XML data feed. If it is successful in getting the weather from the feed, the service implementation gets the session from the current request, gets the HashMap of successful queries from the session, puts the new query into the HashMap, and puts the HashMap back into the session.
When a client makes makes a call to get the list of successful queries so far, the service implementation gets the session from the current request, gets the HashMap from the session and returns the HashMap to the client.
Weather app screenshot
All the source code provided on this site is distributed under the Apache Open Source License v2.0.