鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 网站制作教程 > JSP > >

一个中文过滤器代码

来源:互联网 作者:佚名 时间:2015-08-19 07:40
package com.itcast.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 jav

package com.itcast.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;

public class SetCodeFilter implements Filter {

 public void destroy() {
  // TODO Auto-generated method stub

 }

 public void doFilter(ServletRequest req, ServletResponse res,
   FilterChain chain) throws IOException, ServletException {
  req.setCharacterEncoding("utf8");
  chain.doFilter(req,res);
 }

 public void init(FilterConfig arg0) throws ServletException {

 }

}

 

 

相应的web.xml文件中的配置是:

<filter>
   <filter-name>setCodeFilter</filter-name>
   <filter-class>com.itcast.filter.SetCodeFilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>setCodeFilter</filter-name>
   <url-pattern>*.do</url-pattern>
  </filter-mapping>
  <filter-mapping>
   <filter-name>setCodeFilter</filter-name>
   <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

 

网友评论
<