简体   繁体   English

我的response.sendRedirect无法正常工作

[英]My response.sendRedirect isn't working

I have a SmartGWT app and in it a Filter in which I'm trying to figure out (on login) if the request should be forwarded (eg desktop to mobile). 我有一个SmartGWT应用程序,在其中有一个过滤器,我试图在该过滤器中(登录时)确定是否应转发请求(例如,将桌面转发到移动设备)。 The code executes and the browser makes a get request but doesn't get a response and doesn't do a redirect. 执行代码,浏览器发出一个get请求,但没有得到响应,也没有进行重定向。 I've tried with http://google.com and that didn't work too so it must be something else. 我已经尝试使用http://google.com ,但该方法也无法正常运行,因此必须是其他方法。

public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws ServletException, IOException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession();

    WURFLHolder wurfl = (WURFLHolder) getFilterConfig().getServletContext().getAttribute(WURFLHolder.class.getName());

    WURFLManager manager = wurfl.getWURFLManager();

    Device device = manager.getDeviceForRequest(request);

    boolean webBrowser = isWebBrowser(device);

    String path = ((HttpServletRequest) request).getRequestURI();

    boolean isBrowserOnMobile = true; // webBrowser && path.contains(MOBILE_REQ_PATH);

    boolean isMobileOnDesktop = !webBrowser && path.contains(DESKTOP_REQ_PATH);

    if (isBrowserOnMobile || isMobileOnDesktop) {
        if (isBrowserOnMobile) {
            path = "http://www.google.com";
        } else {
            path = "/PregledPredmeta/MobileAppEntryPoint.html";
        }
        response.encodeRedirectURL(path);
        response.sendRedirect(path);
        return;

...... ......

Did you send any content to the HTTP response before using response.sendRedirect() ? 在使用response.sendRedirect()之前,您是否将任何内容发送到HTTP响应? To use HTTP Redirect HEADER, you can't send any response to the browser. 要使用HTTP重定向HEADER,您不能将任何响应发送到浏览器。 Even space or line-break/new-line can stop the redirect. 甚至空格或换行/换行符也可以停止重定向。

If you've checked all the code and make sure you haven't sent any content to the browser, you can use a JavaScript redirection <script>location.href='yoururl';</script> . 如果您已经检查了所有代码并确保未向浏览器发送任何内容,则可以使用JavaScript重定向<script>location.href='yoururl';</script> Its not a cool solution, but it works. 它不是一个很酷的解决方案,但它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM