Topic: servlet如何能输出中文?????

  Print this page

1.servlet如何能输出中文????? Copy to clipboard
Posted by: ABI
Posted on: 2002-12-25 00:19

JSP中用
<%@ page contentType="text/html;charset=ISO8859_1" %>
就可以做数据库中读出中文

servlet中用
resp.setContentType("text/html;charset=ISO8859_1");
resp.setContentType("text/html;charset=gb2312");
resp.setContentType("text/html;charset=GBK");
输出的都为"????"号.

2.读取中文 [Re: ABI] Copy to clipboard
Posted by: jamesji
Posted on: 2002-12-26 11:41

用 JDBC 执行 SELECT 语句从服务器端读取数据(中文)后,将数据用 APPEND 方法加到 TextArea(TA) ,不能正确显示。但加到 List 中时,大部分汉字却可正确显示。

将数据按“ISO-8859-1” 编码方式转化为字节数组,再按系统缺省编码方式 (Default Character Encoding) 转化为 STRING ,即可在 TA 和 List 中正确显示。
程序段如下:

dbstr2 = results.getString(1);
//After reading the result from DB server,converting it to string.
dbbyte1 = dbstr2.getBytes(“iso-8859-1”);
dbstr1 = new String(dbbyte1);

在转换字符串时不采用系统默认编码方式,而直接采用“ GBK” 或者 “GB2312” ,从数据库取数据都没有问题。

3.这是源码 [Re: ABI] Copy to clipboard
Posted by: ABI
Posted on: 2002-12-28 02:04

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class servletcon extends HttpServlet
{

    public void service(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
      
resp.setContentType("text/html");
      PrintWriter out = new PrintWriter(resp.getOutputStream());
     try
  {  
   out.println("<html>");
   out.println("<head><title>aaa</title></head>");
   out.println("<body>");  
        Class.forName("allaire.jrun.jdbc.JRunDriver");
        Connection conn =DriverManager.getConnection("jdbc:jrun:sqlserver://127.0.0.1:1433;databaseName=test","sa", "");
Statement stmt=conn.createStatement();
String strSQL="Begin use fancy SELECT goodstype FROM goods end";
   ResultSet rs=stmt.executeQuery(strSQL);
out.println ("<table>");

   while(rs.next())
   {
     String str=rs.getString("goodstype");
     byte[] str1 =str.getBytes("gb2312");
     str=new String(str1);
out.println("类别:"+str+"<BR>");
   }
   out.println("</table>");
  }
  catch (Exception fe)
  {
     out.println(fe.getMessage());
  }
  out.println("</body>");
  out.println("</html>");
  out.close();
}
}

4.[Re:ABI] [Re: ABI] Copy to clipboard
Posted by: ABI
Posted on: 2002-12-28 02:07

byte[] str1 =str.getBytes("gb2312");
byte[] str1 =str.getBytes("GBK");
byte[] str1 =str.getBytes("iso-8859-1");
byte[] str1 =str.getBytes("iso8859_1");
byte[] str1 =str.getBytes("UTF-8");

上面用哪一种都是输出的"???"号
除了用UTF-8输出的是这些乱文"锟岋骏锟擄拷锟嗭糠锞诧縿"

5.Re:servlet如何能输出中文????? [Re: ABI] Copy to clipboard
Posted by: dxping
Posted on: 2002-12-28 17:03

编译的时候用:
javac -encoding iso8859-1 ******.java

就可解决,我在tomcat4.1中通过

6.????? [Re: ABI] Copy to clipboard
Posted by: ABI
Posted on: 2002-12-29 01:52

为什么我用javac -encoding iso8859-1 servletcon.java
编译后,"类别" 输出成了"?à±?"
数据中的中文输出还是"?????"

并且用了几种方式编译"GBK""gb2312"这两种类别还是可以输出,数据库中的中文输也是"???"

7.多谢~~ [Re: ABI] Copy to clipboard
Posted by: ABI
Posted on: 2002-12-31 22:24

多谢~~原来问题出在JRun自带的MSSQL驱动上....


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923