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;@Overridepublic void destroy() {// TODO Auto-generated method stub}@Overridepublic void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)throws IOException, ServletException {// TODO Auto-generated method stubHttpServletRequest 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");}}@Overridepublic void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stubconfig = 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;@Overridepublic void destroy() {// TODO Auto-generated method stub}@Overridepublic void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)throws IOException, ServletException {// TODO Auto-generated method stubHttpServletRequest 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");}/*修改部分*/}@Overridepublic void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stubconfig = arg0;}}
现在就可以正常访问了
如果觉得《JavaWeb: localhost 将您重定向的次数过多》对你有帮助,请点赞、收藏,并留下你的观点哦!