我正在上大学,我需要做一个简单的登录系统,如果没有登录,拒绝直接URL访问某些页面,我试图使用过滤器,但是当我加载任何页面时,该过滤器被称为至少20次,然后被调用更多次,如果我愿意的话,在30秒内被调用80次,因为它想要,我需要帮助,这是我的代码。
过滤器
package edu.eci.pdsw.misc;
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;
import edu.eci.pdsw.view.LoginBean;
public class LoginFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
// Get the loginBean from session attribute
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(String.valueOf(LoginBean.mas()));
boolean loggedIn = (session != null) && (session.getAttribute("user") != null);
if (!loggedIn) {
String contextPath = ((HttpServletRequest)request).getContextPath();
((HttpServletResponse)response).sendRedirect(contextPath + "/Inicio.xhtml");
}
chain.doFilter(request,response);
}
public void init(FilterConfig config) throws ServletException {}
public void destroy() {}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>omega</param-value>
</context-param>
<!-- Login filter -->
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>edu.eci.pdsw.misc.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/Inicio.xhtml</welcome-file>
</welcome-file-list>
<listener>
<listener-class>edu.eci.pdsw.guice.GuiceContextListener</listener-class>
</listener>
</web-app>
如果还有其他需要,请告诉我,我会把它放在这里,谢谢您的帮助