容器 ,而拦截器不依赖于 servlet 容器。
3、拦截器只能对 action 请求起作用,而过滤器则可以对几乎所有的请求起作
用。
4、在 action 的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初
始化时被调用一次
拦截器:就是在你调用一个方法前,或者调用一个方法后,做一些业务逻辑
的操作,例如打印字符串。
下面通过实例来看一下过滤器和拦截器的区别:
使用拦截器进行/admin 目录下 jsp 页面的过滤:
<package name="newsDemo" extends="struts-default"
namespace="/admin">
<interceptors>
<interceptor name="auth"
class="com.test.news.util.AccessInterceptor" />
<interceptor-stack name="authStack">
<interceptor-ref name="auth" />
</interceptor-stack>
</interceptors>
<!-- action -->
<action name="newsAdminView!*" class="newsAction"
method="{1}">
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authStack">
</interceptor-ref>
</action>
</package>
下面是我实现的拦截器:
package com.test.news.util;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.test.news.action.AdminLoginAction;
public class AccessInterceptor extends AbstractInterceptor {