日期:2014-05-17 浏览次数:20736 次
<filter> <filter-name>checkLogin</filter-name> <filter-class>hospital.filters.LoginFilter</filter-class> <init-param> <param-name>redirectURL</param-name> <param-value>/web/login.jsp</param-value> </init-param> <init-param> <param-name>checkSessionKey</param-name> <param-value>loginName</param-value> </init-param> <init-param> <param-name>notCheckURLList</param-name> <param-value>/web/LoginServlet;/web/login.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>checkLogin</filter-name> <url-pattern>/web/*</url-pattern> </filter-mapping>
package hospital.filters; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; 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; public class LoginFilter implements Filter{ protected FilterConfig filterConfig = null; private String redirectURL = null; private List notCheckURLList = new ArrayList(); private String sessionKey = null; public void destroy() { System.out.println("destroy"); notCheckURLList.clear(); } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("doFilter"); HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; HttpSession session = request.getSession(); if (sessionKey == null) { filterChain.doFilter(request, response); return; } if ((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null) { response.sendRedirect(request.getContextPath() + redirectURL); return; } filterChain.doFilter(servletRequest, servletResponse); } public void init(FilterConfig arg0) throws ServletException { System.out.println("init"); this.filterConfig = filterConfig; redirectURL = filterConfig.getInitParameter("redirectURL"); sessionKey = filterConfig.getInitParameter("checkSessionKey"); String notCheckURLListStr = filterConfig .getInitParameter("notCheckURLList"); if (notCheckURLListStr != null) { StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";&quo