1、過濾器(Filter):又稱攔截器。實現Filter接口的類咱們稱之爲Filter(過濾器或攔截器),Filter能夠對用戶訪問的資源進行攔截。例如:客戶端發送請求是,先將請求攔截下來,判斷用戶是否已登陸,若是已登陸則放行,反之則進入登陸頁面。html
2、攔截器的使用:java
a)、在Filter裏面能夠用request得到請求消息,用response寫入響應消息
b)、chain.doFilter(request, response) 方法放行 目標Servlet使用的是同一個請求和響應
c)、doFilter 方法後面的代碼會執行,在目標Servlet 返回響應後執行, 也能夠使用同一個請求和響應web
3、攔截器的實現過程:服務器
a)、寫一個類實現 Filter 接口 , 在doFilter 方法中寫功能代碼app
1 package com.st.filter; 2 import java.io.IOException; 3 4 import javax.servlet.Filter; 5 import javax.servlet.FilterChain; 6 import javax.servlet.FilterConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.ServletRequest; 9 import javax.servlet.ServletResponse; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.st.bean.UserBean; 14 public class FilterTest implements Filter { 15 @Override 16 public void destroy() { 17 // 生命週期結束,服務器中止時執行此方法 18 } 19 @Override 20 public void doFilter(ServletRequest arg0, ServletResponse arg1, 21 FilterChain arg2) throws IOException, ServletException { 22 // TODO Auto-generated method stub 23 // 請求過濾器攔截的路徑時執行此方法 24 HttpServletRequest request = (HttpServletRequest) arg0; 25 HttpServletResponse response = (HttpServletResponse) arg1; 26 //下面三句能夠很好的解決亂碼問題,須要注意的是這三句儘可能放在最上面 27 request.setCharacterEncoding("utf-8"); 28 response.setCharacterEncoding("utf-8"); 29 response.setContentType("text/html;charset=UTF-8"); 30 31 String path = request.getServletPath(); 32 if(path.endsWith("LoginServlet.do")){ 33 arg2.doFilter(request, response);// 若是是以LoginServlet.do結尾則放行,執行LoginServlet 34 } 35 else{ 36 UserBean user =(UserBean)request.getSession().getAttribute("USER_INFO"); 37 if(user !=null) //用戶已經登陸,實現放行操做 38 arg2.doFilter(request, response); 39 else //用戶沒有登陸,不放行,跳轉到登陸頁面 40 request.getRequestDispatcher("/login.jsp").include(request, response); 41 } 42 } 43 @Override 44 public void init(FilterConfig arg0) throws ServletException { 45 // 生命週期開始,服務器啓動後就會執行此方法 46 } 47 }
b)、在web.xml中配置Filter攔截的資源路徑jsp
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 7 <display-name></display-name> 8 <servlet> 9 <description>This is the description of my J2EE component</description> 10 <display-name>This is the display name of my J2EE component</display-name> 11 <servlet-name>FilterService</servlet-name> 12 <servlet-class>com.st.service.FilterService</servlet-class> 13 </servlet> 14 <servlet> 15 <description>This is the description of my J2EE component</description> 16 <display-name>This is the display name of my J2EE component</display-name> 17 <servlet-name>LoginServlet</servlet-name> 18 <servlet-class>com.st.service.LoginServlet</servlet-class> 19 </servlet> 20 <filter> 21 <filter-name>MyFilter</filter-name> 22 <filter-class>com.st.filter.FilterTest</filter-class> 23 </filter> 24 <filter-mapping> 25 <filter-name>MyFilter</filter-name> 26 <url-pattern>*.do</url-pattern> 27 </filter-mapping> 28 29 <servlet-mapping> 30 <servlet-name>FilterService</servlet-name> 31 <url-pattern>/FilterService.do</url-pattern> 32 </servlet-mapping> 33 <servlet-mapping> 34 <servlet-name>LoginServlet</servlet-name> 35 <url-pattern>/LoginServlet.do</url-pattern> 36 </servlet-mapping> 37 <welcome-file-list> 38 <welcome-file>index.jsp</welcome-file> 39 </welcome-file-list> 40 </web-app>