wmiao:一段登录过滤的代码 仅作参考! web.xml <!-- 登陆过滤 --> <filter> <filter-name>SessionFilter</filter-name> <filter-class>com.goocom.filter.SessionFilter</filter-class> <init-param> <!--登陆页面 --> <param-name>logonStrings</param-name> <param-value>login/login.jsp</param-value> </init-param> <init-param> <!--过滤页面参数 --> <param-name>includeStrings</param-name> <param-value>.jsp;.do</param-value> </init-param> <init-param> <!--不过滤页面参数 --> <param-name>noIncludeStrings</param-name> <param-value>forward.jsp;login.do;login_win.jsp</param-value> </init-param> <init-param> <!--跳转页面 --> <param-name>redirectPath</param-name> <param-value>/common/forward.jsp</param-value> </init-param> <init-param> <!--过滤器是否有效 --> <param-name>disabletestfilter</param-name> <param-value>Y</param-value> </init-param> </filter> <filter-mapping> <filter-name>SessionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> SessionFilter.java public class SessionFilter implements Filter { public static boolean isContains(String container, String[] regx) { boolean result = false; for (int i = 0; i < regx.length; i++) { if (container.indexOf(regx[i]) != -1) { return true; } } return result; } public FilterConfig config; public void setFilterConfig(FilterConfig config) { this.config = config; } public FilterConfig getFilterConfig() { return config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpreq = (HttpServletRequest) request; HttpServletResponse httpres = (HttpServletResponse) response; HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper( (HttpServletResponse) response); String logonStrings = config.getInitParameter("logonStrings"); String includeStrings = config.getInitParameter("includeStrings"); String noIncludeStrings = config.getInitParameter("noIncludeStrings"); String redirectPath = httpreq.getContextPath() + config.getInitParameter("redirectPath"); String disabletestfilter = config.getInitParameter("disabletestfilter"); if (disabletestfilter.toUpperCase().equals("N")) { chain.doFilter(request, response); return; } String[] logonList = logonStrings.split(";"); String[] includeList = includeStrings.split(";"); String[] noIncludeList = noIncludeStrings.split(";"); UserVO user = (UserVO) httpreq.getSession().getAttribute("userVO"); if (user == null) { if (!this.isContains(httpreq.getRequestURI(), includeList)) { chain.doFilter(request, response); return; } if (this.isContains(httpreq.getRequestURI(), logonList)) { chain.doFilter(request, response); return; } if (this.isContains(httpreq.getRequestURI(), noIncludeList)) { chain.doFilter(request, response); return; } wrapper.sendRedirect(redirectPath); } else { chain.doFilter(request, response); } } public void destroy() { this.config = null; } public void init(FilterConfig filterConfig) throws ServletException { this.config = filterConfig; } } /* * <filter> <filter-name>SessionFilter</filter-name> * <filter-class>com.goocom.filter.SessionFilter</filter-class> <init-param> * <param-name>logonStrings</param-name> <param-value>login/login.jsp</param-value> * </init-param> <init-param> <param-name>includeStrings</param-name> * <param-value>.jsp;.html</param-value> </init-param> <init-param> * <param-name>redirectPath</param-name> <param-value>/login/login.jsp</param-value> * </init-param> <init-param> <param-name>disabletestfilter</param-name> * <param-value>Y</param-value> </init-param> </filter> <filter-mapping> * <filter-name>SessionFilter</filter-name> <url-pattern>/*</url-pattern> * </filter-mapping> */ forward.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <jsp:directive.page import="com.goocom.util.Global" /> <% String path = request.getContextPath(); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <mce:script language=javascript><!-- function doLoad(){ document.getElementById("a1").click(); } // --></mce:script> <body οnlοad="doLoad()"> <a href="<%=path%>/login/login.jsp" target="_top" id="a1"></a> </body> </html>