My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
StorageApi  
Quick getting started introduction to the Storage API
Phase-Implementation, API-Storage
Updated Mar 28, 2010 by bguijt

Introduction

The Storage API (download) leverages the W3C Web Storage API. See this article for a nice introduction to the sessionStorage.

With this API you are able to store data on the client using a Java Map-like interface (key/values).

Browser support

Not every browser supports this feature, and currently there are no fallback scenario's to rely on Flash, Java or Gears plugins. The following browsers are supported:

  1. iPhone Safari (OS3.0)
  2. Android (2.0)
  3. webOS (1.4)
  4. Desktop Safari (4.0)
  5. Chrome (4.0)
  6. Firefox (3.5)
  7. Internet Explorer (8)
  8. Opera (10.50)

Other browsers might also support Web Storage, we just didn't test them nor found any feature support documentation.

Usage

Get the Storage API using Maven

Add the repository and the following to your pom.xml file:

<dependencies>
  ...
  <dependency>
    <groupId>com.google.code.gwt-mobile-webkit</groupId>
    <artifactId>gwt-html5-storage</artifactId>
  </dependency>
</dependencies>

Add the Storage API to your project

If you don't use Maven, copy the gwt-html5-storage-x.x.x.jar file from the downloaded distribution to your project classpath. This jar contains everything you need to use the Storage API.

Next, in your GWT module gwt.xml file (which uses the Storage API), add the following entry:

<inherits name="com.google.code.gwt.storage.Html5Storage" />

This imports the Storage API into your module. Now you are ready to use the API!

is API supported?

The following will test whether the API is supported in your browser:

if (Storage.isSupported()) {
    // Interact with the storage...
}

Getting a Storage instance

There are two Storage types available. Both have exactly the same API, they just persist differently:

  1. localStorage actually persists data permanently;
  2. sessionStorage keeps the data available for the duration of the Session.

A Storage is obtained like this:

Storage localStorage = Storage.getLocalStorage();
Storage sessionStorage = Storage.getSessionStorage();

Writing in the Storage

Values can be written as follows:

localStorage.setItem("key", "value");

All keys and values must be Strings.

Reading the Storage

String value = localStorage.getItem("key");

Iterating over the Storage

for (int i=0; i<localStorage.getLength(); i++) {
    String key = localStorage.key(i);
    String value = localStorage.getItem(key);
    // ...
}

Accessing the Storage using a Java Map

For convenience sake we added a Java Map implementation over the Storage API. Using this Map you can read and write the Storage using familiar methods:

// Creating a Map instance using the localStorage:
Map<String, String> map = new StorageMap(localStorage);

// Invoke some Map method:
map.putAll(otherMap);
// ...

Word of advice: Please note that the Java Map interface is way bigger than the Storage interface, and introducing the StorageMap to your code implies some overhead in accessing the Storage data. Only use the StorageMap if absolutely necessary.

Comment by mathias....@gmail.com, Feb 9, 2010

Should work on Chrome 4.0 as well.

Comment by marton...@gmail.com, Oct 19, 2010

Firefox (WIN) 3.6.10 Does not like integers as key values when using. Works greate in Chrome (WIN) 8.0.552.0 dev, Safari (WIN) 5.0.2, Opera (WIN) 10.63.

Example: if (Storage.isSupported()) {

Storage localStorage = Storage.getLocalStorage(); int id = 5; localStorage.setItem(id +"", "value"); //Error in firefox
}

Gives: "com.google.gwt.core.client.JavaScriptException?: (NS_ERROR_DOM_INDEX_SIZE_ERR): Index or size is negative or greater than the allowed amount" in hosted mode with firefox.

Comment by project member bguijt, Oct 19, 2010

And so it seems!

http://firefox.sourcearchive.com/documentation/3.6plus-pnobinonly-0ubuntu5/nsDOMStorage_8cpp-source.html

Apparently, this seems to work 'as designed' according to Firefox. Feeding the API with an integer key is interpreted as an indexed value.

Prefix with an underscore (e.g. "5") should be a valid workaround.

Comment by funkymob...@gmail.com, Sep 29, 2011

I'm not using Mavern. I downloaded the jar to my project under a folder called "libs". Right clicked on the jar file and added to my build path. Then modified the gwt.xml file to add the inherit line. However I keep getting this error:

Unable to find com/google/code/gwt/storage/Html5Storage?.gwt.xml on your classpath ....

What am I doing wrong?


Sign in to add a comment
Powered by Google Project Hosting