1.前沿上一篇文章提到在web.xml中各個元素的執行順序是這樣的,context-param-->listener-->filter-->servlet; 而攔截器是在Spring MVC中配置的,若是從整個項目中看,一個servlet請求的執行過程就變成了這樣context-param-->listener-->filter-->servlet-->interceptor(指的是攔截器),爲何攔截器是在servlet執行以後,由於攔截器自己就是在servlet內部的,下面把所學和所總結的用本身的描述整理出來~。另外本文的項目框架是基於上篇文章http://blog.csdn.net/Jintao_Ma/article/details/52892625 講述的框架,下載路徑:http://download.csdn.net/download/jintao_ma/9661038。javascript
2.概念html
context-param:就是一些須要初始化的配置,放入context-param中,從而被監聽器(這裏特指org.springframework.web.context.ContextLoaderListener)監聽,而後加載;java
監聽器(listener):就是對項目起到監聽的做用,它能感知到包括request(請求域),session(會話域)和applicaiton(應用程序)的初始化和屬性的變化;jquery
過濾器(filter):就是對請求起到過濾的做用,它在監聽器以後,做用在servlet以前,對請求進行過濾;web
servlet:就是對request和response進行處理的容器,它在filter以後執行,servlet其中的一部分就是controller層(標記爲servlet_2),還包括渲染視圖層(標記爲servlet_3)和進入controller以前系統的一些處理部分(servlet_1),另外咱們把servlet開始的時刻標記爲servlet_0,servlet結束的時刻標記爲servlet_4。spring
攔截器(interceptor):就是對請求和返回進行攔截,它做用在servlet的內部,具體來講有三個地方:apache
1)servlet_1和servlet_2之間,即請求尚未到controller層api
2)servlet_2和servlet_3之間,即請求走出controller層次,尚未到渲染時圖層tomcat
3)servlet_3和servlet_4之間,即結束視圖渲染,可是尚未到servlet的結束session
它們之間的關係,能夠用一張圖來表示:
3.使用原則
對整個流程清楚以後,而後就是各自的使用,在使用以前應該有一個使用規則,爲何這個說,由於有些功能好比判斷用戶是否登陸,既能夠用過濾器,也能夠用攔截器,用哪個纔是合理的呢?那麼若是有一個原則,使用起來就會更加合理。實際上這個原則是有的:
把整個項目的流程比做一條河,那麼監聽器的做用就是可以聽到河流裏的全部聲音,過濾器就是可以過濾出其中的魚,而攔截器則是攔截其中的部分魚,而且做標記。因此當須要監聽到項目中的一些信息,而且不須要對流程作更改時,用監聽器;當須要過濾掉其中的部分信息,只留一部分時,就用過濾器;當須要對其流程進行更改,作相關的記錄時用攔截器。下面是具體的使用案例
本文涉及到的jsp頁面:
index.jsp:
- <%@ page language="java" import="com.mycompany.mvc.listener.*" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>index.jsp</title>
- </head>
- <body>
- this is index jsp
-
- <a href="/myWebApp/system/login">login</a>
- <br></br>
- 測試servletcontext:
- <%
- application.setAttribute("app","app");
- application.getAttribute("app");
- application.removeAttribute("app");
- %>
- <br></br>
- 測試httpsession:
- <%
- session.setAttribute("app3","app3");
- session.getAttribute("app3");
- session.removeAttribute("app3");
- %>
- <br></br>
- 測試servletrequest:
- <%
- request.setAttribute("app3","app3");
- request.getAttribute("app3");
- request.removeAttribute("app3");
- %>
- <br></br>
- 當前在線人數:
- <%=session.getAttribute("peopleOnLine")%>
- <br></br>
- HttpSessionBindingListener測試:
- <%
- session.setAttribute("bean",new myHttpSessionBindingListener());
- session.removeAttribute("bean");
- %>
- </body>
- </html>
login.jsp:
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>main.jsp</title>
- </head>
-
- <c:set var="ctx" value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}" />
- <script type="text/javascript" src="${ctx}/plugins/jquery-3.0.0/jquery-3.0.0.js"></script>
- <script type="text/javascript">
- </script>
-
- <body>
- This is has login jsp
- <a href="/myWebApp/system/view">view</a>
- </body>
- </html>
view.jsp:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>view jsp</title>
- </head>
- <body>
- 用戶已經登錄,歡迎來到登錄後系統主界面
- </body>
- </html>
4.監聽器
4.1listener具體分爲八種,可以監聽包括request域,session域,application域的產生,銷燬和屬性的變化;
具體使用,能夠看以前轉載一篇文章(再次感謝"孤傲蒼狼",他的主頁http://www.cnblogs.com/xdp-gacl/):http://blog.csdn.net/Jintao_Ma/article/details/51464124
在配置完而後咱們在web.xml中諸以下面的配置便可:
- <listener> <listener-class>
- com.mycompany.mvc.listener.myServletContextListener
- </listener-class>
- </listener>
- <listener>
- <listener-class>
- com.mycompany.mvc.listener.myServletContextAttributeListener
- </listener-class>
- </listener>
4.2 listener實際應用
4.2.1 獲取當前在線人數
- package com.mycompany.mvc.listener;
-
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
-
- public class myHttpSessionListener implements HttpSessionListener{
-
- public static int peopleOnLine = 0;
-
- @Override
- public void sessionCreated(HttpSessionEvent arg0) {
- System.out.println("myHttpSessionListener.sessionCreated():"+arg0);
- peopleOnLine++;
- arg0.getSession().setAttribute("peopleOnLine",peopleOnLine);
- }
-
- @Override
- public void sessionDestroyed(HttpSessionEvent arg0) {
- System.out.println("myHttpSessionListener.sessionDestroyed():"+arg0);
- peopleOnLine--;
- arg0.getSession().setAttribute("peopleOnLine",peopleOnLine);
- }
- }
在頁面中就能夠獲取:
- 當前在線人數:
- <%=session.getAttribute("peopleOnLine")%>
其實也能夠得到歷史全部在線人數,只須要把歷史全部在線人數保存在文件中,而後每次項目啓動讀取這個文件,當前人數增長時,把歷史全部人數也相應增長,項目關閉時,再保存起來。
4.2.2 在系統初始化時,獲取項目絕對路徑
以下,得到絕對路徑後保存到系統變量System中:
- @Override
- public void contextInitialized(ServletContextEvent servletContext) {
- System.out.println("myServletContextListener.contextInitialized()");
- System.setProperty("realPath", servletContext.getServletContext().getRealPath("/"));
- System.out.println("myServletContextListener.contextInitialized()");
- }
5.過濾器(filter)
5.1過濾器只須要繼承javax.servlet.filter便可,通常來講咱們只要添加tomcat運行時環境就可以包含javax.servlet的jar包,可是eclipse在tomcat8中沒有找到,實際上tomcat8中確實沒有,只有經過maven來添加了:
-
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- </dependency>
-
-
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.0</version>
- </dependency>
5.2 filter的實際應用
5.2.1 請求編碼轉換
- package com.mycompany.mvc.filter;
-
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- public class urlEncodeFilter implements Filter{
-
- Logger logger = LoggerFactory.getLogger(urlEncodeFilter.class);
- Map<String,Object> paramMap = new HashMap<String,Object>();
-
- @Override
- public void destroy() {
- }
-
- @Override
- public void doFilter(ServletRequest arg0, ServletResponse arg1,
- FilterChain arg2) throws IOException, ServletException {
- System.out.println("urlEncodeFilter doFilter..."+paramMap.get("urlEncode").toString());
- arg0.setCharacterEncoding(paramMap.get("urlEncode").toString());
- arg2.doFilter(arg0, arg1);
- }
-
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- String urlEncode = arg0.getInitParameter("urlEncode");
- paramMap.put("urlEncode",urlEncode);
- }
-
- }
web.xml配置:
- <filter>
- <filter-name>urlEncodeFilter</filter-name>
- <filter-class>com.mycompany.mvc.filter.urlEncodeFilter</filter-class>
- <init-param>
- <param-name>urlEncode</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>urlEncodeFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
5.2.2 日誌記錄,好比記錄全部對網站發起請求的地址
- package com.mycompany.mvc.filter;
-
- import java.io.IOException;
-
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletRequest;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- public class logFilter implements Filter{
-
- Logger logger = LoggerFactory.getLogger(logFilter.class);
-
- @Override
- public void destroy() {
-
- }
-
- @Override
- public void doFilter(ServletRequest arg0, ServletResponse arg1,
- FilterChain arg2) throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest)arg0;
- System.out.println("logFilter doFilter servletPath:"+request.getRemoteHost());
- arg2.doFilter(arg0, arg1);
- }
-
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- }
-
- }
web.xml:
- <filter>
- <filter-name>logFilter</filter-name>
- <filter-class>com.mycompany.mvc.filter.logFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>logFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
5.2.3 對未登錄用戶的判斷
- package com.mycompany.mvc.filter;
-
- import java.io.IOException;
-
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
-
- import org.apache.commons.lang.StringUtils;
-
- import com.mycompany.mvc.utils.Constant;
-
- public class loginFilter implements Filter{
-
- private String dispatchUrl = "";
- private String excludeUrl = "";
-
- @Override
- public void destroy() {
-
- }
-
- @Override
- public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
- throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest)arg0;
- String servletPath = request.getServletPath();
-
- HttpSession session = request.getSession();
- String sessionKey = (String) session.getAttribute(Constant.SESSIONKEY);
-
-
- if(servletPath.equals(dispatchUrl) || servletPath.equals(excludeUrl)){
- arg2.doFilter(arg0, arg1);
- }else{
- if(!StringUtils.isEmpty(sessionKey)){
- arg2.doFilter(arg0, arg1);
- }else{
- request.getRequestDispatcher(dispatchUrl).forward(arg0, arg1);
- }
- }
- }
-
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- dispatchUrl = arg0.getInitParameter("dispatchUrl");
- excludeUrl = arg0.getInitParameter("excludeUrl");
- }
-
- }
web.xml:
- <filter>
- <filter-name>loginFilter</filter-name>
- <filter-class>com.mycompany.mvc.filter.loginFilter</filter-class>
- <init-param>
-
- <param-name>excludeUrl</param-name>
- <param-value>/main</param-value>
- </init-param>
- <init-param>
-
- <param-name>dispatchUrl</param-name>
- <param-value>/system/login</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>loginFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
之因此上面的/main可以直接跳轉到index這個登錄界面,是由於SpringMvc中配置了這個(上篇文章有講述到):
<mvc:view-controller path="${adminPath}" view-name="index"/>
它的意思就是不通過controller層,直接把index放入ModelAndView,而後由渲染層進行渲染。 講到這裏,再結合上面說到的攔截器,咱們發現,這個時候攔截器仍是可以攔截2次的,就是視圖渲染前和渲染後,可是進入controller層以前確定攔截不到了,由於請求根本就沒有進入controller。
systemAction:
- package com.mycompany.system.controller;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
-
- @Controller
- @RequestMapping("/system")
- public class systemAction {
-
- @RequestMapping("/login")
- public ModelAndView login(HttpServletRequest request){
- ModelAndView mv = new ModelAndView();
- HttpSession session = request.getSession();
-
-
- session.setAttribute("sessionKey","test");
- mv.setViewName("login");
- return mv;
- }
-
- @RequestMapping("/view")
- public ModelAndView view(HttpServletRequest request){
- ModelAndView mv = new ModelAndView();
- mv.setViewName("view");
- return mv;
- }
-
- }
Constant.java:
- package com.mycompany.mvc.utils;
-
- public class Constant {
-
- public static final String SESSIONKEY = "sessionKey";
-
- }
6.攔截器(interceptor)
6.1 攔截器這個要詳細講述一下了,上一篇文章說到,Spring的配置文件應該掃描service層及如下,SpringMvc的配置文件應該掃描controller層; 咱們在service層若是想作日誌的話,可使用spring aop特性,在spring.xml中配置aspect便可,那麼若是想在controller層作日誌,相應地,在SpringMvc.xml中應該怎麼配置呢?
這個時候就須要攔截器,它其實也是一種aop的實現(aop自己是一種思想),並且這種實現本質上和aspect是同樣的,只是作了更多的事情,咱們固然能夠在SpringMvc.xml中也配置aspect,不過如今有一個更好的實現,爲何不用呢。
關於攔截器細節,能夠參考這篇文章:http://elim.iteye.com/blog/1750680
6.2 攔截器的實際應用
6.2.1 能夠全局作日誌
- package com.mycompany.mvc.interceptor;
-
- import java.lang.reflect.Method;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.method.HandlerMethod;
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
-
-
- public class logInterceptor implements HandlerInterceptor{
-
- @Override
- public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
- throws Exception {
-
- }
-
- @Override
- public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
- throws Exception {
- System.out.println("logInterceptor.postHandle()---view Name:"+arg3.getViewName());
- }
-
- @Override
- public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
-
- if(arg2 instanceof HandlerMethod){
- HandlerMethod hMethod = (HandlerMethod)arg2;
- Method method = hMethod.getMethod();
- System.out.println("logInterceptor.preHandle()--method Name:"+method.getName());
- }
- return true;
- }
-
- }
6.2.2 記錄部分調用的時間
- package com.mycompany.mvc.interceptor;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
-
-
- public class timeInterceptor implements HandlerInterceptor{
-
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
- throws Exception {
- return true;
- }
-
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
- ModelAndView modelAndView) throws Exception {
- System.out.println("timeInterceptor.postHandle()--time:"+System.currentTimeMillis());
- }
-
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
- throws Exception {
- }
-
- }
上述兩個攔截器功能的配置以下,SpringMvc.xml:
-
- <mvc:interceptors>
- <bean class="com.mycompany.mvc.interceptor.logInterceptor"></bean>
- <mvc:interceptor>
- <mvc:mapping path="/system/view"/>
- <bean class="com.mycompany.mvc.interceptor.timeInterceptor"></bean>
- </mvc:interceptor>
- </mvc:interceptors>
from:https://blog.csdn.net/smxjant/article/details/78912820
更詳細的web.xml解釋,請參考:
https://blog.csdn.net/ahou2468/article/details/79015251