创建过滤器
Servlet實現了Filter接口,定義了如下方法:
- init():程序啟動時調用此方法,用于初始化該Filter
- doFilter():客戶請求服務器是會經過這里,是具體執行過濾器代碼
- dostory():程序關閉時調用此方法
Filter的生命周期,init()和destroy()方法只被調用一次,doFilter()方法每次有客戶端請求就會被調用一次
創建過濾器類FirstFilter類
在web.xml中配置過濾器
<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><filter><filter-name>firstFilter</filter-name><!--過濾器名稱 --><filter-class>com.cn.zj.ServletFilter.FirstFilter</filter-class><!--過濾器的實現類 --></filter><filter-mapping><filter-name>firstFilter</filter-name> <!--映射過濾器名稱 --><url-pattern>/*</url-pattern><!--使用通配符*什么請求都經過濾器 --></filter-mapping> **index.js頁面** <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>創建過濾器</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> 請重新刷卡登錄!!!</body> </html>總結
- 上一篇: 利用Servlet实现用户永久登录
- 下一篇: 防盗链过滤器