Calling remote Servlet from GWT

2.2k Views Asked by At

I am trying to call remote servlet from GWT, actually the GWT-RPC doesn't seems to work, so I am trying to do it using the RequestBuilder.

Here's the code snippet:

    String url = "http://some-remote-host:8888/GWTJSTest/SomeServlet?name=" + textBox.getText();
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
//              requestBuilder.setHeader("Origin", "*");
//              requestBuilder.setHeader("Access-Control-Allow-Origin", "*");
    try
    {
        requestBuilder.sendRequest(null, new RequestCallback()
        {
            public void onResponseReceived(Request request, Response response)
            {
                if (response.getStatusCode() == 200)
                {
                    Window.alert(response.getText());
                }else
                {
                    Window.alert(response.getText() + " : " + response.getStatusCode() + response.getStatusText());
                }
            }

            public void onError(Request arg0, Throwable arg1)
            {
                Window.alert(arg1.toString());

            }
        });
    } catch (RequestException e)
    {
        Window.alert("CATCH BLOCK: " + e.getMessage());
        e.printStackTrace();
    }

Actually, IE8 returns the data but after a warning message, but Firefox doesn't! Why is this?

As you see, I am trying to set some request headers but no way.

3

There are 3 best solutions below

0
On BEST ANSWER

I've added : <add-linker name="xs" /> to .gwt.xml

And then replaces GWT-PRC by JsonpRequestBuilder (transforming JSONP between the server and the client)

0
On

If you're trying to make a request to your own server and port (the same one that your GWT page is on), replace the first line with:

String url = "/GWTJSTest/SomeServlet?name=" + textBox.getText();

If you're trying to talk to a different server, or a different port on your own server, the Same Origin Policy will keep you from doing that. You'll need to proxy it from your own server.

0
On

The remote servlet is the one that needs to set the CORS header that you have:

Access-Control-Allow-Origin: *

Alternatively you can specify just your own domain instead of * if you don't want other domains interacting with the remote servlet.