質問

私は私のクライアント側のアプリケーションのためにGWTを使用しています。しかし、私は、セッション管理を扱うことができるかどうかはわかりませんよ。 GWTアプリケーションは、すべてのサーバコールがAJAXを介して行われ、1つのページに常駐しています。セッションは、サーバー上の有効期限が切れた場合。のは、ブラウザを閉じて、RPCを使用してサーバーにいくつかの要求を送信していないユーザーを想定してみましょう、私のサーバーは、セッションの有効期限が切れていると、クライアント側の部分は、再びログイン画面を表示する必要があることを私のサンプルコードをアプリケーションに通知することができる方法:<? / P>

ContactDataServiceAsync contactDataService = GWT
                .create(ContactDataService.class);
        ((ServiceDefTarget) contactDataService).setServiceEntryPoint(GWT
                .getModuleBaseURL()
                + "contactDatas");

        contactDataService.getContact(2,
                new AsyncCallback<ContactData>() {
                    public void onFailure(Throwable caught) {
                                      //code to show error if problem in connection or redirect  to login page

                    }

                    public void onSuccess(ContactData result) {
                        displayContact(result);
                    }
                });
セッションが期限切れになった場合のみ、

それはそれ以外の場合は、のwindow.alert()を使用して、いくつかのエラーを表示したい、ログイン画面を表示しています。

この操作を行うと、サーバ側とクライアント側で必要なすべてのコードは何ですか?

にする方法
役に立ちましたか?

解決

あなたは、サーバーは、ユーザーがログアウトされた場合には、クライアントに含むAuthenticationExceptionをスローする可能性があります。
これは、ログイン・ページにユーザーをリダイレクトすることができますコールバックONFAILURE方法、でcatchedされます。

編集:
含むAuthenticationExceptionはもちろんの標準の例外ではない、私はちょうど例を作っていました。これは、標準の例外に固執するのがベストかもしれません。

あなたがinstanceof演算子の
を使用することができ、特定の例外をキャッチした場合にしようとするには、

    public void onFailure(Throwable e) {
                  if(e instanceof AuthenticationException) {
                        redirecttoLogin();
                  }
                  else {
                    showError(),
               }
            }

他のヒント

この直接RPCを使用したものには適用されませんが、RPCを使用していないあなたの人々のために、サーバーからHTTP 401を送信する必要があります。次に、あなたのRequestBuilderコールバックでそのステータスコードを確認することができます。

クライアント:のすべてのコールバックは、あなたがonFailur()を実装する抽象コールバックが伸びる。

public abstract class AbstrCallback<T> implements AsyncCallback<T> {

  @Override
  public void onFailure(Throwable caught) {
    //SessionData Expired Redirect
    if (caught.getMessage().equals("500 " + YourConfig.ERROR_MESSAGE_NOT_LOGGED_IN)) {
      Window.Location.assign(ConfigStatic.LOGIN_PAGE);
    }
    // else{}: Other Error, if you want you could log it on the client
  }
}

サーバー:あなたのSessionDataにへのアクセス権を持っている。ここで、はあなたのすべてのServiceImplementationsはAbstractServicesImplを拡張します。 onBeforeRequestDeserialized(文字列serializedRequest)をオーバーライドし、そこSessionDataに確認してください。 SessionDataには有効期限が切れている場合、クライアントへのspacificエラーメッセージを書き込みます。このエラーメッセージは、あなたのAbstrCallbackでchecktを取得し、ログインページにリダイレクトします。

されます
public abstract class AbstractServicesImpl extends RemoteServiceServlet {

  protected ServerSessionData sessionData;

  @Override
  protected void onBeforeRequestDeserialized(String serializedRequest) {

    sessionData = getYourSessionDataHere()

    if (this.sessionData == null){ 
      // Write error to the client, just copy paste
      this.getThreadLocalResponse().reset();
      ServletContext servletContext = this.getServletContext();
      HttpServletResponse response = this.getThreadLocalResponse();
      try {
        response.setContentType("text/plain");
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        try {
          response.getOutputStream().write(
            ConfigStatic.ERROR_MESSAGE_NOT_LOGGED_IN.getBytes("UTF-8"));
          response.flushBuffer();
        } catch (IllegalStateException e) {
          // Handle the (unexpected) case where getWriter() was previously used
          response.getWriter().write(YourConfig.ERROR_MESSAGE_NOT_LOGGED_IN);
          response.flushBuffer();
        }
      } catch (IOException ex) {
        servletContext.log(
          "respondWithUnexpectedFailure failed while sending the previous failure to the client",
          ex);
      }
      //Throw Exception to stop the execution of the Servlet
      throw new NullPointerException();
    }
  }

}

また、あなたはまた、スローNullPointerExceptionがログを避けるためにdoUnexpectedFailure(Throwableをトン)をオーバーライドすることができます。

@Override
protected void doUnexpectedFailure(Throwable t) {
  if (this.sessionData != null) {
    super.doUnexpectedFailure(t);
  }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top