jsp页面提交到action中文乱码的处理


首先需要修改服务器Tomcat的编码,打开config目录下的server.xml文件,在 节点处添加URIEncoding=”UTF-8″;
然后再写个过滤器,就万事OK了!

写一个JAVA类
package com.company.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;
/**
* 编码过滤器实体类
* @author Administrator
*
*/
public class EncodingFilter implements Filter {
private FilterConfig config;
private String encoding="utf-8";
public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);//过滤请求的编码
chain.doFilter(request, response);//继续过滤
response.setContentType("text/html;charset=utf-8");//过滤响应的编码

}

public void init(FilterConfig config) throws ServletException {
this.config=config;
String s=config.getInitParameter("encoding");//获取初始化参数数,
if(s!=null){
this.encoding=s;
}

}

}
再在项目的web.xml中配置:



EncodingFilter
com.company.filter.EncodingFilter

encoding UTF-8



EncodingFilter
/*

Tags: , , ,



留个脚印

You must be logged in to post a comment.