1

GWT プロジェクトでセッション タイムアウトをキャッチする方法を教えてください。私はgwtディスパッチライブラリを使用しています。フィルターを実装してから、セッションが存在するかどうかを確認するようなことができるのだろうかと思いますが、gwt プロジェクトにはさまざまなアプローチがあると思います。どんな助けでも大歓迎です。

ありがとう

4

1 に答える 1

2

クライアント:すべてのコールバックは、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
  }
}

サーバー:すべての ServiceImplementations は、SessionData にアクセスできる AbstractServicesImpl を拡張します。onBeforeRequestDeserialized(String serializedRequest) をオーバーライドし、そこで SessionData を確認します。SessionData の有効期限が切れている場合は、特定のエラー メッセージをクライアントに書き込みます。このエラー メッセージは AbstrCallback でチェックされ、ログイン ページにリダイレクトされます。

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();
    }
  }

}

さらに、doUnexpectedFailure(Throwable t) をオーバーライドして、スローされた NullPointerException のログ記録を回避することもできます。

@Override
protected void doUnexpectedFailure(Throwable t) {
  if (this.sessionData != null) {
    super.doUnexpectedFailure(t);
  }
}
于 2013-01-15T13:24:34.327 に答える