2011-06-22 60 views
1

您能否建議我如何繼續捕獲GWT項目中的會話超時?我使用gwt dispatch lib。 我m我可以做一些類似實現過濾器,然後檢查會話是否存在或不,但我想在gwt項目中有不同的方法。 歡迎任何幫助。當會話超時重定向到GWT項目中的登錄頁面

感謝

+0

您是否期望像這樣http://stackoverflow.com/questions/925078/session-management-in-gwt? – DonX

+0

您好,我檢查了它,但我的問題在這裏是你做什麼,以便在服務器端超時並拋出異常,因爲我看到你通過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 
    } 
} 

服務器:所有你ServiceImplementations延長AbstractServicesImpl,你可以訪問您的SessionData。重寫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); 
    } 
} 
相關問題