例1 創建一個過濾器,實現網站訪問計數器的功能,並在web.xml文件的配置中,將網站訪問量的初始值設置為5000。
(1)創建名稱為CountFilter的類,該類實現javax.servlet.Filter接口,是一個過濾器對象,通過該過濾器實現統計網站訪問人數的功能。關鍵代碼如下:
package com.cn.gao; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class CountFilter implements Filter { //來訪數量 private int count; public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { count++; //訪問數量自增 //將ServletRequest轉換成HttpServletRequest HttpServletRequest req = (HttpServletRequest) request; //獲取ServletContext ServletContext context = req.getSession().getServletContext(); context.setAttribute("count", count); //將來訪數量值放入到ServletContext中 chain.doFilter(request, response); //向下傳遞過濾器 } public void init(FilterConfig filterConfig) throws ServletException { String param = filterConfig.getInitParameter("count"); //獲取初始化參數 count = Integer.valueOf(param); //將字符串轉換為int } }
計數器count變量的值在CountFilter類的doFilter()方法中被遞增,因為客戶端在請求服務器中的Web應用時,過濾器攔截請求通過doFilter()方法進行過濾處理,所以,當客戶端請求Web應用時,計數器count的值將自增1。為了能夠訪問計數器中的值,實例中將其放置於Servlet上下文中,Servlet上下文對象通過將ServletRequest轉換為HttpServletRequest對象后獲取。
(2)配置已創建的CountFilter對象,此操作通過配置web.xml文件進行實現。關鍵代碼如下:
<?xml version="1.0" encoding="gbk"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 過濾器聲明 --> <filter> <filter-name>CountFilter</filter-name> <filter-class>com.cn.gao.CountFilter</filter-class> <init-param> <param-name>count</param-name> <param-value>5000</param-value> </init-param> </filter> <filter-mapping> <filter-name>CountFilter</filter-name> <url-pattern>/index.jsp</url-pattern> </filter-mapping> </web-app>
注意:如果直接對過濾器對象中的成員變量進行賦值,那么在過濾器被編譯后將不可修改,所以,實例中將過濾器對象中的成員變量定義為過濾器的初始化參數,從而提高代碼的靈活性。
(3)創建程序中的首頁index.jsp頁面,在該頁面中通過JSP內置對象Application獲取計數器的值。關鍵代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h2> 歡迎光臨,<br/> 您是本站的第【 <%=application.getAttribute("count") %> 】位訪客! </h2> </body> </html>
由於在web.xml文件中將計數器的初始值設置為5000,所以實例運行后,計數器的數值變為大於5000的數,每次刷新頁面都會增加1,多次刷新頁面后,實例運行結果如下圖所示: