使用過濾器來檢查當前用戶登錄,則需要通過檢查會話寫方法userIsLoggedIn()自己,屬性:
public class LoginFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
if (userIsLoggedIn()) {
//process request normally, pass up the filter chain to the servlet:
chain.doFilter(req, res);
} else {
//go to login screen instead
RequestDispatcher dispatcher = getRequestDispatcher("login");
dispatcher.forward(request, response);
}
}
}
在你需要聲明你的過濾器:
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>
com.foo.LoginFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
你可以使用過濾器來達到這個目的 – Abubakkar 2013-03-27 16:18:37
你能提供更多的信息嗎? – ufk 2013-03-27 16:23:49
這可以幫助http://www.oracle.com/technetwork/java/filters-137243.html – Abubakkar 2013-03-27 16:27:06