??????What’s the Filter
??????Filter?????????????????Servlet?????б???????????????WEB??????????Filter????????web???????????????web?????????Jsp?? Servlet?? ??????????? html ????????????????????Щ????????????????URL?????????????????????д??????????????Щ????????

????????????Filter ???????Filter?????javax.servlet.Filter??? ???doFilter()?????????????? ??web.xml?н???????
??????package com.yiyexiaoyuan.filter;
??????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;
??????/**
??????* Servlet Filter implementation class TestFilter
??????*/
??????//???Filter???
??????public class TestFilter implements Filter
??????{
??????/**
??????* Default constructor.
??????*/
??????public TestFilter()
??????{
??????// TODO Auto-generated constructor stub
??????}
??????/**
??????* @see Filter#destroy()
??????*/
??????//Filter??????????
??????public void destroy()
??????{
??????// TODO Auto-generated method stub
??????}
??????/**
??????* @see Filter#doFilter(ServletRequest?? ServletResponse?? FilterChain)
??????*/
??????//???????????
??????public void doFilter(ServletRequest request?? ServletResponse response??
??????FilterChain chain) throws IOException?? ServletException
??????{
??????//??????Filter
??????chain.doFilter(request?? response);
??????}
??????/**
??????* @see Filter#init(FilterConfig)
??????*/
??????//???????????
??????public void init(FilterConfig fConfig) throws ServletException
??????{
??????// TODO Auto-generated method stub
??????}
??????}
????????web.xml??????
??????<filter>
??????<display-name>TestFilter</display-name>
??????<filter-name>TestFilter</filter-name>
??????<filter-class>com.yiyexiaoyuan.filter.TestFilter</filter-class>
??????</filter>
??????<filter-mapping>
??????<filter-name>TestFilter</filter-name>
??????<url-pattern>/TestFilter</url-pattern>
??????</filter-mapping>
????????ó??? ????????????Filter
??????package com.yiyexiaoyuan.filter;
??????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;
??????import javax.servlet.http.HttpServletRequest;
??????/**
??????* Servlet Filter implementation class PageEncodingFilter
??????*/
??????public class PageEncodingFilter implements Filter
??????{
??????private String encoding = "UTF-8";
??????protected FilterConfig filterConfig;
??????public void init(FilterConfig filterConfig) throws ServletException
??????{
??????this.filterConfig = filterConfig;
??????// ????????????????UTF-8???????????web.xml????????????????????????
??????if (filterConfig.getInitParameter("encoding") != null)
??????encoding = filterConfig.getInitParameter("encoding");
??????}
??????public void doFilter(ServletRequest srequset?? ServletResponse sresponse??
??????FilterChain filterChain) throws IOException?? ServletException
??????{
??????HttpServletRequest request = (HttpServletRequest) srequset;
??????request.setCharacterEncoding(encoding);
??????filterChain.doFilter(srequset?? sresponse);
??????}
??????public void destroy()
??????{
??????this.encoding = null;
??????}
??????}
??????web.xml????
??????<filter>
??????<display-name>PageEncodingFilter</display-name>
??????<filter-name>PageEncodingFilter</filter-name>
??????<filter-class>com.yiyexiaoyuan.filter.PageEncodingFilter</filter-class>
??????<init-param>
??????<param-name>encoding</param-name>
??????<param-value>UTF-8</param-value>
??????</init-param>
??????</filter>
??????<filter-mapping>
??????<filter-name>PageEncodingFilter</filter-name>
??????<url-pattern>/servlet/*</url-pattern>
??????</filter-mapping>