JSP中字符编码转换问题

问题描述:一个input.jsp页面中的参数,传递到另外一个save.jsp页面上,然后存入到数据库中,如果input.jsp页面输入偶数中文没有问题,输入奇数则出现?,存入数据库的也是?。

问题源码:save.jsp中

<%

   String fileName=request.getParameter("Name");
           System.out.println("=========================================="+fileName);
          fileName= new String(fileName.getBytes("GBK"),"UTF-8");
          System.out.println("=========================================="+fileName);

 %>

 

修改为: 

<%

    request.setCharacterEncoding("ISO-8859-1");
         String fileName=request.getParameter("Name");
         System.out.println("=========================================="+fileName);
            fileName =new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
    System.out.println("=========================================="+fileName);

%>

处理UTF-8和GBK奇偶数不同的问题。

注意一点:遇到字符乱码问题,这里的解决方法是,先将GBK转成ISO-8859-1的编码,然后在由ISO-8859-1编码转成UTF-8。因为ISO-8859-1是一个字节一个字符。

 

 

 

posted @ 2013-10-14 20:57  程序员张越  阅读(921)  评论(0编辑  收藏  举报