1. 什麼是過濾器html
過濾器是一個運行在服務器端的程序,先於與之相關的servlet或JSP頁面以前運行實現對請求資源的過濾的功能;前端
過濾器可附加到一個或多個servlet或jsp頁面上,能夠檢查請求信息也能夠處理相應信息;java
filter的基本功能是對Servlet容器調用Servlet的過程進行攔截,從而在Servlet執行先後實現一些特殊的功能。web
如圖,瀏覽器發出的請求先遞交給第一個filter進行過濾,符合規則則放行,遞交給filter鏈中的下一個過濾器進行過濾。過濾器在鏈中的順序與它在web.xml中配置的順序有關,配置在前的則位於鏈的前端。當請求經過了鏈中全部過濾器後就能夠訪問資源文件了,若是不能經過,則可能在中間某個過濾器中被處理掉。瀏覽器
在doFilter()方法中,chain.doFilter()前的通常是對request執行的過濾操做,chain.doFilter後面的代碼通常是對response執行的操做。過濾鏈代碼的執行順序以下:服務器
過濾器通常用於登陸權限驗證、資源訪問權限控制、敏感詞彙過濾、字符編碼轉換等等操做,便於代碼重用,沒必要每一個servlet中還要進行相應的操做。app
2. 過濾器的編寫流程dom
1 package com.eagle.web; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class ServletFilter extends HttpServlet { 10 11 private static final long serialVersionUID = 1L; 12 13 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 14 String name = (String) request.getAttribute("name"); 15 System.out.println("servlet獲取request域中的name屬性="+name); 16 response.getWriter().write("hello haohao...name="+name); 17 } 18 19 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 doGet(request, response); 21 } 22 }
1 package com.eagle.filter; 2 3 import java.io.IOException; 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 11 public class Demo1Filter implements Filter { 12 13 public Demo1Filter() { 14 15 } 16 17 public void init(FilterConfig fConfig) throws ServletException { 18 19 } 20 21 public void destroy() { 22 23 } 24 25 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 26 throws IOException, ServletException { 27 request.setAttribute("name", "filter"); 28 System.out.println("servlet以前進行調用,設置request域的name屬性爲filter"); 29 chain.doFilter(request, response); 30 } 31 }
1 <filter> 2 <display-name>Demo1Filter</display-name> 3 <filter-name>Demo1Filter</filter-name> 4 <filter-class>com.eagle.filter.Demo1Filter</filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name>Demo1Filter</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
3. 配置web.xml詳解jsp
<filter>
<filter-name>loginFilter</filter-name> //過濾器名稱
<filter-class>com.x.control.loginFilter</filter-class> //過濾器類的包路徑
<init—param> //可選
<param—name>參數名</param-name> //過濾器初始化參數
<param-value>參數值</param-value>
</init—pamm>
</filter>
<filter-mapping> //過濾器映射
<filter-name>loginFilter</filter-name>
<url-pattern>指定過濾器做用的對象</url-pattern>ide
</filter-mapping>
在配置中須要注意的有兩處:
1:做用於全部web資源:<url—pattern>/*</url-pattern>。則客戶端請求訪問任意資源文件時都要通過過濾器過濾,經過則訪問文件,不然攔截。
2:做用於某一文件夾下全部文件:<url—pattern>/dir/*</url-pattern>
3:做用於某一種類型的文件:<url—pattern>*.擴展名</url-pattern>。好比<url—pattern>*.jsp</url-pattern>過濾全部對jsp文件的訪問請求。
4:做用於某一文件夾下某一類型文件:<url—pattern>/dir/*.擴展名</url-pattern>
若是一個過濾器須要過濾多種文件,則能夠配置多個<filter-mapping>,一個mapping定義一個url-pattern來定義過濾規則。
4. 過濾器實現設置字符編碼問題
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <form action="${ pageContext.request.contextPath }/Login" method="post"> 12 <table border="0px"> 13 <tr> 14 <td>用戶名:</td> 15 <td><input type="text" name="username" id="username" placeholder="請輸入用戶名" /></td> 16 </tr> 17 <tr> 18 <td>密碼:</td> 19 <td><input type="password" name="password" id="password" placeholder="請輸入密碼" /></td> 20 </tr> 21 <tr> 22 <td colspan="2"><input type="submit" value="提交" /></td> 23 </tr> 24 </table> 25 </form> 26 </body> 27 </html>
package cn.eagle.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Login extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username="+username+";password"+password); response.getWriter().write("hello haohao..."+"username="+username+";password"+password+"張三"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
1 package cn.eagle.filter; 2 3 import java.io.IOException; 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 11 public class MyRequestCharacterFilter implements Filter { 12 13 public MyRequestCharacterFilter() { 14 15 } 16 17 public void init(FilterConfig fConfig) throws ServletException { 18 19 } 20 21 public void destroy() { 22 23 } 24 25 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 26 throws IOException, ServletException { 27 request.setCharacterEncoding("utf-8"); 28 response.setCharacterEncoding("utf-8"); 29 response.setContentType("text/html;charset=utf-8"); 30 chain.doFilter(request, response); 31 } 32 }
1 <filter> 2 <display-name>MyRequestCharacterFilter</display-name> 3 <filter-name>MyRequestCharacterFilter</filter-name> 4 <filter-class>cn.eagle.filter.MyRequestCharacterFilter</filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name>MyRequestCharacterFilter</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
5. 解決post和get方式亂碼問題
1 package cn.eagle.domain; 2 3 import java.io.UnsupportedEncodingException; 4 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletRequestWrapper; 7 8 public class MyRequest extends HttpServletRequestWrapper { 9 10 public MyRequest(HttpServletRequest request) { 11 super(request); 12 13 } 14 15 @Override 16 public String getParameter(String name) { 17 18 String value = super.getParameter(name); 19 try { 20 if ("GET".equalsIgnoreCase(this.getMethod())) { 21 value = new String(value.getBytes("ISO8859-1"), "UTF-8"); 22 } 23 } catch (Exception e) { 24 throw new RuntimeException(); 25 } 26 return value; 27 } 28 29 }
1 package cn.eagle.filter; 2 3 import java.io.IOException; 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 cn.eagle.domain.MyRequest; 14 15 public class EncodingFilter implements Filter { 16 17 public EncodingFilter() { 18 19 } 20 21 public void init(FilterConfig fConfig) throws ServletException { 22 23 } 24 25 public void destroy() { 26 27 } 28 29 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 30 throws IOException, ServletException { 31 HttpServletRequest request = (HttpServletRequest) req; 32 HttpServletResponse response = (HttpServletResponse) resp; 33 // 設置編碼 34 request.setCharacterEncoding("utf-8"); 35 response.setCharacterEncoding("utf-8"); 36 response.setContentType("text/html;charset=utf-8"); 37 // 建立自定義request 38 MyRequest myRequest = new MyRequest(request); 39 chain.doFilter(myRequest, response); 40 } 41 }
1 package cn.eagle.web; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class Encoding extends HttpServlet { 10 11 private static final long serialVersionUID = 1L; 12 13 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 14 String username = request.getParameter("username"); 15 System.out.println(username); 16 response.getWriter().write("hello haohao..."+username); 17 } 18 19 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 doGet(request, response); 21 } 22 }