struts1的多个filter的配置
Web.xml的配置,关于那个url-pattern我是费了点劲,得出的结论是url-pattern是IE地址栏里的URL,并不是相对WEN-INF的相对路径,如果要拦截WEB-INF下的JSP文件夹里的页面的话,<url-pattern>/jsp/*</url-pattern>,<url-pattern>/WEB-INF/jsp/*</url-pattern>
这两种配置都是不对的。原因是,它始终关注着浏览器地址栏里的地址。我就是这样的理解的。
<filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>filter.CharacterEncodingFilter</filter-class></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>IsLogin</filter-name><filter-class>filter.LoginFilter</filter-class><init-param><param-name>loginPage</param-name><param-value>/index.jsp</param-value></init-param></filter> <filter-mapping><filter-name>IsLogin</filter-name> //拦截后缀名为showUserList.do的URL<url-pattern>/showUserList.do</url-pattern> //拦截后缀名为executeBussiessMethod.do的URL<url-pattern>/executeBussiessMethod.do</url-pattern></filter-mapping>
public class LoginFilter implements Filter{/** * */private static final long serialVersionUID = 1L; private static Logger LOG = Logger.getLogger(LoginFilter.class); private FilterConfig config;public void init(FilterConfig config) throws ServletException {this.config=config;}public void destroy() {this.config=null;}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {System.out.println("===============Filter LoginFilter===============");HttpServletRequest requ=(HttpServletRequest)request;HttpServletResponse resp = (HttpServletResponse)response; HttpSession session=requ.getSession();if(session.getAttribute("session_user")==null){ //这里的 test是应用名字,这里并不像test是根目录一样。resp.sendRedirect("/test/index.jsp"); }else{chain.doFilter(request, response);}}