A web start app acting as a client can identify itself to a server the same way that any other client can - simply have the user log in at the beginning of a session. But since a desktop client has access to the local file system, user information can be stored locally. This means that data such as a userID and configuration preferences can be stored on the local hard drive. Since we want our desktop clients to work across platforms and since the developer cannot know exactly which directory this application is launching from, we have the preferences API to provide a layer of abstraction above the local file system.
If the user will primarily be using the same computer each time they run this application, this is most convenient way to store configuration info and user preferences. Here's an example:
/**
*
* Loads the user preferences from local storage using the Preferences API.
* The actual location of this file is platform and computer user-login
* dependant, but from the perspective of the preferences API, it is stored in
* a node referenced by the package this class resides in.
*/
private void loadPrefernces() {
try {
prefs = Preferences.userNodeForPackage(this.getClass());
}
catch(SecurityException stop) {
JOptionPane.showMessageDialog(null, "The security settings on this computer are preventing the application\n"
+ "from loading and saving certain user preference data that the program needs to function.\n"
+ "Please have your network administrator adjust the security settings to allow a Java desktop\n"
+ "application to save user preferences. The exception the program encountered is below:\n"
+ stop.toString(),
"Security Error", JOptionPane.ERROR_MESSAGE);
}
catch(Exception some) {
System.out.println("Other Preference Exception:");
System.out.println(some);
}
//extract information from the prefs object:
String username = prefs.get("user","default-newUser");
String userID = prefs.get("id", "0"); //a 0 ID means that the information was not found on the local computer.
}
//Use this method to store username and ID on the local computer.
public void saveUserPrefs() {
prefs.put("user", user.getUsername() );
prefs.put("id", "" + user.getID());
}
Of course, the above code does not help us if this is the first time the user is running the application on the computer or if the user was not assigned an ID. We therefore need to build some login functionality. The JNLP application can communicate with the web server the same way that a browser would do so. I found the httpClient libraries to be very helpful for implementing this. If the user's browser is behind a proxy, this may require some additional code to allow you java application to talk communicate back to the server.