教你使用servlet拦截器,放行不需要拦截的内容
一般来说什么权限,日志问题都需要拦截器,servlet原生态拦截器肯定是没有框架中的拦截器好用的,特别是排除放行的内容,是不好用的,下面举个例子说明
package filter; import java.io.IOException; 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 com.mysql.jdbc.StringUtils; public class RoleFilter implements Filter{ public static String defaultUrl; // 不拦截的资源类型 private static String[] ignoreTypes; @Override public void destroy() { System.out.println("销毁拦截器"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; String requestUri = request.getRequestURI(); String contextPath = request.getContextPath(); String url = requestUri.substring(contextPath.length()); boolean isIgnoreType = false; if(ignoreTypes != null){ for (int i = 0; i < ignoreTypes.length; i++) { if (url.endsWith("." + ignoreTypes[i])) { isIgnoreType = true; break; } } }else{ chain.doFilter(request, response); } if(url.indexOf("/login.action") > -1||url.indexOf("/loginbuttom.action") > -1||url.indexOf("/loginsub.action") > -1||isIgnoreType==true){ System.out.println(url+"这是登录入口或者静态资源,放行"); chain.doFilter(request, response); }else{ String name = (String)request.getSession().getAttribute("UserName"); if(name==null){ response.sendRedirect(contextPath+"/login.action"); }else{ System.out.println("用户名:"+name); chain.doFilter(request, response); } } } @Override public void init(FilterConfig filterConfig) throws ServletException { defaultUrl = filterConfig.getInitParameter("defaultUrl"); String ignoreTypes = filterConfig.getInitParameter("ignoreTypes"); if(ignoreTypes != null && !ignoreTypes.trim().equals("")){ this.ignoreTypes = ignoreTypes.split(","); } } }
这是拦截器的java代码,实现filter接口就行了,然后看看web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Web_03</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>CenterController</servlet-name> <servlet-class>controller.CenterController</servlet-class> </servlet> <servlet-mapping> <servlet-name>CenterController</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <filter> <filter-name>RoleFilter</filter-name> <filter-class>filter.RoleFilter</filter-class> <init-param> <param-name>defaultUrl</param-name> <param-value>http://www.baidu.com</param-value> </init-param> <init-param> <param-name>ignoreTypes</param-name> <param-value>gif,bmp,png,jpg,js,flash,css</param-value> </init-param> </filter> <filter-mapping> <filter-name>RoleFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
配置就是这么简单,静态元素就在
ignoreTypes里面配置就可以了,非常简单