Servlet过滤器映射到浏览器中的错误和QUOT / *结果;服务器重定向的方式,将永远不会完成&QUOT此地址的请求; [英] Servlet filter mapped on /* results in browser error "server is redirecting the request for this address in a way that will never complete"

查看:341
本文介绍了Servlet过滤器映射到浏览器中的错误和QUOT / *结果;服务器重定向的方式,将永远不会完成&QUOT此地址的请求;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个动态的JSP / Servlet的Web应用程序。为了处理会话,我使用的是 / * 映射一个过滤器的web.xml 。当我打开在Firefox的网页,它提供了以下的Firefox特定的错误消息:

I am developing a dynamic JSP/Servlet web application. In order to handle the session, I am using a filter which is mapped on /* in web.xml. When I'm opening a page in Firefox, it gives the following Firefox-specific error message:

Firefox已经检测到服务器重定向的方式为这个地址的请求,将永远不会完成

Firefox has detected that the server is redirecting the request for this address in a way that will never complete

显示在Chrome类似的错误。这是怎么造成的,我该怎么解决呢?

A similar error is shown in Chrome. How is this caused and how can I solve it?

推荐答案

您过滤器重定向到它与同样的条件下再次调用同样的过滤器的URL这又因此导致了新的重定向,等等。您的过滤器基本上是重定向到自身无限循环。 web浏览器阻止〜20的请求后,无限循环,从设计糟糕的web应用保存终端用户。

Your filter is redirecting to an URL which is invoking the very same filter with the very same conditions again which in turn thus results in a new redirect, etcetera. Your filter is basically redirecting to itself in an infinite loop. The webbrowser is blocking the infinite loop after ~20 requests to save the enduser from badly designed webapplications.

您需要修正你的过滤器因此,当已经完成它,它不执行重定向。让我们假设它是在映射登录滤波器的基本现实世界的例子/ * 应该被重定向到时没有登录用户通过<其中确定的登录页面code>用户属性开庭。你明明想,如果登录页面本身正在被请求的过滤器不应该重定向到登录页面。

You need to fix your filter accordingly that it is not performing a redirect when it has already been performed. Let's assume a basic real world example of a login filter which is mapped on /* which should be redirecting to the login page when the user is not logged in which is identified by user attribute in session. You obviously want that the filter should not redirect to the login page if the login page itself is currently been requested.

@WebFilter("/*")
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession(false);
        String loginURL = request.getContextPath() + "/login.jsp";

        boolean loggedIn = session != null && session.getAttribute("user") != null;
        boolean loginRequest = request.getRequestURI().equals(loginURL);

        if (loggedIn || loginRequest) {
            chain.doFilter(request, response); // Logged-in user found or already in login page, so just continue request.
        } else {
            response.sendRedirect(loginURL); // No logged-in user found and not already in login page, so redirect to login page.
        }
    }

    // ...
}

您看,如果你想允许/继续请求,只需调用 chain.doFilter()而不是 response.sendRedirect()的。如果您想更改到不同的目的地的请求时,只能使用重定向。

You see, if you want to allow/continue a request, just call chain.doFilter() instead of response.sendRedirect(). Use redirect only if you want to change a request to a different destination.

这篇关于Servlet过滤器映射到浏览器中的错误和QUOT / *结果;服务器重定向的方式,将永远不会完成&QUOT此地址的请求;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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