ITPub博客

首页 > IT基础架构 > 网络安全 > Tomcat3.1中文乱码解决办法

Tomcat3.1中文乱码解决办法

原创 网络安全 作者:rainytag 时间:2007-06-28 22:01:13 0 删除 编辑
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("
");
// 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("
");
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("");
%>
. 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!

 [@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9650775/viewspace-922018/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 深入解剖Tomcat
下一篇: Redhat下安装Tomcat
请登录后发表评论 登录
全部评论

注册时间:2008-01-01

  • 博文量
    652
  • 访问量
    2207668