我在网上搜到说jsp用GB2312编码,bean用iso8859-1编码。用这段代码
String   str=new   String(request.getParameter().getBytes(“iso-8859-1”),”GB2312”);  
把jsp中的数据存入bean。
我不太多,谁能帮我讲讲?
还有从数据库用bean读出数据显示在jsp页面中。要怎么转码呢?

解决方案 »

  1.   

    String str=new String(request.getParameter().getBytes(“iso-8859-1”),”GB2312”); 
    是在存入数据库之前进行转码,把iso-8859-1转换成gb2312的编码,这样支持中文。并不是说bean用的是iso8859-1的编码。
    从数据库读出来的时候,只要数据库里的不是乱码,一般都不要转码,直接就显示可以了。
      

  2.   

    没有这种说法:说jsp用GB2312编码,bean用iso8859-1编码。
    你要明白,转码的原因是什么?因为有乱码。为什么有乱码,原因很多。比如,jsp,数据库,java工程的编码不一致。等等。String str=new String(request.getParameter().getBytes(“iso-8859-1”),”GB2312”);  拆开写,你就会看的明白。String paramValue = request.getParameter("userName");//假如此时已经乱码,因为是从jsp传过来的,如果已经乱码,则需要转码,至于转换成什么格式,是需要区别的,一般而言,jsp编码传递过来的是西文,也就是ISO-8859-1,需要转为跟你数据库编码一致,jsp编码一致的即可。
    String str=new String(paramValue.getBytes(“iso-8859-1”),”GB2312”);