struts2拦截器和过滤器的区别

 我来答
百度网友5e15b90
2017-11-08 · TA获得超过1031个赞
知道小有建树答主
回答量:806
采纳率:91%
帮助的人:811万
展开全部

拦截器和过滤器的区别:

1、拦截器是基于java的反射机制的,而过滤器是基于函数回调

2、过滤器依赖与servlet容器,而拦截器不依赖与servlet容器

3、拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用

4、拦截器可以访问action上下文、值栈里的对象,而过滤器不能

5、在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次

拦截器 :是在面向切面编程的就是在你的service或者一个方法前调用一个方法,或者在方法后调用一个方法比如动态代理就是拦截器的简单实现,在你调用方法前打印出字符串(或者做其它业务逻辑的操作),也可以在你调用方法后打印出字符串,甚至在你抛出异常的时候做业务逻辑的操作。

下面通过实例来看一下过滤器和拦截器的区别:

使用拦截器进行/admin 目录下jsp页面的过滤

[html] view plain copy

  • <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 name="defaultStack"/>   

  • <interceptor-ref name="authStack">   

  • </interceptor-ref>   

  • 下面是我实现的Interceptor class: 

    [java] view plain copy

  • 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;   

  • /**  

  • * @author chaoyin  

  • */   

  • public class AccessInterceptor extends AbstractInterceptor {   

  • private static final long serialVersionUID = -4291195782860785705L;   

  • @Override   

  • public String intercept(ActionInvocation actionInvocation) throws Exception {   

  • ActionContext actionContext = actionInvocation.getInvocationContext();   

  • Map session = actionContext.getSession();   

  • //except login action   

  • Object action = actionInvocation.getAction();   

  • if (action instanceof AdminLoginAction) {   

  • return actionInvocation.invoke();   

  • }   

  • //check session   

  • if(session.get("user")==null ){   

  • return "logout";   

  • }   

  • return actionInvocation.invoke();//go on   

  • }   

  • }   

  • 过滤器:是在javaweb中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者 struts的action前统一设置字符集,或者去除掉一些非法字符.

    使用过滤器进行/admin 目录下jsp页面的过滤,首先在web.xml进行过滤器配置: 

    [html] view plain copy

  • <filter>   

  • <filter-name>access filter</filter-name>   

  • <filter-class>   

  • com.test.news.util.AccessFilter   

  • </filter-class>   

  • </filter>   

  • <filter-mapping>   

  • <filter-name>access filter</filter-name>   

  • <url-pattern>/admin/*</url-pattern>   

  • </filter-mapping>   

  • 下面是过滤的实现类: 

    [java] view plain copy

  • package com.test.news.util;   

  • 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 javax.servlet.http.HttpSession;   

  • public class AccessFilter implements Filter {   

  • /**  

  • * @author chaoyin  

  • */   

  • public void destroy() {   

  • }   

  • public void doFilter(ServletRequest arg0, ServletResponse arg1,   

  • FilterChain filterChain) throws IOException, ServletException {   

  • HttpServletRequest request = (HttpServletRequest)arg0;   

  • HttpServletResponse response = (HttpServletResponse)arg1;   

  • HttpSession session = request.getSession();   

  • if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){   

  • response.sendRedirect("login.jsp");   

  • return ;   

  • }   

  • filterChain.doFilter(arg0, arg1);   

  • }   

  • public void init(FilterConfig arg0) throws ServletException {   

  • }   

  • }     

Storm代理
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I... 点击进入详情页
本回答由Storm代理提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式