首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

filter来处理乱码有关问题

2012-12-18 
filter来处理乱码问题package sino.person.web.filterimport java.io.IOExceptionimport java.util.Enum

filter来处理乱码问题

package sino.person.web.filter;import java.io.IOException;import java.util.Enumeration;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;public class CharacterEncodingFilter implements Filter {public void destroy() {// TODO Auto-generated method stub}public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException {//判断如果客户端使用get方式提交参数,则对参数硬编码HttpServletRequest request = (HttpServletRequest)req;String method = request.getMethod();if(method.equalsIgnoreCase("get")){//获取地址栏中传递的所有参数名Enumeration names = request.getParameterNames();while(names.hasMoreElements()){//获取每个参数名String name = (String)names.nextElement();//根据参数名,获取每个参数值String[] ss = req.getParameterValues(name);//对每个值进行字符编码for(int i = 0; i < ss.length ; i++){ss[i] = new String(ss[i].getBytes("iso-8859-1"),"gbk");}}}//如果使用post方式提交,则设置req和resp即可request.setCharacterEncoding("gbk");resp.setCharacterEncoding("gbk");chain.doFilter(request, resp);}public void init(FilterConfig arg0) throws ServletException {}}

?

热点排行