1. 程式人生 > >java中listener、filter、interceptor作用和區別

java中listener、filter、interceptor作用和區別

原文出自https://blog.csdn.net/Jintao_Ma/article/details/52972482

JavaWeb中監聽器+過濾器+攔截器區別、配置和實際應用

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

2.概念

context-param:就是一些需要初始化的配置,放入context-param中,從而被監聽器(這裡特指org.springframework.web.context.ContextLoaderListener)監聽,然後載入;

監聽器(listener):就是對專案起到監聽的作用,它能感知到包括request(請求域),session(會話域)和applicaiton(應用程式)的初始化和屬性的變化;

過濾器(filter):就是對請求起到過濾的作用,它在監聽器之後,作用在servlet之前,對請求進行過濾;

servlet:就是對request和response進行處理的容器,它在filter之後執行,servlet其中的一部分就是controller層(標記為servlet_2),還包括渲染檢視層(標記為servlet_3)和進入controller之前系統的一些處理部分(servlet_1),另外我們把servlet開始的時刻標記為servlet_0,servlet結束的時刻標記為servlet_4。

攔截器(interceptor):就是對請求和返回進行攔截,它作用在servlet的內部,具體來說有三個地方:

1)servlet_1和servlet_2之間,即請求還沒有到controller層

2)servlet_2和servlet_3之間,即請求走出controller層次,還沒有到渲染時圖層

3)servlet_3和servlet_4之間,即結束檢視渲染,但是還沒有到servlet的結束

它們之間的關係,可以用一張圖來表示:

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來添加了:

<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>servlet-api</artifactId>
	    <version>2.5</version>
	</dependency>
 
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
	<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>
<!-- 		不進行過濾的url,因為它就是跳轉到登陸介面, -->
		<param-name>excludeUrl</param-name>
		<param-value>/main</param-value>
	</init-param>
	<init-param>
<!-- 		未登入使用者跳轉的url -->
		<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();
		/*假設使用者輸入的使用者名稱密碼正確,則放入sessionKey中,對應的value可以
		 * 是User物件,這裡以字串"test"代替*/
		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;
/**@Description
 * logInterceptor公共攔截器,做日誌記錄
 */
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 {
		/*取得呼叫的controller方法等*/
		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;
/**@Description
 * 登陸時間攔截器,記錄使用者登入時間
 * */
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>