BaseFilter.java:
package day22BaseFilter;
import java.io.IOException;
import java.util.Enumeration;
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;
import javax.servlet.http.HttpServletResponse;
/*
如何定义一个过滤器
1. 定义一个类实现Filter接口,重写接口中的方法
2. 在web.xml文件中注册
*/
public class BaseFilter implements Filter {
public BaseFilter(){
System.out.println("BaseFilter方法");
}
/*
* 3.释放资源的方法
* servlet容器调用方法==servelet destroy() 方法
* 能被调用1次
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
System.out.println("destroy方法");
}
/*
* 2.处理客户请求的方法
* servlet容器调用方法==servelet service() 方法
* 能被调用多次
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("doFilter方法");
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
System.out.println("req.getContextPath()= "+req.getContextPath());
System.out.println("req.getRequestURI()= "+req.getRequestURI());
System.out.println("req.getRequestURL()= "+req.getRequestURL());
System.out.println("req.getServletPath() "+req.getServletPath());
//放行 控制后面的资源是否执行
chain.doFilter(request, response);
}
/*
* 1.初始化方法 servlet容器调用方法==servlet init 方法
* *能被调用一次
* FilterConfig类似于servletConfig
* ApplicationFilterConfig 实现FilterConfig接口
*/
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init方法");
System.out.println("filterConfig "+filterConfig);
System.out.println("获取过滤器的名称:"+filterConfig.getFilterName());
/* <init-param>
<param-name>test1</param-name>
<param-value>a1</param-value>
</init-param>*/
String initparam1=filterConfig.getInitParameter("test1");
System.out.println("initparam1= "+initparam1);
Enumeration<String> em=filterConfig.getInitParameterNames();
while(em.hasMoreElements()){
String paramName=em.nextElement();
String paramValue=filterConfig.getInitParameter(paramName);
System.out.println(paramName+" "+paramValue);
}
/*<context-param>
<param-name>testContext</param-name>
<param-value>xxxxxxxx</param-value>
</context-param>*/
//获取上下文对象
ServletContext sc=filterConfig.getServletContext();
String str=sc.getInitParameter("testContext");
System.out.println("str= "+str);
}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<context-param>
<param-name>testContext</param-name>
<param-value>xxxxxxxx</param-value>
</context-param>
<!-- 注册filter -->
<filter>
<filter-name>BaseFilter</filter-name>
<filter-class>day22BaseFilter.BaseFilter</filter-class>
<init-param>
<param-name>test1</param-name>
<param-value>a1</param-value>
</init-param>
<init-param>
<param-name>test2</param-name>
<param-value>a2</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>BaseFilter</filter-name>
<!-- 拦截所有的请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>