从servlet发送回页面ajax的数据中文乱码
推荐于2016-02-06
展开全部
在你的filter文件中做如下类似的配置,用于解决整体项目的中文乱码问题。
/**
* 转换由表单读取的数据的内码.
* 从 ISO 字符转到 GB2312.
*/
public String toChi(String input) {
try {
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes, "GB2312");
}
catch (Exception ex) {
}
return null;
}
/**
* Return the HttpServletRequest holded by this object.
*/
private HttpServletRequest getHttpServletRequest()
{
return (HttpServletRequest)super.getRequest();
}
/**
* 读取参数 -- 修正了中文问题.
*/
public String getParameter(String name)
{
return toChi(getHttpServletRequest().getParameter(name));
}
/**
* 读取参数列表 - 修正了中文问题.
*/
public String[] getParameterValues(String name)
{
String values[] = getHttpServletRequest().getParameterValues(name);
if (values != null) {
for (int i = 0; i < values.length; i++) {
values[i] = toChi(values[i]);
}
}
return values;
}
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpreq = (HttpServletRequest)request;
System.out.println("filter");
if(httpreq.getMethod().equals("POST")) {
request.setCharacterEncoding("GB2312");
} else {
request = new Request(httpreq);
}
response.setContentType("text/html;charset=GB2312");
chain.doFilter(request, response);
}
/**
* 转换由表单读取的数据的内码.
* 从 ISO 字符转到 GB2312.
*/
public String toChi(String input) {
try {
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes, "GB2312");
}
catch (Exception ex) {
}
return null;
}
/**
* Return the HttpServletRequest holded by this object.
*/
private HttpServletRequest getHttpServletRequest()
{
return (HttpServletRequest)super.getRequest();
}
/**
* 读取参数 -- 修正了中文问题.
*/
public String getParameter(String name)
{
return toChi(getHttpServletRequest().getParameter(name));
}
/**
* 读取参数列表 - 修正了中文问题.
*/
public String[] getParameterValues(String name)
{
String values[] = getHttpServletRequest().getParameterValues(name);
if (values != null) {
for (int i = 0; i < values.length; i++) {
values[i] = toChi(values[i]);
}
}
return values;
}
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpreq = (HttpServletRequest)request;
System.out.println("filter");
if(httpreq.getMethod().equals("POST")) {
request.setCharacterEncoding("GB2312");
} else {
request = new Request(httpreq);
}
response.setContentType("text/html;charset=GB2312");
chain.doFilter(request, response);
}
展开全部
把你得到的前台参数作如下处理:
String s=new String(param.getBytes("IS0-8859-1“,"UTF-8");
String s=new String(param.getBytes("IS0-8859-1“,"UTF-8");
追问
这个已经处理过了,前台接收的数据已经处理过乱码了,就是发送回页面ajax时的数据编码方式没法设定成utf-8。就像上面最后那张图那样。
追答
你jsp页面编码改下UTF-8
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用new String()方法改变一下编码格式
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询