How to handle a session timeout exception (with MVP Places and Activities)?

廉价感情. 提交于 2019-12-24 09:55:54

问题


how do you handle a time out request in a GWT app ? Here is a snipped of my web.xml file :

<session-config>
<session-timeout>30</session-timeout>
</session-config>

My GWT project is based on MVP Activities and Places. Whenever the user waits more than 30mn, i want to display a popup and redirect the user to the login page. Here is what i do for all RPC services :

 public void onFailure(Throwable caught) {
    ...
    if (caught instanceof InvocationException) {
                    {
                        Window.alert("Time out de session. Veuillez vous reconnecter. 2");
                        Window.open(GWT.getHostPageBaseURL() + "identification.html", "_self", null);
                        return;
                    }
    ...}

It works but several things are annoying : 1) the caught exception should be RequestTimeoutException. But it's not caught, which is why i use InvocationException instead. How come it's not caught ? 2) how can i handle this exception in a more generic way ? It's a bit stupid to have to catch that exception in all RPC services ... I read about some AsyncCallbackAdapter class ... 3) Right now i handle RPC services only but of course time out exception occurs everywhere : links, buttons, page refresh ... I'm using MVP Places and Activities. Is there a way to catch that exception when the user tries to go to a place ?

Thanks for helping


回答1:


  1. RequestTimeoutException is thrown when server is not responding.

  2. You should create you own checked exception, something like SessionTimeoutException and handle it in you client code. GWT knows how to handle (serialize) checked exceptions and pass them to your client code: http://code.google.com/webtoolkit/doc/latest/tutorial/RPC.html#exceptions

    To handle this in application-wide way, you can hook into RPC mechanism, by creating you own generator for remote services: How to redirect to login page after session expire in GWT RPC call

  3. The easiest way (without changing all existing code) would be to set a Timer to periodically check (every few minutes) the server session. When session times out show a modal DialogBox (preventing user input on other widgets) notifying user he/she needs to login again.



来源:https://stackoverflow.com/questions/6903507/how-to-handle-a-session-timeout-exception-with-mvp-places-and-activities

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!