如何识别GWT中的http错误代码?

时间:2011-02-10 20:14:32

标签: gwt gxt

当我的用户在会话过期后尝试对我们的网站执行操作时(例如,如果他们将浏览器打开),服务器会响应HTTP状态405,因为用户不再登录。

发生这种情况时,我想将用户重定向到登录屏幕。

如何识别GWT中何时返回405错误代码,以便我可以重定向用户?

由于

修改 这是我正在使用的过滤器:

public class AuthenticationFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        if (req instanceof HttpServletRequest) {
            boolean isLoggedIn = CustomSecurity.login((HttpServletRequest)req);
            if (isLoggedIn) {
                // TODO: How to redirect the user here???
                }
        }
        chain.doFilter(req, res);
    }

    public void init(FilterConfig arg0) throws ServletException {
    }
}

web.xml 内容:

<filter>
    <filter-name>Authentication Filter</filter-name>
    <filter-class>com.company.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Authentication Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

如何重定向用户?还有,有办法强制整个浏览器重定向?由于这是一个小部件,我认为Window.Location.assign('url')只会重定向小部件的HTML内容。

4 个答案:

答案 0 :(得分:3)

我目前正在开发一个GWT应用程序,它可以执行与您要求的非常相似的操作。但是,我们在标准的javax.servlet.Filter子类中处理这些重定向,我们在web.xml中按照惯例定义这些重定向并将其映射到/ *以便它捕获所有请求(我们也使用此过滤器以及其他许多原因)

我不知道你是否也在使用过滤器,或者甚至只是在你的GWT应用程序中使用某种全能的Servlet,但如果你是,那么你的问题的解决方案就很容易了。您将拥有HttpServletRequest的句柄,您可以看到getSession(false)方法是否返回null,然后您知道发送请求的用户不再具有会话。然后你可以在你的登录页面上做一个标准的response.sendRedirect(...)。

答案 1 :(得分:2)

对于GWT-RPC,只要服务器返回200响应代码,您就会获得StatusCodeException。可以从异常中检索状态代码,您可以使用Window.Location.assign(url)通过GWT重定向。对于RequestBuilder,可以通过response.getStatusCode()访问状态代码cqan。

编辑:如果GWT的窗口不是根窗口,这是重定向的代码。

private native void redirect(String url)/*-{
  var win = $wnd;
  while (win.parent != null && win.parent != win)
    win = win.parent;
  win.location = url;
}-*/;

答案 2 :(得分:1)

RequestCallback实施中,您可以查看response.getStatusCode()。我们在后端有Spring Security,如果未经过身份验证,则会将用户连接到基于表单的登录页面。验证后,它会重定向它们。

例如:

@Override
public void onResponseReceived(Request request, Response response) {
  if (SC_UNAUTHORIZED == response.getStatusCode()) {
    Window.Location.reload();
  }
}

答案 3 :(得分:0)

对编码人员使用try / catch!

AsyncCallback<String> callback = new AsyncCallback<String>() {
        public void onFailure(Throwable caught) {
            try {
                throw caught;
            } catch (StatusCodeException statusCodeException) {
                Util.handleException(statusCodeException);
            } catch (Throwable exception) {
                Util.handleException(exception);
            }
        }

        public void onSuccess(String result) {
            Window.alert("Result : " + result);
        }
    };

然后处理你的结果

final static public void handleException(StatusCodeException exception) {
        String errorMessage = exception.toString() + 
                              "\n CODE: " + exception.getStatusCode() + 
                              "\n MESSAGE: " + exception.getEncodedResponse();
        Window.alert("ERROR: " + errorMessage);
    }    
final static public void handleException(Throwable exception) {
        String errorMessage = exception.toString() + "\n";
        for (StackTraceElement trace : exception.getStackTrace()) {
            errorMessage += trace.getClassName() + " :: " + 
                            trace.getMethodName() + " :: " + 
                            trace.getLineNumber() + "\n";
        }
        Window.alert("ERROR: " + errorMessage);
    }

请记住,重载是你的朋友!不要忘记,如果你没有详细或漂亮地编译你的代码,你将获得堆栈跟踪的垃圾。除非当然有人想出如何破解JSStackEmulation类

http://code.google.com/p/google-web-toolkit/wiki/WebModeExceptions

相关问题