我的response.sendRedirect不起作用 [英] My response.sendRedirect isn't working

查看:651
本文介绍了我的response.sendRedirect不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SmartGWT应用程序,并在其中一个过滤器,我试图找出(登录)如果请求应该被转发(例如桌面到手机)。代码执行并且浏览器发出获取请求,但是没有得到响应,也没有做重定向。我已经尝试过使用 http://google.com ,但这并不起作用,所以它一定是别的。 / p>

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

HttpServletRequest请求=(HttpServletRequest)req;
HttpServletResponse响应=(HttpServletResponse)res;
HttpSession session = request.getSession();

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

WURFLManager manager = wurfl.getWURFLManager();

Device device = manager.getDeviceForRequest(request);

布尔webBrowser = isWebBrowser(设备);

String path =((HttpServletRequest)请求).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;

......

解决方案

在使用 response.sendRedirect()之前,您是否向HTTP响应发送了任何内容?要使用HTTP重定向HEADER,您不能向浏览器发送任何响应。即使是空格或分行/新行可以停止重定向。



如果您已经检查了所有的代码,并确保您没有发送任何内容浏览器,您可以使用JavaScript重定向< script> location.href ='yoururl';< / script> 。它不是一个很酷的解决方案,但它的工作。


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.

这篇关于我的response.sendRedirect不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆