1、拦截器的使用
1)拦截器接口
实现了Spring框架的HandlerInterceptor接口,HandlerInterceptor采用了JDK8的接口定义,可以使得不需要每个方法都去实现
2)方法的调用顺序
3)Java代码
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("调用preHandle方法"+handler);
String loginName = (String)request.getSession().getAttribute("loginName");
if (loginName == null || !"高云".equals(loginName)){
request.getSession().setAttribute("error","没有登录!");
request.getRequestDispatcher("error.jsp").forward(request,response);
return false;
}
String name = request.getParameter("name");
Integer age = Integer.parseInt(request.getParameter("age"));
if (name != null ){
//张三不能登录
if ("zs".equals(name)){
request.setAttribute("error","张三不能登录!");
request.getRequestDispatcher("error.jsp").forward(request,response);
return false;
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("调用postHandle方法,这个方法是在Controller方法执行之后,执行");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("调用afterCompletion方法,该方法在请求处理完成后,做最后的处理,可以用来清理缓存什么的");
}
}
4)dispatcherServlet.xml文件配置
<mvc:interceptors>
<mvc:interceptor>
<!--拦截请求地址-->
<mvc:mapping path="/showMessage.do"/>
<!--拦截器类所在包-->
<bean class="com.bjpowernode.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
2、多个拦截器的使用规则
1)执行顺序
2)dispatcherServlet.xml配置文件
<mvc:interceptors>
<mvc:interceptor>
<!--拦截请求地址-->
<mvc:mapping path="/showMessage.do"/>
<!--拦截器类所在类-->
<bean class="com.bjpowernode.interceptor.MyInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<!--拦截请求地址2-->
<mvc:mapping path="/showMessage.do"/>
<!--拦截器类所在类2-->
<bean class="com.bjpowernode.interceptor.MyInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors>
3)类以及输出结果
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("1111111111调用preHandle方法"+handler);
String loginName = (String)request.getSession().getAttribute("loginName");
if (loginName == null || !"高云".equals(loginName)){
request.getSession().setAttribute("error","没有登录!");
request.getRequestDispatcher("error.jsp").forward(request,response);
return false;
}
String name = request.getParameter("name");
Integer age = Integer.parseInt(request.getParameter("age"));
if (name != null ){
//张三不能登录
if ("zs".equals(name)){
request.setAttribute("error","张三不能登录!");
request.getRequestDispatcher("error.jsp").forward(request,response);
return false;
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("11111111调用postHandle方法,这个方法是在Controller方法执行之后,执行");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("111111111调用afterCompletion方法,该方法在请求处理完成后,做最后的处理,可以用来清理缓存什么的");
}
}
***************************************************************************************************************************************************
public class MyInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("22222222222调用preHandle方法"+handler);
String loginName = (String)request.getSession().getAttribute("loginName");
if (loginName == null || !"高云".equals(loginName)){
request.getSession().setAttribute("error","没有登录!");
request.getRequestDispatcher("error.jsp").forward(request,response);
return false;
}
String name = request.getParameter("name");
Integer age = Integer.parseInt(request.getParameter("age"));
if (name != null ){
//张三不能登录
if ("zs".equals(name)){
request.setAttribute("error","张三不能登录!");
request.getRequestDispatcher("error.jsp").forward(request,response);
return false;
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("222222222222调用postHandle方法,这个方法是在Controller方法执行之后,执行");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("22222222222调用afterCompletion方法,该方法在请求处理完成后,做最后的处理,可以用来清理缓存什么的");
}
}
4)输出结果
3、拦截器和过滤器的区别
1)过滤器是servlet中的对象, 拦截器是框架中的对象
2)过滤器实现Filter接口的对象, 拦截器是实现HandlerInterceptor
3)过滤器是用来设置request,response的参数,属性的,侧重对数据过滤的。
拦截器是用来验证请求的,能截断请求。
4)过滤器是在拦截器之前先执行的。
5)过滤器是tomcat服务器创建的对象
拦截器是springmvc容器中创建的对象
6)过滤器是一个执行时间点。
拦截器有三个执行时间点
7)过滤器可以处理jsp,js,html等等
拦截器是侧重拦截对Controller的对象。 如果你的请求不能被DispatcherServlet接收, 这个请求不会执行拦截器内容
8)拦截器拦截普通类方法执行,过滤器过滤servlet请求响应