2011-06-22 10 views
1

GWTプロジェクトでセッションタイムアウトを取得する方法を教えてください。 gwtディスパッチライブラリを使用しています。 私は、フィルタを実装し、セッションが存在するかどうかを確認するようなことをすることはできますか?しかし、私はgwtプロジェクトでは異なるアプローチがあると思います。 何か助けを歓迎します。セッションタイムアウトがGWTプロジェクトのログインページにリダイレクトされる場合

おかげ

+0

このようなものが必要ですか?http://stackoverflow.com/questions/925078/session-management-in-gwt? – DonX

+0

こんにちはDin、私はそれをチェックアウトしましたが、ここで私の質問は、サーバー側でタイムアウトを取得し、例外をスローするために私はあなたがonFailureメソッドを介してクライアント側でキャッチ参照してくださいですか?ありがとう – brakebg

答えて

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 
    } 
} 

サーバー:あなたのSessionDataにへのアクセス権を持っているところあなたのすべてのServiceImplementationsは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(); 
    } 
    } 

} 

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

@Override 
protected void doUnexpectedFailure(Throwable t) { 
    if (this.sessionData != null) { 
    super.doUnexpectedFailure(t); 
    } 
} 
関連する問題