使用字符集utf-8支持多国语言的问题  2009-01-16   Java  

可以在jsp页面上同时输入中文,日文,韩文等几乎世界上所有语言.保证页面上不出现乱码.

1.servlet controller:
设置请求的字符集.

request.setCharacterEncoding("utf-8");

2. jsp:
设置页面的字符集.

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>

3. Post传递参数(这种方式没有问题,尽量采用这种方式):

<form name="TestForm" method="post" action="/test.do">
	<input type="text" name="name">
</form>	

4. Get传递参数:
使用URLEncoder编码.

test.do?name=<%=java.net.URLEncoder.encode(request.getParameter("name"),
"UTF-8")%>

5. server.xml:
设置useBodyEncodingForURI=”true”(推荐)或URIEncoding=”utf-8″参数.

<Connector port="8080" ... useBodyEncodingForURI="true"/>

useBodyEncodingForURI和URIEncoding的区别是:
useBodyEncodingForURI根据

request.setCharacterEncoding("utf-8");

自动设定每次请求的字符集;
而URIEncoding将tomcat的所有GET方式的请求都进行重新编码.会影响所有WEB应用.

6. mysql:
1)数据库和表的字符集设置为utf8_unicode_ci
2)数据库连结:

jdbc:mysql://localhost/mydb?useUnicode=true&characterEncoding=utf-8
jsp和java用&,XML用&amp;

经过以上几个步骤后, web端和数据库就都支持多国语言了, 甚至可以同时输入,查询和存储多国语言信息.


 发表评论

(必填)

(必填)

评论(必填,最多字数:100):