您当前的位置:飞翔学院-IT中国 →
编程开发 →
JSP → 文章内容
Tomcat3.1中文乱码解决办法
作者:佚名 来源:不详 发布时间:2007-11-29 23:56:21
作者:jdeveloper
This is for Tomcat3.1 only
I downloaded Tomcat3.1 for windows and linux and found that one must do some more work to
display Chinese characters correctly.Below is my experience:
. For Jsp
In Jsp files, you can output Chinese characters easily,that is just do like:
<% out.println("北京"); %>
However, when you display resultset from Database you must add this to your Jsp
files to let them show Chinese characters as you want.
<%@ page import="java.sql.*" contentType="text/html;charset=GB2312" %>
<%
String driverName = "oracle.jdbc.driver.OracleDriver";
Driver d;
Connection con;
Statement stmt;
ResultSet results;
try {
d =
(Driver)Class.forName(driverName).newInstance();
con = DriverManager.getConnection("jdbc:oracle:thin:ndb/ndb@111.222.1.103:1521:YZB");
stmt = con.createStatement();
stmt.execute("select * from hjctest");
results = stmt.getResultSet();
StringBuffer buf = new StringBuffer();
String temp;
try {
ResultSetMetaData rsmd = results.getMetaData();
int numCols = rsmd.getColumnCount();
int i, rowcount = 0;
// get column header info
for (i=1; i <= numCols; i++){
if (i > 1) buf.append(",");
buf.append(rsmd.getColumnLabel(i));
}
buf.append("<br>");
// break it off at 100 rows max
while (results.next() && rowcount < 100){
// Loop through each column, getting the column
// data and displaying
for (i=1; i <= numCols; i++) {
if (i > 1) buf.append(",");
buf.append((results.getString(i)));
}
buf.append("<br>");
rowcount++;
}
results.close();
out.write(buf.toString());
}
catch (Exception e) {
System.out.println(e);
return;
}
con.close();
}
catch (Exception e) {
out.println("error: " + e.toString());
}
out.println("</BODY></HTML>");
%>
. For Servlet
To display Chinese Characters from a servlet you should set the contentType like this
.....
response.setContentType("text/html;Charset=GB2312");
ServletOutputStream out = response.getOutputStream();
out.println("北京");
...
. Comments
Maybe, this will save you from wasting time to display Chinese characters
from Tomcat3.1 .
Good luck!