servlet 过滤(filter)

EncodingFilter.java

package filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.FilterConfig;

public class EncodingFilter implements Filter{

private String targetEncoding = "gb2312";
protected FilterConfig filterConfig;

public void init(FilterConfig config) throws ServletException { 
    this.filterConfig = config; 
    this.targetEncoding = config.getInitParameter("encoding"); 
} 
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException, ServletException { 
    request.setCharacterEncoding(targetEncoding);//把请求用指定的方式编码 
    // 把处理发送到下一个过滤器 
    chain.doFilter(request,response);   
} 

public void destroy() 
{ 
    this.filterConfig=null; 
} 

public void setFilterConfig(final FilterConfig filterConfig) 
{ 
    this.filterConfig=filterConfig; 
}   

}

web.xml

<filter> 
 <filter-name>encoding</filter-name>  
       <filter-class>filter.EncodingFilter</filter-class>  
       <init-param> 
         <param-name>encoding</param-name> 
         <param-value>gb2312</param-value>  
       </init-param> 
</filter>     


<filter-mapping>  
   <filter-name>encoding</filter-name>  
   <url-pattern>*</url-pattern>   
 </filter-mapping>  

我觉得这样写应该可以实现过滤,但没能,请大家看一下,问题的症结在哪儿.谢谢了

2个回答

[code="xml"]
*
[/code]

改成
[code="xml"]
/*
[/code]

试试看。

楼上的就可以;

/* 路径映射;
*.do 扩展映射
[url]http://foxty.iteye.com/blog/39332[/url]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问