| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 559 人关注过本帖
标题:[分享]jsp,db,apache中文乱码的解决方案
只看楼主 加入收藏
xiuyuan123
Rank: 2
等 级:新手上路
威 望:3
帖 子:140
专家分:0
注 册:2006-4-25
收藏
 问题点数:0 回复次数:0 
[分享]jsp,db,apache中文乱码的解决方案
一般在传送时使用的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"。

希望能帮大家解决问题!!
搜索更多相关主题的帖子: apache jsp 乱码 方案 中文 
2006-06-28 13:07
快速回复:[分享]jsp,db,apache中文乱码的解决方案
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014772 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved