JSP 中文问题
环境:tomcat5.5.7 + JDK1.5.0_02 + mysql4.1.2
问题:从数据库中读出数据时显示乱码“?????????”
代码如下:
mysql.html
<html>
<head>
<title> mysql </title>
<meta http-equiv="Content-type" content="text/html;charset=GB2312">
</head>
<body>
<form name = "form" method = "post" action = "mysql.jsp">
<p>姓:<input name="last_name" type="text" id="last_name"></p>
<p>名:<input name="first_name" type = "text" id="first_name"></p>
<p>
<input type = "submit" value="传送">
<input type = "reset" value="取消">
</p>
</form>
</body>
</html>
mysql.jsp
<%@ page import = "java.sql.*"%>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>mysql.jsp</title>
</head>
<body>
<h2>将信息存入Mysql中</h2>
<%
Connection con = null;
Statement stmt = null;
Statement stmt1 = null;
ResultSet rs = null;
request.setCharacterEncoding("GB2312");
String employee_id = null;
String last_name = request.getParameter("last_name");
String first_name = request.getParameter("first_name");
String birth = "1977/12/08";
String sex = "F";
String new_last_name = "" ;
String new_first_name ="" ;
%>
从 mysql.html 接收到如下信息:<br>
姓:<%= last_name %>
名:<%= first_name %><br><br>
<%
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=19780306&useUnicode=true&characterEncoding=GB2312");
stmt = con.createStatement();
String upd = "insert into employee(employee_id,last_name,first_name,birth,sex) values("+employee_id+",'"+last_name+"','"+first_name+"','"+birth+"','"+sex+"')";
stmt.executeUpdate(upd);
stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "SELECT * FROM employee";
rs = stmt1.executeQuery(query);
rs.last( );
new_last_name = rs.getString("last_name");
new_first_name = rs.getString("first_name");
stmt.close();
stmt1.close();
con.close();
}
catch(SQLException sqle)
{
out.println("sqle="+sqle);
}
finally
{
try
{
if (con!= null)
{
con.close();
}
}
catch(SQLException sqle)
{
out.println("sqle="+sqle);
}
}
%>
从employee表取出最新新增的姓名:<br>
新增姓名:<%= new_last_name+new_first_name %><br>
</body>
</html>
问题点数:100、回复次数:13Top
1 楼tmpx(沙漠之鹰)回复于 2005-05-11 10:28:56 得分 0
说漏了一点,数据库驱动用的mysql-connector-java-3.2.0-alpha-bin.jarTop
2 楼tmpx(沙漠之鹰)回复于 2005-05-11 10:37:00 得分 0
我改用mysql-connector-java-3.0.10-stable-bin.jar也还是不行,请高手帮忙看看Top
3 楼Kuangxian(狂仙)回复于 2005-05-11 10:46:59 得分 10
加个<%request.setCharacterEncoding("gb2312");%>试试Top
4 楼mind_1220(大灰狼)回复于 2005-05-11 10:49:48 得分 10
我想问题不是出现在你读的时候!
而是在你写的时候!
你写如数据库的时候已经不是中文了!
你可以仔细的看看Top
5 楼tmpx(沙漠之鹰)回复于 2005-05-11 10:56:12 得分 0
执行结果:
将信息存入Mysql中
从 mysql.html 接收到如下信息:
姓:蔡 名:铮锋
从employee表取出最新新增的姓名:
新增姓名:??????
Top
6 楼wzy19514(凡事留一线,日后好相见)回复于 2005-05-11 11:02:40 得分 5
up
学习
Top
7 楼YLENTER()回复于 2005-05-11 11:04:44 得分 10
写这样一个函数就没问题拉。
<%! String trans(String chi)
{
String result=null;
byte temp[];
try{
tem=chi.getByte("iso-8859-1");
result=new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println(e.toString());
}
return result;
}
%>
Top
8 楼mind_1220(大灰狼)回复于 2005-05-11 11:04:59 得分 10
据我以前的经验
我把以前在resin + win2k的代码 放到
linux tomcat 后
使用用佯的数据库
都会出现乱麻的问题
但是以前写进去的就是对
新写入的就是错的!
所以我判断是 写入的错误
而不是读出的错误Top
9 楼tmpx(沙漠之鹰)回复于 2005-05-11 11:10:41 得分 0
写这样一个函数就没问题拉。
<%! String trans(String chi)
{
String result=null;
byte temp[];
try{
tem=chi.getByte("iso-8859-1");
result=new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println(e.toString());
}
return result;
}
%>
我用了,还是不行
不过问题解决了,出在字符集上,读出数据时不能用
new_last_name =new String (rs.getString("last_name").getBytes("iso-8859-1"),"GB2312");
new_first_name =new String (rs.getString("first_name").getBytes("iso-8859-1"),"GB2312");
要用
new_last_name =new String (rs.getString("last_name").getBytes("iso-8859-1"),"GBK");
new_first_name =new String (rs.getString("first_name").getBytes("iso-8859-1"),"GBK");
可能是数据库字符集的原因,我的系统是win2000 server
有知道原因的高手请给我解惑Top
10 楼anchor1(聪头)回复于 2005-05-11 11:15:16 得分 5
new_last_name =new String (rs.getString("last_name").getBytes("8859-1"),"gb2312")Top
11 楼Maple99(Maple)回复于 2005-05-11 11:24:54 得分 30
GB2312一共收录了7445个字符,太少了,而GBK则收录21886个符号,最新的GB18030则更多Top
12 楼liwenchao()回复于 2005-05-11 12:01:17 得分 20
Jsp中文乱码小议
-------郭鹏
Jsp是一个很热门的话题,但让大多数人都头痛的是JSP页面中的乱码问题,笔者身有体会;曾为了解决中文乱码问题郁闷了好几天,试了很多方法都不行。最后在JSP专业人士的帮助下,终于解决。之后笔者曾对此做了一些小的研究。
首先我们先了解一下问题的原因。一般情况在在每个JSP页的头部都有这样一条语句:
<% page contentType="text/html; charset=gb2312" %>这条语句决定了此页面使用GB2312编码形式,而在数据库中一般用的是iso-8859-1字符集存储数据. 而Java程序在处理字符时默认采用统一的ISO-8859-1字符集(体现Java国际化思想),所以在添加数据时,默认的字符集编码是iso-8859-1,而页面采用的是GB2312,所以就出现乱码问题。为解决此问题应在存储的时候把GB2312换转成iso-8859-1。有此时候在读出时也会出现乱码,那么只需反过来就可以了,把iso-8859-1转换成GB2312。
具体实例归纳了以下几点,也许会对大家有一些帮助。
1、在建立JSP页面时应该注意在jsp页面的头部加入一下代码
<% page contentType="text/html; charset=gb2312" %>
此方法是解决JSP页面显示时的乱码。
2、有时还需在HTML代码中的<head></head>中加入这句
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
3、在bean中用的是iso-8859-1编码,在jsp中一般用GB2312编码,处理此类乱码问题如下
String str=new String(strName.getBytes(“iso-8859-1”),”GB2312”);
具体用到此方法的地方为,当提交表单到bean(bean的功能是存储数据到数据库)表单提交后,数据库中存入的数据确变成了????,因此在调用bean之前应对编码进行转换,方法:String str=new String(request.getParameter().getBytes(“iso-8859-1”),”GB2312”);
这样,存入数据库的数据库的数据就可见了。
4、对于页面间的参数传递也可以用这个方法来处理乱码问题:
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{}
return "null";
}
5、在表单定义时加上这个属性有时也可以解决表单提交过程中出现的乱码
<form enctype="multipart/form-data"></form>
6、提交英文字符能正确显示,如果提交中文时就会出现乱码。原因:浏览器默认使用UTF-8编码方式来发送请求,而UTF-8和GB2312编码方式表示字符时不一样,这样就出现了不能识别字符。解决办法:通过request.setCharacterEncoding("gb2312")对请求进行统一编码,就实现了中文的正常显示。
Top
13 楼wzg725(志洋)回复于 2005-05-11 12:39:04 得分 0
遇到过与楼主同样的问题(mysql)
写了这样一方法:
public static String convertStr(String str){
try{
if (str == null)
str = "";
if (!str.equals(""))
{
str = new String(str.getBytes("ISO-8859-1"),"GB2312");
}
}catch (UnsupportedEncodingException e){
e.printStackTrace();
//throw new SystemException(e);
}
return str;
}
在JSP中:
<%=Utils.convertStr((String)map.get("ADDRESS"))%>
其它需注意的问题如楼上所说.Top




