servelt请求参数值是否包含&符? [英] servelt request parameter value contains ampersand?

查看:92
本文介绍了servelt请求参数值是否包含&符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网址下方.

http://localhost:8080/servlet?user=John&message=hai&hello&recipient=scott

在上面的网址中,我有3个请求参数,如下所示.

In above url i have 3 request parameters as below.

user=John
message=hai&hello
recipient=scott

这里的问题是message请求参数的值,因为这里的值包含ampsend(&).当我尝试request.getParameter("message")时,我只会得到hai而不是hai&hello.我该如何解决这个问题?

Here the problem is with message request parameter's value.because here its value contains ampersend (&). when i try request.getParameter("message") then i get only hai but not hai&hello. How can i solve this issue?

谢谢!

推荐答案

尝试此操作,而不是....&message=hi%26hello.....我是说,对它进行编码.

Try this, instead ....&message=hi%26hello..... I mean, encode it.

[已编辑]

正如您所说,您无法控制它,它是旧版应用程序,他们无法修复它;那么我想您仍然可以使用URLEncoder对该URL进行编码.

As you said you have no control over it, and it is legacy application and they cannot fix it; then you can still, I suppose, use URLEncoder to encode the URL.

String encodedUrl = URLEncoder.encode(url, "UTF-8");
// Then use encodedUrl as you were using url.

[已编辑]

..或将其视为String.很简单,不是吗?

..or just treat it as a String. Simple, isn't it?

// Please refactor.
String[] paramPart = url.split("?");
String[] params = paramPart[1].split("&");
Map<String, String> paraMap = new HashMap<>();
for(int i=0; i<params.length; i++) {
  String[] keyValue;
  if(params[i].contains("=")) {
    keyValue = params[i].split("=");
    paraMap.put(keyValue[0], keyValue[1]);
  } else {
    params[i-1] = params[i-1] + "&" + params[i];
    keyValue = params[i-1].split("=");
    paraMap.put(keyValue[0], keyValue[1]);
  }
}

这篇关于servelt请求参数值是否包含&符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆