java intercepter_Java之Interceptor和Filter

最近研究了下Spring的HandlerInterceptor和Java的Filter,因为经常搞混它们两个,今天整理个笔记记录一下。

HandlerInterceptor 是Spring里面的拦截器

Filter是Java里面的过滤器

共同点 还是贴下Java里面的注释吧,解释还是很到位的:

*

A HandlerInterceptor gets called before the appropriate HandlerAdapter

* triggers the execution of the handler itself. This mechanism can be used

* for a large field of preprocessing aspects, e.g. for authorization checks,

* or common handler behavior like locale or theme changes. Its main purpose

* is to allow for factoring out repetitive handler code.

*

HandlerInterceptor is basically similar to a Servlet 2.3 Filter, but in

* contrast to the latter it just allows custom pre-processing with the option

* of prohibiting the execution of the handler itself, and custom post-processing.

* Filters are more powerful, for example they allow for exchanging the request

* and response objects that are handed down the chain. Note that a filter

* gets configured in web.xml, a HandlerInterceptor in the application context.

*

*

As a basic guideline, fine-grained handler-related preprocessing tasks are

* candidates for HandlerInterceptor implementations, especially factored-out

* common handler code and authorization checks. On the other hand, a Filter

* is well-suited for request content and view content handling, like multipart

* forms and GZIP compression. This typically shows when one needs to map the

* filter to certain content types (e.g. images), or to all requests.

解释下吧:

HandlerInteceptor一般用于权限验证,以及一些处理风格本地化等公共代码。

Filter一般用于修改请求内容和界面的解析处理相关。

Inteceptor的三个方法:

//做实际的请求之前调用

//返回true会接着链式调用

//返回false终止链式调用

boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

throws Exception;

//请求之后,解析视图界面之前调用

void postHandle(

HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)

throws Exception;

//解析完界面之后调用

void afterCompletion(

HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)

throws Exception;

// 在mvc-dispatcher-servlet.xml或者applicationContext.xml中配置

Filter配置

@Component

public class RequestLogFilter implements Filter{

@Override

public void destroy() {

System.out.println("destory");

}

@Override

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

// TODO Auto-generated method stub

System.out.println("doFilter");

chain.doFilter(request , response);

}

@Override

public void init(FilterConfig filterConfig) throws ServletException {

// TODO Auto-generated method stub

System.out.println("init");

}

}

// web.xml配置

testFilter

com.abcd.system.RequestLogFilter

testFilter

/*

测试结果顺序:发现filter会在前面处理

init

doFilter

preHandle

postHandle

afterCompletion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值