tomcat中设置了URIEncoding为utf-8怎么解决get方式的提交乱码问题 20
我在tomcat中设置了URIEncoding=utf-8但是get方式提交后用request.getParameter后为乱码怎么办在servlet中request.s...
我在tomcat中设置了URIEncoding=utf-8但是get方式提交后用request.getParameter后为乱码怎么办
在servlet中request.setCharacterEncoding("utf-8"); 不行打印出来的是?????号 展开
在servlet中request.setCharacterEncoding("utf-8"); 不行打印出来的是?????号 展开
4个回答
展开全部
写个中文过滤的代码,直接贴过去,改下包路径和servlet-name
1. 编写处理字符编码的Filter类:
package yours.package;
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;
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 = selectEncoding(request);
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;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
2.修改WEB-INF/web.xml文件
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>yours.package.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
1. 编写处理字符编码的Filter类:
package yours.package;
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;
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 = selectEncoding(request);
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;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
2.修改WEB-INF/web.xml文件
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>yours.package.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
展开全部
看看你的页面是不是也是utf-8的格式...如果你页面传送过来的本身就不是utf-8,那再怎么转都是错的..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
servlet里:
request.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
request.setCharacterEncoding("gb18030");试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询