浏览器url传参中文时得到null的解决方法


   在写一个中文参数需求的时候遇到了以下问题,经过半天的测试和各种编码,以及网上一些有用没用的资料尝试终于解决
   比如下面的url地址:http://travel.widget.baike.com:8010/travelComment.do?action=queryAllCommentList&docTitle=北京饭店
   在程序中通过下面方法取参数request.getParameter("docTitle");;常理就是你不通过特殊处理也应该是乱码或者编码过的东西才对;但是意外的是null值

   中间各种尝试的过程:
  1.设置请求和相应的编码
    request.setCharacterEncoding("UTF-8");
       response.setCharacterEncoding("UTF-8");
     2.直接转码参数
       System.out.println(new String(request.getParameter("docTitle").getBytes("iso8859-1"),"utf-8"));】
  3.通过url编码参数
    java.net.URLEncoder.encode(docTitle);
    java.net.URLDecoder.decode(docTitle);
   以上方案都已失败告终。

   解决方法:
   下面方面得到了中文参数,并得以传入到后面程序。
        String param = request.getQueryString(); // 得到结果action=queryAllCommentList&docTitle=%B1%B1%BE%A9%B7%B9%B5%EA
        String docTitle = java.net.URLDecoder.decode(param.split("=")[2]);
        System.out.println(docTitle);

 

posted on 2013-09-05 18:17  you Richer  阅读(375)  评论(0编辑  收藏  举报