一种方法是把tomcat的server.xml文件
所有的<Connector>标签的URIEncoding属性进行设置,
如: <Connector port="8000" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />
另一种方法是写过滤器
public class SetCharacterEncodingFilter implements Filter {
private static final String DEFAULT_ENCODING = "UTF-8";
protected String encoding;
protected FilterConfig filterConfig;
protected boolean ignore;
public SetCharacterEncodingFilter() {
encoding = null;
filterConfig = null;
ignore = true;
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException {
try {
if (ignore || request.getCharacterEncoding() == null)
if (encoding != null) {
request.setCharacterEncoding(encoding);
response.setContentType("text/html; charset=" + encoding);
} else {
request.setCharacterEncoding(DEFAULT_ENCODING);
response.setContentType("text/html; charset=" + DEFAULT_ENCODING);
}
} catch (Exception e) {
e.printStackTrace();
}
chain.doFilter(request, response);
}
public FilterConfig getFilterConfig() {
return filterConfig;
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
encoding = filterConfig.getInitParameter("CharacterEncoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
ignore = true;
else if (value.equalsIgnoreCase("true"))
ignore = true;
else if (value.equalsIgnoreCase("yes"))
ignore = true;
else
ignore = false;
}
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
}
web.xml配置如下:
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class> com.xx.xx.xx.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>CharacterEncoding</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>SetCharacterEncoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
最后附上:
System.out.println(URLDecoder.decode(queryString, "ISO-8859-1")+"URLDecoder.decode()转成ISO-8859-1");
System.out.println(URLDecoder.decode(queryString, "GBK")+"URLDecoder.decode()转成GBK");
System.out.println(URLDecoder.decode(queryString, "UTF-8")+"URLDecoder.decode()转成UTF-8");
System.out.println(new String(queryString.getBytes("ISO-8859-1"),"gbk")+"ISO-8859-1转成gbk");
System.out.println(new String(queryString.getBytes("ISO-8859-1"),"UTF-8")+"ISO-8859-1转成UTF-8");
System.out.println(new String(queryString.getBytes(),"utf-8")+"getBytes()直接转成UTF-8");
相关推荐
JSP乱码 N种解决方案
JSP解决乱码JSP解决乱码JSP解决乱码JSP解决乱码JSP解决乱码
解决jsp页面乱码,页面信息配置,tomcat配置以及各种信息配置
jsp传值的时候出现的 乱码的3种解决方法
主要介绍几种JSP页面显示为乱码的解决方法,详细的说明乱码原因和解决的办法.....
如何解决JSP页面显示乱码问题
后来查了网上一些人的解决方案,最彻底的解决办法就是修改jspsmartupload的源码,在upload、download方法处强制指定它的编码格式就可以了。这是修改后的jar包,可以直接拿来用,但在初始化SmartUpload类的时候需要...
中文乱码是jsp动态网站开发中经常遇到的问题,该文从产生jsp中文乱码的范例消除乱码的具体方案,具有实用性和易操作性。
这是我收集的最全的解决JSP中文乱码的文章了 其中前面几个万能解决方法是我归纳出来的,不要怕分多,因为我太缺分了,绝对值10分
JSP中汉字乱码现象分析与解决.pdf
jsp中出现的所有中文乱码都可以解决,没有做不得,只有想不到。
jsp传值中文乱码问题解决方法示例介绍.docx
本方案解决了jsp常见的乱码问题 下载后查看详细内容
解决jsp提交乱码的问题 可以解决get post 的乱码问题
get提交中文乱码 地址重写中文乱码 jsp url中文乱码四种解决方式
利用过滤器解决JavaWeb(JSP)的乱码问题
JSP乱码解决方案大家可以看看,可以有利于大家,在JSP页面乱码问题,扥到更好的解决方案
jsp乱码jsp乱码问题jsp乱码jsp乱码问题jsp乱码jsp乱码问题jsp乱码jsp乱码问题jsp乱码jsp乱码问题jsp乱码jsp乱码问题jsp乱码jsp乱码问题jsp乱码jsp乱码问题jsp乱码jsp乱码问题jsp乱码jsp乱码问题jsp乱码jsp乱码问题...
jsp解决乱码方法总结!jsp解决乱码方法总结!jsp解决乱码方法总结!