Struts2中过滤器和拦截器的区别

时间:2023-03-09 13:27:26
Struts2中过滤器和拦截器的区别

拦截器和过滤器的区别:

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

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

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

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

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

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

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

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

  1. <package name="newsDemo" extends="struts-default"
  2. namespace="/admin">
  3. <interceptors>
  4. <interceptor name="auth" class="com.test.news.util.AccessInterceptor" />
  5. <interceptor-stack name="authStack">
  6. <interceptor-ref name="auth" />
  7. </interceptor-stack>
  8. </interceptors>
  9. <!-- action -->
  10. <action name="newsAdminView!*" class="newsAction"
  11. method="{1}">
  12. <interceptor-ref name="defaultStack"/>
  13. <interceptor-ref name="authStack">
  14. </interceptor-ref>

下面是我实现的Interceptor class:

  1. package com.test.news.util;
  2. import java.util.Map;
  3. import com.opensymphony.xwork2.ActionContext;
  4. import com.opensymphony.xwork2.ActionInvocation;
  5. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  6. import com.test.news.action.AdminLoginAction;
  7. /**
  8. * @author chaoyin
  9. */
  10. public class AccessInterceptor extends AbstractInterceptor {
  11. private static final long serialVersionUID = -4291195782860785705L;
  12. @Override
  13. public String intercept(ActionInvocation actionInvocation) throws Exception {
  14. ActionContext actionContext = actionInvocation.getInvocationContext();
  15. Map session = actionContext.getSession();
  16. //except login action
  17. Object action = actionInvocation.getAction();
  18. if (action instanceof AdminLoginAction) {
  19. return actionInvocation.invoke();
  20. }
  21. //check session
  22. if(session.get("user")==null ){
  23. return "logout";
  24. }
  25. return actionInvocation.invoke();//go on
  26. }
  27. }

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

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

  1. <filter>
  2. <filter-name>access filter</filter-name>
  3. <filter-class>
  4. com.test.news.util.AccessFilter
  5. </filter-class>
  6. </filter>
  7. <filter-mapping>
  8. <filter-name>access filter</filter-name>
  9. <url-pattern>/admin/*</url-pattern>
  10. </filter-mapping>

下面是过滤的实现类:

    1. package com.test.news.util;
    2. import java.io.IOException;
    3. import javax.servlet.Filter;
    4. import javax.servlet.FilterChain;
    5. import javax.servlet.FilterConfig;
    6. import javax.servlet.ServletException;
    7. import javax.servlet.ServletRequest;
    8. import javax.servlet.ServletResponse;
    9. import javax.servlet.http.HttpServletRequest;
    10. import javax.servlet.http.HttpServletResponse;
    11. import javax.servlet.http.HttpSession;
    12. public class AccessFilter implements Filter {
    13. /**
    14. * @author chaoyin
    15. */
    16. public void destroy() {
    17. }
    18. public void doFilter(ServletRequest arg0, ServletResponse arg1,
    19. FilterChain filterChain) throws IOException, ServletException {
    20. HttpServletRequest request = (HttpServletRequest)arg0;
    21. HttpServletResponse response = (HttpServletResponse)arg1;
    22. HttpSession session = request.getSession();
    23. if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){
    24. response.sendRedirect("login.jsp");
    25. return ;
    26. }
    27. filterChain.doFilter(arg0, arg1);
    28. }
    29. public void init(FilterConfig arg0) throws ServletException {
    30. }
    31. }