jsp,db,apache中文乱码的解决方案

http://www.matrix.org.cn/thread.shtml?topicId=21142&fid=14

以下是我做的中文乱码的总结,希望能帮你解决问题:
一般在传送时使用的encoding:
使用GET 的方式:
String test = new String((request.getParameter("test")).getBytes("ISO-8859-1"),"GBK");

使用POST 的方式:
request.setCharacterEncoding("GBK");

pageEncoding及contentType的作用:
<%@ page pageEncoding="GBK" %>
pageEncoding是当jsp转译成_jsp.java时使用的encoding.预设是iso8859_1.
然後_jsp.java编译成_jsp.class是使用utf-8作为encoding.
response.setContentType("text/html; charset=GBK");
or
<%@ page contentType="text/html;charset=GBK" %>
就是输出到浏览器时的编码,预设是iso8859_1.
这样浏览器才知道应该用甚麽charset来显示.

资料库输出到jsp出现乱码:
第一,检查一下你database里的资料是否乱码.
如果database里的资料是乱码,检查一下再进入资料库时request接收後资料是否是乱码.
即忘了加入request.setCharacterEncoding(String charset);
request.setCharacterEncoding(String charset)把表单传送过来的资料以charset的字型来encoding.
如果接收时是正常的.但资料库是乱码,
那就检查一下database的编码设定.

如果资料库的资料正常.但输出到jsp的资料是乱码.
以mysql为例:
String connect = "jdbc:mysql://localhost/dbname?user=&password=&useUnicode=true&characterEncoding=GBK";
就上面的mysql的例子.
需要设定useUnicode为true.
而characterEncoding=GBK必须与contentType的charset一样.
mysql的设定档my.ini:
[client]  default-character-set=GBK
[mysqld]  default-character-set=GBK


以下连结则介绍了access的jdbc:odbc的编码:
http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/bridge.html

include资料出现乱码:
旧的版本tomcat是不能在每一页jsp中加入
<%@ page contentType="text/html; charset=GBK" %>
tomcat 5.x之後是可以每一页都加入上面那一句的.
会必须include的page及被include的page的编码要相同.连大小写都要相同.

但这样做不是最好的方法.

其实可以在你的web application底下的web.xml的<web-app>里加入:


<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version="2.4">
.........
.........  
<jsp-config>
<jsp-property-group>  
  <description>jsp encoding example</description>
  <display-name>JSPConfiguration</display-name>
  <url-pattern>*.jsp</url-pattern>
  <el-ignored>true</el-ignored>
  <page-encoding>GBK</page-encoding>
  <scripting-invalid>false</scripting-invalid>
  <include-prelude></include-prelude>
  <include-coda></include-coda>
  <description>html encoding example</description>
  <display-name>JSPConfiguration</display-name>  
  <url-pattern>*.html</url-pattern>  
  <el-ignored>true</el-ignored>
  <page-encoding>GBK</page-encoding>
  <scripting-invalid>false</scripting-invalid>
  <include-prelude></include-prelude>  
  <include-coda></include-coda>
</jsp-property-group>
</jsp-config>
.........
.........
</web-app>




apache整合tomcat 中文问题:
apache+tomcat+JK2集成时,http的请求是通过jk2的ajp13转到tomcat的8009端口处理的,
所以要修改tomcat/conf/server.xml中的以下两项
1 <Connector port="8080"    ……  URIEncoding="GBK" >
2 <Connector port="8009"    ……  URIEncoding="GBK" >
都加上URIEncoding="GBK"。


以下是javaman写的中文问题总结:
http://www.matrix.org.cn/forum_view.asp?forum_id=14&view_id=676

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值