當會話沒有打開或關閉時,我想重定向到起始頁面。我添加了Servlet過濾器,但它不起作用。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()
是真的。註銷時 - false。如何更改我的過濾器?我哪裏有錯誤?
什麼是豆子?你的意思是每創建一個新的*時間? –
「但它不起作用」它不起作用,它根本不重定向,它重定向到錯誤的頁面,你得到一個HTTP 500,沒有任何反應,什麼?根據調試器的判斷,else塊中的代碼是否可以執行?請詳細說明,謝謝。 – Keith
@DaveNewton我的Bean類:http://pastebin.com/EKU1d0EY – rubaka