My response.sendRedirect isn't working

蓝咒 提交于 2020-01-05 05:50:06

问题


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 (e.g. desktop to mobile). The code executes and the browser makes a get request but doesn't get a response and doesn't do a redirect. I've tried with http://google.com and that didn't work too so it must be something else.

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;

......


回答1:


Did you send any content to the HTTP response before using response.sendRedirect()? To use HTTP Redirect HEADER, you can't send any response to the browser. 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>. Its not a cool solution, but it works.



来源:https://stackoverflow.com/questions/18600334/my-response-sendredirect-isnt-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!