现在在oracle db中保存的是gb或者big5编码的数据,在原来的网页上显示的是gb或者big5,所以从数据库中读出来的东西在网页上显示并没有问题。但是现在客户要求所有的网页都要转成utf-8编码,但db不能转编码。我尝试过把JSP中
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
转成
<meta http-equiv= "Content-Type " content= "text/html; charset=utf-8 " />
然后在jsp中加入
String gb_value = resultSet.getString( "name ");
byte[] b = gb_value.getBytes( "gb2312 ");
String utf8_name = new String(b, "UTF-8 ");
结果输出 <%=utf8_name%> 时,显示乱码?由于只有jsp文件,没有web.xml。所以我本来想有filter来做都不可以。请问各位大虾有什么办法帮帮小弟呢?100分求最佳答案!
------解决方案--------------------
这句byte[] b = gb_value.getBytes( "gb2312 ")改成byte[] b = gb_value.getBytes( "ISO-8859-1 ")试试.
------解决方案--------------------
ByteArrayInputStream in = new ByteArrayInputStream(gb_value.getBytes);
InputStreamReader reader = InputStreamReader(in, "GBK ");
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = OutputStreamWriter(out, "UTF-8 ");
int i = 0;
while((i = reader.read()) != -1)
writer.write(i);
String utf8_name = out.toString( "UTF-8 ");
试试看吧,没环境没测
建议你把这个写成公用的函数,读GBK的字符串返回UTF-8的字符串。
------解决方案--------------------
不止光改动 <meta http-equiv= "Content-Type " content= "text/html; charset=utf-8 " />
还要文件-〉右键-〉Properties-〉Text file encoding。这个文件编码格式也给改掉