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
對象。
FilterConfig示例
在此示例中,如果將web.xml的中的construction
參數值為no
,則請求將轉發到servlet,如果將參數值設置為:yes
,則篩檢程式將使用消息創建回應:”此頁面正在處理中“。下麵來看看FilterConfig
的簡單例子。
打開Eclipse,創建一個動態Web專案:FilterConfig,其完整的目錄結構如下所示 -
以下是這個專案中的幾個主要的代碼檔。在這裏創建了4
個檔:
- index.html - 應用程式入口
- MyFilter.java - 篩檢程式實現
- HelloServlet.java - 一個簡單的Servlet
- web.xml - 專案部署配置檔
下麵是這幾個檔的具體代碼。
檔: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.zaixian;
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.zaixian;
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.zaixian.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.zaixian.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/ ,如果沒有錯誤,應該會看到以下結果 -
點擊頁面中的鏈接,應該會看到以下結果 -
上一篇:
Servlet身份驗證篩檢程式
下一篇:
Servlet篩檢程式示例