If you want to create a simple application that needs to store some data in a database, the normal way to do this would be using a database like MySQL. Here you would send the data via RPC to your server and the server would connect to the database and store it. When you ask the server for some data, the server would again connect to the database, fetch the requested data and send it back to the client.
In my opinion this is some kind of overhead for just a simple application. So to avoid that you can use the Java Serialization. All you need to do here, is creating classes for the informations you want to store.
As an example we try to create a phone-book that is able to do:
- Save entries with name and phone number
- Organize entries in groups with names (e.g. Friends, Family, Work ..)
As you can see, the classes need to implement the interface Serializable to be store able. Now lets convert the diagram to code…
Phonebook:
public class Phonebook implements Serializable {
public List groups = new ArrayList();
public List entries = new ArrayList();
public List getGroups() {
return groups;
}
public List getEntries() {
return entries;
}
}
Entry:
public class Entry implements Serializable {
public Entry() {}
public String name;
public String number;
public List myGroups = new ArrayList();
public Entry(String name, String number) {
this.setName(name);
this.setNumber(number);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public List getMyGroups() {
return myGroups;
}
}
Group:
public class Group implements Serializable {
public Group(){}
public String name;
public List members = new ArrayList();
public Group(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getMembers() {
return members;}
}
You may have noticed, that Entry and Group have an empty constructor, this is needed to fulfill GWTs guidelines for serialization.
Now we need a RPC-Service to communicate with our server. This RPC-Service needs a function to save, and a function to load data. We gonna call them “getPhonebook” and “savePhonebook”.
DataHandlerImpl:
public class DataHandlerImpl extends RemoteServiceServlet implements DataHandler {
public void savePhonebook(String username, Phonebook book) {
FileOutputStream fos;
try { /**
* Open a file for this user and write the serialated data to it.
*/
fos = new FileOutputStream(username + ".ser");
ObjectOutputStream oos = new ObjectOutputStream( fos );
oos.writeObject( book );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Phonebook getPhonebook(String username) {
Phonebook root = null;
FileInputStream fis;
try { /**
* Take a look, if this user already has saved a Phonebook
* if so, transform it back.
*/
fis = new FileInputStream( username + ".ser");
ObjectInputStream ois = new ObjectInputStream( fis );
root = (Phonebook) ois.readObject();
} catch (FileNotFoundException e) {
/** * First time user logs in, so we have to create a new
* Phonebook for him.
*/
root = new Phonebook();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
/**
* Return either his new Phonebook or his saved Phonebook.
*/return root;}
}
Now we can call this RPC-Serivce with something like handler.getPhonebook(“johndoe”, callback); and we would get our saved phonebook and can work with it.
Get saved Phonebook:
final DataHandlerAsync handler = DataHandler.Util.getInstance();
public void onModuleLoad() {
AsyncCallback callback = new AsyncCallback() {
public void onFailure(Throwable caught) {}
public void onSuccess(Phonebook result) {createGUI(result);}
};
handler.getPhonebook("johndoe", callback);}
We ask the server for our Phonebook and send it over to the GUI to do something with it. When we want to save our changes, we just send our Phonebook back to the server.
Save Phonebook:
AsyncCallback callback = new AsyncCallback() {
public void onFailure(Throwable caught) {
/**
* Oh Oh something is wrong here...
*/
}
public void onSuccess(Object result) {
/**
* Yep everything is just fine...
*/
}
};
handler.savePhonebook("johndoe", book, callback);
Thats all. Now you have everything you need to load/save data. Now you just need to build a nice little GUI for your application. Maybe something like this:
