GWT RPC possible in window close handler?

I am listening for the window close event:

closeHandlerReg = Window.addCloseHandler(new CloseHandler<Window>() {
  @Override
  public void onClose(CloseEvent<Window> event) {
    // ...
  }
});

      

The documentation states that no UI can be displayed in this callback. How about GWT RPC calls? I'm trying to do this, but it doesn't show up on the server (either in breakpoints or in log statements).

0


source to share


4 answers


The problem is that GWT RPC is asynchronous and calls to RPC services return immediately. In this case, the window is closed before the browser can send the underlying XMLHTTPRequest.

If you absolutely must, you should be able to access some kind of servlet (not GWT RPC) with a "synchronous" XMLHTTPRequest. Here's an example: http://weblogs.asp.net/bleroy/archive/2005/12/15/433278.aspx



But you really shouldn't be doing anything like that in window.onunload

or window.onbeforeunload

(these are the main DOM events for CloseEvent and ClosingEvent for window. There is probably a better way to do what you are trying to do.

+2


source


The use case you have should be possible. When you make an RPC call to closeHandler, it should arrive at the server, because while it is returning directly, it started sending data and set a callback to wait for the result. However, the callback will not work because the connection is lost because the window is closed. But this is not a problem as you only want to notify the server. So maybe the question you are posting? and does it work at all when you post it at some other point in the code and not in closeHandler?



+2


source


Old question, but still - maybe someone is facing the same problem.

RPCs will not work in the close handler as discussed. This workaround worked for me:

In the onClose method, do something like:

Window.Location.replace(GWT.getModuleBaseURL() + "rpcCall?param1=" + param1 + "&param2=" + param2);

      

whereas "rpcCall" is the name of the rpc url you set in your web.xml file. Of course, a random number of parameters can be passed in the URL.

Then, in your server-side implementation of the rpc interface, you can override the doGet method:

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {

    String param1 = URLDecoder.decode(request.getParameter("param1"), "UTF-8");
    String param2 = URLDecoder.decode(request.getParameter("param2"), "UTF-8");

    // do something
}

      

0


source


And one more solution: not to make an rpc call when the window is closed, but a normal HTTP call, which can then be handled by a custom servlet on the server side.

In your GWT module, do something like this:

Window.addWindowClosingHandler(new ClosingHandler() {

        @Override
        public void onWindowClosing(ClosingEvent event) {
            sendWindowClosed(GWT.getModuleBaseURL() + "teardownservice");
        }
    });


    private native void sendWindowClosed(String url)
    /*-{
        var Http = new XMLHttpRequest();
        Http.open("GET", url);
        Http.send();
       }-*/;
}

      

In a server side servlet, you can handle this call:

public class TearDownServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        .... whatever you want ....
    }
}

      

0


source







All Articles