实现WebFilter获取错误:多次递归调用localhost

时间:2016-04-21 07:14:10

标签: servlets servlet-filters

对url路径进行过滤,但在遇到预期的URL时遇到此错误。

错误是:无法清除临时存储:确定某些文件对于Web应用程序内的访问不安全,或者对文件资源进行了太多调用。的SecurityError

Code snap:
@WebFilter(urlPatterns = {"/profile"}, description = "Session Checker Filter")
public class SessionCheckerFilter implements Filter {
    private FilterConfig config = null;
    static int i =0;
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
        config.getServletContext().log("Initializing SessionCheckerFilter");
    }

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

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        // Check to see if user's session attribute contains an attribute
        // named user_id. If the attribute is not exists redirect
        // user to the login page.
        //
        if (request.getSession().getAttribute("username") == null) {

            System.out.println(++i);
            response.sendRedirect(request.getContextPath() + "/");
      }
        else 
            response.sendRedirect(request.getContextPath() + "/profile");

    }
    public void destroy() {
        config.getServletContext().log("Destroying SessionCheckerFilter");
    }
}

1 个答案:

答案 0 :(得分:0)

经过两次更改后,此问题已解决。

1. My local server running on port 8090 and and in redirect URL i have  defined port is 8080. I have to make it same port.

2. In redirect URL i have defined xx.com other than localhost so in /etc/hosts i have to define xx.com other than localhost.