tomcat中文问题解决
2007-02-27 22:00
134 查看
一、表单POST的数据的中文乱码解决
这类型的数据中文可以通过filters来实时过滤。filters代码如下:
package filters;
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.UnavailableException;
public class SetCharacterEncodingFilter 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 ...{
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) ...{
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
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);
}
}
filters配置(配置web.xml):
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二、将中文数据存入数据库乱码问题
以mysql为例,改写连接字符串即可:
jdbc:mysql://localhost:3306/workshopdb? useUnicode=true&characterEncoding=GBK
三、 通过url传递参数和识别中文文件名问题
问题表现:1、通过url传递参数,例如:
http://localhost:81/crjy/admin/articlelist.jsp?levelId=64&levelName=学生党建
通过request.getParameter("levleName")得到的值为乱玛。
2、识别中文文件名,例如:
<img src="./pic/四川地图.jpg"> 图片不能显示。
解决之道:
1、如果只想解决第一个问题那很简单,两句代码即可:
String role=request.getParameter("chara");
role=new String(role.getBytes("ISO-8859-1"),"GB2312");
out.println(role);
因为tomcat服务器默认用 ISO-8859-1 字符集的。但是这样只能解决第一个问题不能解决中文文件名问题
2、两个问题一起解决,修改server.xml,找到下列语句添加URIEncoding="GB18030",这样两个问题就一起解决了(不需要
role=new String(role.getBytes("ISO-8859-1"),"GB2312");转化,得到的参数即为正常的中文)
<Connector acceptCount="100" connectionTimeout="20000" debug="0" disableUploadTimeout="true" enableLookups="false" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="81" redirectPort="8443"/>
这类型的数据中文可以通过filters来实时过滤。filters代码如下:
package filters;
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.UnavailableException;
public class SetCharacterEncodingFilter 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 ...{
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) ...{
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
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);
}
}
filters配置(配置web.xml):
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二、将中文数据存入数据库乱码问题
以mysql为例,改写连接字符串即可:
jdbc:mysql://localhost:3306/workshopdb? useUnicode=true&characterEncoding=GBK
三、 通过url传递参数和识别中文文件名问题
问题表现:1、通过url传递参数,例如:
http://localhost:81/crjy/admin/articlelist.jsp?levelId=64&levelName=学生党建
通过request.getParameter("levleName")得到的值为乱玛。
2、识别中文文件名,例如:
<img src="./pic/四川地图.jpg"> 图片不能显示。
解决之道:
1、如果只想解决第一个问题那很简单,两句代码即可:
String role=request.getParameter("chara");
role=new String(role.getBytes("ISO-8859-1"),"GB2312");
out.println(role);
因为tomcat服务器默认用 ISO-8859-1 字符集的。但是这样只能解决第一个问题不能解决中文文件名问题
2、两个问题一起解决,修改server.xml,找到下列语句添加URIEncoding="GB18030",这样两个问题就一起解决了(不需要
role=new String(role.getBytes("ISO-8859-1"),"GB2312");转化,得到的参数即为正常的中文)
<Connector acceptCount="100" connectionTimeout="20000" debug="0" disableUploadTimeout="true" enableLookups="false" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="81" redirectPort="8443"/>
相关文章推荐
- tomcat服务器配置字符集为utf-8-彻底解决中文乱码问题
- tomcat容器 SSH项目开发 上传图片图片名为中文时不能显示图片问题解决
- tomcat解决中文乱码问题
- tomcat服务器配置字符集为utf-8-彻底解决中文乱码问题
- Tomcat 5/6 GBK编码下解决中文表单问题的过滤器get方式(借鉴)
- Tomcat的中文问题的java解决之道
- Tomcat中文乱码问题的原理和解决方法
- 关于上个Flex-Jsp-DB例子中Flex和Jsp传递中文参数问题的解决方法!(Tomcat服务器)
- Maven项目转移到Tomcat上时,怎样解决地址栏传中文乱码的问题?
- tomcat8.0环境下解决get方法中文参数乱码问题
- 彻底解决Tomcat 5.0.19的中文乱码问题
- 写了一个查询数据库的程序,不熟的Tomcat6。查询条件为英文时运行正常,为中文时出现乱码。后在tomcap的server.xml中加入useBodyEncodingForURI="true解决了问题
- TOMCAT下的JIVE2的中文问题分析及解决
- CentOS中文语言乱码以及Tomcat日志中文乱码问题解决
- 升级tomcat后出现的页面录入框中文乱码问题解决方法
- tomcat 解决中文乱码问题
- Apache+tomcat服务器解决中文乱码的问题
- 配置Tomcat解决URL传参引出的中文乱码问题
- tomcat服务器配置字符集为utf-8-彻底解决中文乱码问题
- tomcat服务器配置字符集为utf-8-彻底解决中文乱码问题