クライアントとして機能する Web Start アプリは、他のクライアントと同じ方法でサーバーに対して自身を識別することができます。つまり、セッションの開始時にユーザーをログインさせるだけです。ただし、デスクトップ クライアントはローカル ファイル システムにアクセスできるため、ユーザー情報をローカルに保存できます。これは、ユーザー ID や構成設定などのデータをローカル ハード ドライブに保存できることを意味します。デスクトップクライアントをプラットフォーム間で動作させたいため、開発者はこのアプリケーションがどのディレクトリから起動されているかを正確に知ることができない ため、ローカルファイルシステムの上に抽象化のレイヤーを提供するための設定 APIがあります。
ユーザーがこのアプリケーションを実行するたびに主に同じコンピューターを使用する場合、これは構成情報とユーザー設定を保存する最も便利な方法です。次に例を示します。
/**
*
* 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());
}
もちろん、ユーザーがコンピューターでアプリケーションを初めて実行する場合や、ユーザーに ID が割り当てられていない場合、上記のコードは役に立ちません。したがって、いくつかのログイン機能を構築する必要があります。JNLP アプリケーションは、ブラウザと同じ方法で Web サーバーと通信できます。これを実装するには、httpClientライブラリが非常に役立つことがわかりました。ユーザーのブラウザーがプロキシの背後にある場合、Java アプリケーションがサーバーと通信できるようにするために、追加のコードが必要になる場合があります。