Tomcat5.5中文问题解决方案
2008-02-29 13:29
239 查看
1、html
无论是独立的html,还是其他程序生成的,如Servlet等,注意在最终的html的<head>和</head>之间必须加入meta标签,用来指定html中输入字符的编码,如:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试GET && POST-Send</title>
</head>
2、jsp和servlet
首先必须解决程序输出(如response.writeln(String s))和接受从客户端传来的数据(如request.getParameter(String sname))编码问题,我们可以利用文件过滤功能,具体需要所用的jsp/servlet容器或者服务器提供的功能设置,如在Tomcat5.5.9中可以在webapps/yourAppDirectory/WEB-INF/web.xml中设置如下:
<filter>
<filter-name>SetCharsetEncodingFilter</filter-name>
<display-name>SetCharsetEncodingFilter</display-name>
<description>Set CharsetEncoding Filter</description>
<filter-class>com.gg.comm.web.SetCharsetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其中SetCharsetEncodingFilter Class就是用来设置request和reponse字符编码的filter类,其中设置语句如下:
request.setCharacterEncoding(targetEncoding);
response.setContentType("text/html");
response.setCharacterEncoding(targetEncoding);
另外为了解决通过get(url中带有参数)方式传递参数的乱码问题,我们还需要设置一下url传递参数所需要的编码,具体在Tomcat5.5.9中可以在${Tomcat_home}/conf/server.xml中的<connector>和</connector>之间设置,如下:
<!--
URIEncoding="GBK":Force GET method String(Chinese) can be transferd properly by http:uri
note:Tomcat only support GBK specification,so not set charset gb2312
-->
<Connector URIEncoding="GBK" port="80" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
</Connector>
最后为了解决jsp的乱码问题,我们还需要作如下处理,即在左右的jsp头均加上如下指令:
<%@ page contentType="text/html;charset=gb2312" language="java" %>
或者
<%@ page pageEncoding="gb2312"%>
3、jdbc和数据库
关于写入数据库和读取数据库数据的乱码问题,可以通过如下方式轻松解决:
对于JAVA程序的处理方法按我们指定的方法处理。
把数据库默认支持的编码格式改为GBK或GB2312的。
到此,一般来说对于WEB方式的应用来说,中文问题就可以解决了。当然以上方法是根据统一编码的原则解决的以及WEB方式的文件转换关系(file->class->load->execute or transfered or response or request)来做的。
无论是独立的html,还是其他程序生成的,如Servlet等,注意在最终的html的<head>和</head>之间必须加入meta标签,用来指定html中输入字符的编码,如:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试GET && POST-Send</title>
</head>
2、jsp和servlet
首先必须解决程序输出(如response.writeln(String s))和接受从客户端传来的数据(如request.getParameter(String sname))编码问题,我们可以利用文件过滤功能,具体需要所用的jsp/servlet容器或者服务器提供的功能设置,如在Tomcat5.5.9中可以在webapps/yourAppDirectory/WEB-INF/web.xml中设置如下:
<filter>
<filter-name>SetCharsetEncodingFilter</filter-name>
<display-name>SetCharsetEncodingFilter</display-name>
<description>Set CharsetEncoding Filter</description>
<filter-class>com.gg.comm.web.SetCharsetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其中SetCharsetEncodingFilter Class就是用来设置request和reponse字符编码的filter类,其中设置语句如下:
request.setCharacterEncoding(targetEncoding);
response.setContentType("text/html");
response.setCharacterEncoding(targetEncoding);
另外为了解决通过get(url中带有参数)方式传递参数的乱码问题,我们还需要设置一下url传递参数所需要的编码,具体在Tomcat5.5.9中可以在${Tomcat_home}/conf/server.xml中的<connector>和</connector>之间设置,如下:
<!--
URIEncoding="GBK":Force GET method String(Chinese) can be transferd properly by http:uri
note:Tomcat only support GBK specification,so not set charset gb2312
-->
<Connector URIEncoding="GBK" port="80" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
</Connector>
最后为了解决jsp的乱码问题,我们还需要作如下处理,即在左右的jsp头均加上如下指令:
<%@ page contentType="text/html;charset=gb2312" language="java" %>
或者
<%@ page pageEncoding="gb2312"%>
3、jdbc和数据库
关于写入数据库和读取数据库数据的乱码问题,可以通过如下方式轻松解决:
对于JAVA程序的处理方法按我们指定的方法处理。
把数据库默认支持的编码格式改为GBK或GB2312的。
到此,一般来说对于WEB方式的应用来说,中文问题就可以解决了。当然以上方法是根据统一编码的原则解决的以及WEB方式的文件转换关系(file->class->load->execute or transfered or response or request)来做的。
相关文章推荐
- eclipse tomcat报Several ports (8005, 8080, 8009) 端口被占用问题解决方案
- tomcat服务器配置字符集为utf-8-彻底解决中文乱码问题
- tomcat服务器配置字符集为utf-8-彻底解决中文乱码问题
- Tomcat中文乱码问题的原理和解决方法
- Tomcat7下配置SSI(解决中文乱码问题)
- Mysql通过中文查询数据查不出结果问题解决方案
- 关于上个Flex-Jsp-DB例子中Flex和Jsp传递中文参数问题的解决方法!(Tomcat服务器)
- Tomcat中文乱码问题filter解决
- Eclipse,tomcat及jsp-java编码的设定解决的中文乱码问题
- Tomcat中文乱码问题的原理和解决方法
- 彻底解决ubuntu环境下,Qt5.5以上版本无法进行中文注释的问题。
- 解决Tomcat请求中文乱码的问题
- SpringMVC解决中文在tomcat传输乱码问题
- 彻底解决Tomcat 5.0.19的中文乱码问题
- Eclipse,tomcat及jsp页面编码的设定解决的中文乱码问题
- tomcat 中文路径的问题解决。
- centos7 javaweb 链接中文请求tomcat乱码问题解决
- tomcat5.5中jstl和el表达式不显问题解决办法!!!
- 解决linux服务器Tomcat日志中文乱码问题
- jsp中文问题解决方案(完整版)