views:

172

answers:

1

We are using an application made in GWT with the server as tomcat. The project runs fine normally, however there are situations where the server is restarted. At such point of time, the ajax call made by the code below returns blank text with the status code as 304

RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST, URL.encode(serverUrl)); //-- serverUrl is the url to which this call is posted to.
requestBuilder.setHeader("Content-Type", "application/x-www-form-urlencoded");
requestBuilder.setHeader("Expires","0");
requestBuilder.sendRequest(
    postData, 
    new RequestCallback()
    {

        public void onError(Request request, Throwable exception)
        {
            //Do nothing
        }

        public void onResponseReceived(Request request, Response response)
        {
            //sometimes when the server is restarted, I get response.getStatusCode() = 304 and the response.getText() as blank
        }
    }
);

normally we get back some data from the server inside this response text. How do we now get the data when the response itself is blank ?

A: 

HTTP 304 means that the server expects the client to use a cached copy of the page. The HTTP specification indicates that the response body should be empty the case of a 304 response (see see RFC 2616 section 10.3.5). Are you getting the 304 error because the server is messing up or because it really expects the response to be cached by the client? Perhaps try setting Cache-Control: no-cache in your headers.

If the 304 statuses are related to the server messing up, and you can't get them to stop, you have a couple options:

  1. Actually cache responses in your app so that when you get a 304 you can use the cached response
  2. Capture 304 responses and retry the RPC (perhaps recursively, specifying a retryTimesLeft as a parameter to the function)
  3. Indicate that 304's are actually failures and call some error handling function to deal with the lack of data

Which you choose obviously depends on the needs of your app.

BinaryMuse