关于Servlet从GET请求中获取中文参数后,中文参数显示“?”

Servlet在从Get请求中获取中文参数后,显示“?”

解决方法是:在Servlet获取的参数使用

URLDecoder.decode()进行编码;

下面是我的例子:传入的url为http://localhost:8080/test/RoomQueryServlet?mr_location=1楼101(浏览器直接输入该URL,Android客户端看最后)

1.改正前:


        String mr_location = request.getParameter("mr_location");


        System.out.println(mr_location);

打印的结果是:

1?101

2.改正后


        String mr_location = request.getParameter("mr_location");
        mr_location = URLDecoder.decode(mr_location,"UTF-8");
        System.out.println(mr_location);

结果:

1楼101

关于Android客户端的传值例子如下,本来以为客户端也要使用URLEncoder.encode()进行编码,但是发现没有也可以成功:

以下是代码

            //URL地址
            String path = "http://"+IP+"/test/RoomQueryServlet";
            path = path + "?mr_location=" + mr_location;
//            path = URLEncoder.encode(path,"UTF-8");
            Log.d("WEBSERVICE", "executeHttpGet: "+path);

            connection = (HttpURLConnection) new URL(path).openConnection();
            connection.setConnectTimeout(3000);//设置超时时间
            connection.setReadTimeout(3000);
            connection.setDoInput(true);
            connection.setRequestMethod("GET");//设置获取信息方式
            connection.setRequestProperty("Charset","UTF-8");//设置接收数据编码格式

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值