1. 拦截器:
interceptor
过滤器(filter)与拦截器(intercepter)相同点:
1) 都可以拦截请求,过滤请求
2) 都是应用了过滤器(责任链)设计模式
2.区别:
1) filter是范围比较大,配置web.xml中
2) intercepter范围比较小,配置在springmvc中
3) 在进入springmvc处理之前,要先处理web.xml的
springmvc-servlet.txml
1 <bean id="localeResolver" 2 class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean> 3 <mvc:interceptors> 4 <!--<bean class="com.etc.interceptor.LoginInterceptor.etc.interceptor.MyInterceptor"></bean>--> 5 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean> 6 <mvc:interceptor> 7 <mvc:mapping path="/**"/> 8 <mvc:exclude-mapping path="/login"/> 9 <bean class="com.etc.interceptor.LoginInterceptor"/> 10 </mvc:interceptor> 11 </mvc:interceptors>
web.xml
1 <filter> 2 <filter-name>loginFilter</filter-name> 3 <filter-class>com.etc.fileter.MyFileter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>loginFilter</filter-name> 7 <url-pattern>/</url-pattern> 8 </filter-mapping>
LoginInterceptor:
1 package com.etc.interceptor; 2 3 import org.springframework.web.servlet.HandlerInterceptor; 4 import org.springframework.web.servlet.ModelAndView; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 10 public class LoginInterceptor implements HandlerInterceptor { 11 @Override 12 public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { 13 HttpSession session=httpServletRequest.getSession(); 14 if(session.getAttribute("login")==null){ 15 httpServletResponse.sendRedirect("/login"); 16 return false; 17 }else { 18 System.out.println("ip:"+httpServletRequest.getRemoteHost()+"url"+httpServletRequest.getRequestURL()) ; 19 return true; 20 } 21 } 22 23 @Override 24 public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { 25 26 } 27 28 @Override 29 public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { 30 31 } 32 }
MyFileter:
1 package com.etc.fileter; 2 3 import javax.servlet.*; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 import javax.servlet.http.HttpSession; 7 import java.io.IOException; 8 9 public class MyFileter implements Filter { 10 @Override 11 public void init(FilterConfig filterConfig) throws ServletException { 12 13 } 14 15 @Override 16 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 17 // 实际 18 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; 19 HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; 20 21 // 如果调用了这个方法,过滤器就会继承往下执行, 22 HttpSession session = httpServletRequest.getSession(); 23 if (session.getAttribute("login") == null) { 24 //没有登陆 25 httpServletResponse.sendRedirect("/login"); 26 //return false; 27 } else { 28 System.out.println("ip:" + httpServletRequest.getRemoteHost() + ",url:" + httpServletRequest.getRequestURL()); 29 filterChain.doFilter(servletRequest, servletResponse); 30 //return true; 31 } 32 33 } 34 35 @Override 36 public void destroy() { 37 38 } 39 }