定义一个过滤器Filter及Filter提供的相关操作

本文详细介绍了如何定义一个Servlet Filter,包括其在Web应用中的作用,如何配置及使用,涉及过滤请求、响应编码、服务拦截等核心操作。
摘要由CSDN通过智能技术生成

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>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值