【问题标题】:GWT dealing with request errorGWT 处理请求错误
【发布时间】:2014-10-16 13:40:11
【问题描述】:

我有一个 GWT 模块,在其中我通过以下方式导航到不同的 URL:

 Window.Location.assign(url);

导航的 url 然后由一个 servlet 处理,直到此时如果有错误它由 resp.sendError 方法处理

 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed.");

然后会导航到浏览器错误页面。但是我想知道有没有我无法导航到错误页面?即,如果出现错误,我将能够签入我的 GWT 代码然后做些什么?比如重新发送请求等。

谢谢!

【问题讨论】:

  • 你的问题不清楚。你能说得更具体些吗?
  • 是场景不清楚还是我在做什么不清楚?

标签: servlets gwt httprequest


【解决方案1】:

当您离开您的网络应用程序时,就是这样。而不是使用Window.Location.assign,您应该创建一个HTTP request still from your webapplication,例如使用RequestBuilder
前面提到的文档中的示例:

import com.google.gwt.http.client.*;
...

String url = "http://www.myserver.com/getData?type=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));

try {
  Request request = builder.sendRequest(null, new RequestCallback() {
    public void onError(Request request, Throwable exception) {
       // Couldn't connect to server (could be timeout, SOP violation, etc.)
    }

    public void onResponseReceived(Request request, Response response) {
      if (200 == response.getStatusCode()) {
          // Process the response in response.getText()
      } else {
        // Handle the error.  Can get the status text from response.getStatusText()
      }
    }
  });
} catch (RequestException e) {
  // Couldn't connect to server
}

请注意,这仅在您的 servlet 和 web 应用程序位于同一地址(域、端口、协议)上时才有效,因为 Same Origin Policy。如果不是这样,还有some options,比如带填充的JSON(GWT 通过JsonpRequestBuilder 支持)。

【讨论】:

  • 谢谢伊戈尔,我会测试它并告诉你
猜你喜欢
  • 1970-01-01
  • 2013-10-02
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多