FilterConfig
的物件由Web容器建立。這個物件可用於獲取web.xml
檔案中的組態資訊。
FilterConfig介面的方法
FilterConfig
介面中有以下4
個方法。
public void init(FilterConfig config)
: init()
方法僅在初始化過濾器時被呼叫(只呼叫一次)。public String getInitParameter(String parameterName)
: 返回指定引數名稱的引數值。public java.util.Enumeration getInitParameterNames()
: 返回包含所有引數名稱的列舉。public ServletContext getServletContext()
: 返回ServletContext
物件。在此範例中,如果將web.xml的中的construction
引數值為no
,則請求將轉發到servlet,如果將引數值設定為:yes
,則過濾器將使用訊息建立響應:」此頁面正在處理中「。下面來看看FilterConfig
的簡單例子。
開啟Eclipse,建立一個動態Web專案:FilterConfig,其完整的目錄結構如下所示 -
以下是這個專案中的幾個主要的程式碼檔案。在這裡建立了4
個檔案:
下面是這幾個檔案的具體程式碼。
檔案:index.html -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>FilterConfig組態應用範例</title>
</head>
<body>
<div style="text-align: center;">
<a href="servlet1">檢視Filter組態資訊</a>
</div>
</body>
</html>
檔案:MyFilter.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
public class MyFilter implements Filter {
FilterConfig config;
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
req.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
String s = config.getInitParameter("construction");
if (s.equals("yes")) {
out.print("此頁面正在處理中");
} else {
chain.doFilter(req, resp);// sends request to next resource
}
}
public void destroy() {
}
}
檔案:HelloServlet.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("<br>welcome to servlet<br>");
}
}
檔案:web.xml -
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>FilterConfig</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.yiibai.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<filter>
<filter-name>f1</filter-name>
<filter-class>com.yiibai.MyFilter</filter-class>
<init-param>
<param-name>construction</param-name>
<param-value>yes</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/servlet1</url-pattern>
</filter-mapping>
</web-app>
在編寫上面程式碼後,部署此Web應用程式(在專案名稱上點選右鍵->」Run On Server…」),開啟瀏覽器存取URL: http://localhost:8080/FilterConfig/ ,如果沒有錯誤,應該會看到以下結果 -
點選頁面中的連結,應該會看到以下結果 -