欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaWeb: localhost 将您重定向的次数过多

程序员文章站 2022-03-25 08:52:37
...

JavaWeb开发遇到到的问题:

该网页无法正常运作
localhost 将您重定向的次数过多。
尝试清除 Cookie.
ERR_TOO_MANY_REDIRECTS

原因是Filter把自己也给过滤了:
我这里是对所有页面都进行过滤,不满足条件则跳转到login.jsp,然而忘了给login.jsp加判断了

package top.hsocode.Filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class StudentLoginFilter implements Filter {

    private FilterConfig config;
    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletRequest request = (HttpServletRequest)arg0;
        HttpServletResponse response = (HttpServletResponse)arg1;
        HttpSession session = request.getSession();


        String noLoginPaths = config.getInitParameter("noLoginPaths");
        if (noLoginPaths != null) {
            String[] strArray = noLoginPaths.split(";");
            for (int i = 0; i < strArray.length; i++) {
                if (strArray[i] == null || "".equals(strArray[i])) {
                    continue;
                }
                if(request.getRequestURI().indexOf(strArray[i]) != -1){
                    arg2.doFilter(arg0, arg1);
                    return;
                }
            }
        }


        if (session.getAttribute("email") != null) {
            arg2.doFilter(arg0, arg1);
        }else {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        config = arg0;
    }

}

所以做以下修改就可以了

package top.hsocode.Filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class StudentLoginFilter implements Filter {

    private FilterConfig config;
    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletRequest request = (HttpServletRequest)arg0;
        HttpServletResponse response = (HttpServletResponse)arg1;
        HttpSession session = request.getSession();


        String noLoginPaths = config.getInitParameter("noLoginPaths");
        if (noLoginPaths != null) {
            String[] strArray = noLoginPaths.split(";");
            for (int i = 0; i < strArray.length; i++) {
                if (strArray[i] == null || "".equals(strArray[i])) {
                    continue;
                }
                if(request.getRequestURI().indexOf(strArray[i]) != -1){
                    arg2.doFilter(arg0, arg1);
                    return;
                }
            }
        }
        /*修改部分*/
        if(request.getRequestURI().indexOf("/login.jsp") > -1){
            arg2.doFilter(arg0, arg1);
        } else if (session.getAttribute("email") != null) {
            arg2.doFilter(arg0, arg1);
        }else {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }
        /*修改部分*/
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        config = arg0;
    }

}

现在就可以正常访问了

相关标签: 重定向