通过Ajax向Servlet发送带参请求,在Servlet取得该值时出现乱码(只有传递中文时出现) 10
通过Ajax向Servlet发送带参请求,在Servlet取得该值时出现乱码(只有传递中文时出现)。也在Servlet中设置了request的编码方式为GBK了。还是不行...
通过Ajax向Servlet发送带参请求,在Servlet取得该值时出现乱码(只有传递中文时出现)。也在Servlet中设置了request的编码方式为GBK了。还是不行,问什么原因?怎么解决?
展开
展开全部
请使用下面的编码过滤器:
public class CharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = this.encoding;
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
}
并在web.xml中配置:
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>CharacterEncodingFilter</filter-class>
<init-param>
<!-- 定义编码格式 -->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<servlet-name>/</servlet-name>
</filter-mapping>
public class CharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = this.encoding;
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
}
并在web.xml中配置:
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>CharacterEncodingFilter</filter-class>
<init-param>
<!-- 定义编码格式 -->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<servlet-name>/</servlet-name>
</filter-mapping>
展开全部
页面编码,<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
文件的保存编码改成GBK
乱码一般就这几个地方改了就好了
文件的保存编码改成GBK
乱码一般就这几个地方改了就好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
页面上的编码方式改成GBK了没
追问
我直接在服务器上进行输出的
追答
对啊,但是参数是通过ajax从页面上传过来的,文件的保存方式也要一样(你用的eclipse的话)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询