过滤器写法

package com.accp.util;
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 EncodingFilter implements Filter
{
  private FilterConfig filterConfig;
 
  public void destroy() {}
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
 {
   // 从web.xml中获得编码名称
   String encoding = filterConfig.getInitParameter("encoding");
   // 转换编码
   request.setCharacterEncoding(encoding);
   response.setCharacterEncoding(encoding);
   chain.doFilter(request, response);
 }
 public void init(FilterConfig filterConfig) throws ServletException
 {
   this.filterConfig = filterConfig;
 }
}
-------------------web.xml的写法,在所有servlet之上---------------------
 <filter>
  <filter-name>EncodingFilter</filter-name>
  <filter-class>com.accp.util.EncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>EncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping> 
阅读更多
个人分类: javaWeb
上一篇连接数据库方法汇总
下一篇转换字节码
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭