ext 表单提交或ajax请求返回的信息中文乱码,后台response和前台页面的编码都设置为uft-8了
提交到后台中的中文正常,返回的信息中有中文就乱码,我在前后台都设置了response.setCharacterEncoding("UTF-8");前台的jsp的编码也是u...
提交到后台中的中文正常,返回的信息中有中文就乱码,我在前后台都设置了response.setCharacterEncoding("UTF-8");前台的jsp的编码也是utf-8,页面属性编码也是utf-8
展开
展开全部
我用拦截器解决了这个问题
web.xml里面的配置
<filter>
<filter-name>SubmitFilter</filter-name>
<filter-class>common.SubmitFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SubmitFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
拦截器文件里面的代码:
package common;
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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SubmitFilter implements Filter{
private FilterConfig filterConfig = null;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if(this.filterConfig == null){
return;
}
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
// HttpSession session = request.getSession();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
this.filterConfig = arg0;
}
}
这样就解决了问题,我试过了,可以的。
web.xml里面的配置
<filter>
<filter-name>SubmitFilter</filter-name>
<filter-class>common.SubmitFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SubmitFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
拦截器文件里面的代码:
package common;
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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SubmitFilter implements Filter{
private FilterConfig filterConfig = null;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if(this.filterConfig == null){
return;
}
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
// HttpSession session = request.getSession();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
this.filterConfig = arg0;
}
}
这样就解决了问题,我试过了,可以的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询