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;

......

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top