java 过滤器(Filter)与springMVC 拦截器(interceptor)的实现案例

java 过滤器Filter:

package com.sun.test.aircraft.filter;
import javax.servlet.*;
import java.io.IOException;
public class SunnyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init success");
System.out.println("FilterName:" + filterConfig.getFilterName() + " " + "InitParameterNames:" + filterConfig.getInitParameterNames() + " " + "ServletContext:" + filterConfig.getServletContext());
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("start filter");
chain.doFilter(request, response);
}
@Override
public void destroy() {
System.out.println("destroy enheng!");
}
} web.xml配置:
<filter>
<filter-name>sunnyFilter</filter-name>
<filter-class>com.sun.test.aircraft.filter.SunnyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sunnyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
结果:

java 过滤器(Filter)与springMVC 拦截器(interceptor)的实现案例

java 过滤器(Filter)与springMVC 拦截器(interceptor)的实现案例

=====================================================================================================================

springMVC 拦截器interceptor:
package com.sun.test.aircraft.intercept;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class SunnyIntercept implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("interceptor before working");
return true;
} @Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("interceptor working");
String str="interceptor working";
modelAndView.addObject("incept",str);
} @Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("interceptor worked done");
}
}
spring-mvc.xml配置
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello/**"/>
<bean class="com.sun.test.aircraft.intercept.SunnyIntercept"/>
</mvc:interceptor>
</mvc:interceptors>
package com.sun.test.aircraft.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; controller实现(可以理解为实现的do)
@Controller
@RequestMapping("/hello")
public class HelloController { @RequestMapping("/u")
public void hello() {
System.out.println("hello you");
}
}
结果:

java 过滤器(Filter)与springMVC 拦截器(interceptor)的实现案例

一句话:过滤器:“取你所想取,过滤的是链接,关注的是web请求;拦截器:“拒你所想拒”关注的是业务逻辑{业务逻辑之前,业务逻辑正在,业务逻辑之后}所处理的事情

上一篇:.Net及C#基础知识,面试宝典


下一篇:OPENSSL编程 第二十章 椭圆曲线