我想在会话未打开或关闭时重定向到起始页。我添加了 Servlet 过滤器,但它不起作用。
我的SessionFilter类:
public class SessionFilter implements Filter {
private ArrayList<String> urlList;
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String url = request.getServletPath();
HttpSession session = request.getSession(false);
if (new Bean().getLoggedIn() || urlList.contains(url) || session != null){
chain.doFilter(req, resp);
}
else{
response.sendRedirect("index.jsf");
}
}
public void init(FilterConfig config) throws ServletException {
String urls = config.getInitParameter("avoid-urls");
StringTokenizer token = new StringTokenizer(urls, ",");
urlList = new ArrayList<String>();
while (token.hasMoreTokens()) {
urlList.add(token.nextToken());
}
}
public void destroy() {
}
}
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.tsystems.demail.SessionFilter</filter-class>
<init-param>
<param-name>avoid-urls</param-name>
<param-value>/index.jsf, /registration.jsf</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
</web-app>
当我登录时new Bean().getLoggedIn()
是真的。注销时 - 错误。如何更改我的过滤器?我哪里有错误?