过滤器接口与实现字符编码过滤器实例

[1]过滤器的原理

 

[2]过滤链

[3]Filter接口

 

*public void init(FilterConfig filterConfig)throws ServletException//过滤器初始化方法,此方法在过滤器初始化时被调用

*public void doilter(ServletRequest request , ServletResponse response,FilterChain chain)throws IOException, ServletException//对请求进行过滤 处理

*public void destroy();//销毁方法,以便释放资源

[4]FilterConfig接口

 

*public Strng getFilterName();//用于获取过滤器的名字

*public ServletContext getServletContent();//获取Servlet上下文

*public String getInitParameter(String name);//获取过滤器的初始化参数值

*public Enumeration getInitParameterNames();//获取过滤器的所有初始化参数

[5]FilterChain接口

public void doFilter(ServletRequest request request, ServletResponse response)throws IOException , ServletException

[6]过滤器的配置


	
		
		CharactorFilter
		
		com.lyq.CharactorFilter
		
		
			
			encoding
			
			UTF-8
		
	
	
	
		
		CharactorFilter
		
		/*
	


[7]实现字符乱码过滤器处理实例

JSP文件

 
   	

添加图书信息

图书编号:
图书名称:
作  者:
价  格:


servlet文件

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//获取PrintWriter
		PrintWriter out = response.getWriter();
		//获取图书编号
		String id = request.getParameter("id");
		//获取图书名称
		String name=request.getParameter("name");
		//获取作者
		String author =request.getParameter("author");
		//获取价格
		String price= request.getParameter("price");
		//添加成功
		out.print("

图书信息添加成功


"); out.print("图书编号:"+id+"
"); out.print("图书名称:"+name+"
"); out.print(" 作者:"+author+"
"); out.print("体格:"+price+"
"); out.flush();//刷新流 out.close();//关闭流 }


过滤器文件

package com.lyq;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharactorFilter implements Filter {
	// 字符编码
	String encoding = null;
	public void destroy() {
		encoding = null;
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// 判断字符编码是否为空
		if(encoding != null){
			// 设置request的编码格式
			request.setCharacterEncoding(encoding);
			// 设置response字符编码
     		response.setContentType("text/html; charset="+encoding);
		}
		// 传递给下一过滤器
		chain.doFilter(request, response);
	}
	public void init(FilterConfig filterConfig) throws ServletException {
		// 获取初始化参数
		encoding = filterConfig.getInitParameter("encoding");
	}
}


web配置文件

[html] view plain copy print ?
  1.   
  2.     <filter>  
  3.           
  4.         <filter-name>CharactorFilterfilter-name>  
  5.           
  6.         <filter-class>com.lyq.CharactorFilterfilter-class>  
  7.           
  8.         <init-param>  
  9.               
  10.             <param-name>encodingparam-name>  
  11.               
  12.             <param-value>UTF-8param-value>  
  13.         init-param>  
  14.     filter>  
  15.       
  16.     <filter-mapping>  
  17.           
  18.         <filter-name>CharactorFilterfilter-name>  
  19.           
  20.         <url-pattern>/*url-pattern>  
  21.     filter-mapping>  
  22.   <servlet>  
  23.     <description>This is the description of my J2EE componentdescription>  
  24.     <display-name>This is the display name of my J2EE componentdisplay-name>  
  25.     <servlet-name>AddServletservlet-name>  
  26.     <servlet-class>com.lyq.AddServletservlet-class>  
  27.   servlet>  
  28.      
  29.   <servlet-mapping>  
  30.     <servlet-name>AddServletservlet-name>  
  31.     <url-pattern>/AddServleturl-pattern>  
  32.   servlet-mapping>  
  33.   <welcome-file-list>  
  34.     <welcome-file>index.jspwelcome-file>  
  35.   welcome-file-list>  

 

你可能感兴趣的:(web开发,java基础)