开发中,常常需要对指定的请求格式进行过滤。

比如,Struts2的应用中,往往要过滤掉 *.jsp的请求

 ※http://127.0.0.1:8080/Sample/login.action这样的请求被允许。

 ※http://127.0.0.1:8080/Sample/login.jsp这样的请求应该被过滤,并指定跳转到某个页面或执行某个Action等等

Struts2中的过滤器负责过滤所有的*.action,然后进行处理。

同样可以自定义一个Filter类,来单独负责某种格式请求的处理。

实现javax.servlet.Filter接口

}

在web.xml配置文件中,添加Filter过滤

Code

 
    <filter-name>sample</filter-name>
    
<filter-class>test.FilterClass</filter-class> 
</filter> 
<filter-mapping> 
    
<filter-name>sample</filter-name> 
    
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

这样,所有的*.jsp请求都会被过滤,跳转到login.action

相关文章: