String str=rs.getString("goodstype"); str = new String(str.getBytes(),"gb2312");
首先,这个可能不是servlet转码的问题,你写一个application估计出来的还是???,因为从sql server中读取的字符串是用utf-8编码,所以,你需要对读取得字符串进行转码,把它转成gb2312,大概的方法好像是 str = new String(str.getBytes("UTF-8"),"gb2312"); 参照 titaninwinter(泰坦巨人) 的方法,呵呵
已经试了很多转换方式,还是不行。 resp.setContentType("text/html;charset=gb2312"); new String(str.getBytes("GBK"),"GBK"); new String(str.getBytes("gb2312"),"GBK"); new String(str.getBytes("ISO8859_1"),"GBK"); new String(str.getBytes(),"GBK"); new String(str.getBytes("GBK"),"gb2312"); new String(str.getBytes("gb2312"),"gb2312"); new String(str.getBytes("ISO8859_1"),"gb2312"); new String(str.getBytes(GBK),"ISO8859_1"); new String(str.getBytes(gb2312),"ISO8859_1"); new String(str.getBytes(ISO8859_1),"ISO8859_1"); new String(str.getBytes(),"ISO8859_1");
数据库已确认为中文数据.
request.setCharacterEncoding("gb2312");
resp.setContentType("text/html;carset=gb2312");这个也是
req.setCharacterEncoding("gb2312");这个不能编译,好象找不到setCharacterEncoding方法,下面是源码
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
{
req.setCharacterEncoding("gb2312");
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");
out.println("类别:"+str+"<BR>");
}
out.println("</table>");
}
catch (Exception fe)
{
out.println(fe.getMessage());
}
out.println("</body>");
out.println("</html>");
out.close();
}
}
str = new String(str.getBytes(),"gb2312");
str = new String(str.getBytes("UTF-8"),"gb2312");
参照 titaninwinter(泰坦巨人) 的方法,呵呵
resp.setContentType("text/html;charset=gb2312");
new String(str.getBytes("GBK"),"GBK");
new String(str.getBytes("gb2312"),"GBK");
new String(str.getBytes("ISO8859_1"),"GBK");
new String(str.getBytes(),"GBK");
new String(str.getBytes("GBK"),"gb2312");
new String(str.getBytes("gb2312"),"gb2312");
new String(str.getBytes("ISO8859_1"),"gb2312");
new String(str.getBytes(GBK),"ISO8859_1");
new String(str.getBytes(gb2312),"ISO8859_1");
new String(str.getBytes(ISO8859_1),"ISO8859_1");
new String(str.getBytes(),"ISO8859_1");
输出的是乱文
"书籍"输出为"锟?咯锞硷井"
"体育器材"输出为"锟?骏锟?拷锟?糠锞诧?"
"操作系统"输出为"锞诧?锟?糠锟?镜锟?境"
str.getBytes("8859-1"),"gb2312");
只能输出一个"8859-1"
改成"8859_1"后还是输出的"????"
可以说的明白一点吗.
还有,在编译servlet时加上 -encoding ISO8859_1
加resp.setContentType("text/html;charset=ISO8859_1");
用-encoding ISO8859_1方式编译.
源码上的 out.println("类别:"+str+"<BR>");
这里的"类别"输出成了"?à±?"
数据库中的数据还是"????????"