自定义filter包

在有些时候,你可能需要以你的所有项目进行全局的过滤。

因为你的项目可以设计到互相的依赖和调用 。

修改在tomcat下的conf下的web.xml文件。和在原来的web-inif下的修改一样,添加filter.

然后将你的filter打包成jar,放在tomcat下的lib目录下,如果你知道tomcat的lib目录的作用的话。

  1. <filter>  
  2.         <filter-name>appFilter</filter-name>  
  3.         <filter-class>com.common.AppFilter</filter-class>  
  4.     </filter>  
  5.     <filter-mapping>  
  6.         <filter-name>appFilter</filter-name>  
  7.         <url-pattern>/*</url-pattern>  
  8.         <dispatcher>REQUEST</dispatcher>  
  9.         <dispatcher>FORWARD</dispatcher>  
  10.         <dispatcher>INCLUDE</dispatcher>  
  11.     </filter-mapping>  
  1. public class PathFilter implements Filter {  
  2.   
  3.     public void destroy() {}  
  4.   
  5.     public void doFilter(ServletRequest request, ServletResponse response,  
  6.             FilterChain chain) throws IOException, ServletException {  
  7.   
  8.         HttpServletRequest req      = (HttpServletRequest)request;   
  9.         HttpServletResponse resp    = (HttpServletResponse)response;   
  10.         String uri = req.getRequestURI();  
  11.           
  12.         if(uri.endsWith("home.htm")){  
  13.             resp.sendRedirect("/hdsst/home/index.jsp");  
  14.         }  
  15.           
  16.         chain.doFilter(req,resp);  
  17.     }  
  18.       
  19.     public void init(FilterConfig filterConfig) throws ServletException {}  
  20.   
  21. }  
 
原文地址:https://www.cnblogs.com/caobojia/p/5987692.html