逃脱&在网址中

发布于 2024-08-19 21:25:37 字数 147 浏览 1 评论 0原文

我正在使用 jsps,并且在我的 url 中我有一个变量值,例如“L & T”。现在,当我尝试使用 request.getParameter 检索它的值时,我只得到“L”。它识别“&”作为分隔符,因此它不会被视为整个字符串。

我该如何解决这个问题?

I am using jsps and in my url I have a value for a variable like say "L & T". Now when I try to retrieve the value for it by using request.getParameter I get only "L". It recognizes "&" as a separator and thus it is not getting considered as a whole string.

How do I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌上青苔 2024-08-26 21:25:37
java.net.URLEncoder.encode("L & T", "utf8")

这会输出 URL 编码的内容,作为 GET 参数就可以了:

L+%26+T
java.net.URLEncoder.encode("L & T", "utf8")

this outputs the URL-encoded, which is fine as a GET parameter:

L+%26+T
鸠书 2024-08-26 21:25:37

URL 中的文字与符号应编码为:%26

// Your URL
http://www.example.com?a=l&t

// Encoded
http://www.example.com?a=l%26t

A literal ampersand in a URL should be encoded as: %26

// Your URL
http://www.example.com?a=l&t

// Encoded
http://www.example.com?a=l%26t
盗心人 2024-08-26 21:25:37

您需要对参数进行“URL 编码”以避免此问题。 URL 查询字符串的格式为:
...?<名称>=<值>&<名称>=<值>&<等>
所有 都需要进行 URL 编码,这基本上意味着转换所有可能被错误解释的字符(例如 &)转换成%转义值。请参阅此页面了解更多信息:
http://www.w3schools.com/TAGS/ref_urlencode.asp

如果您使用 Java 重新生成问题 URL,可以使用以下方法:
String str = URLEncoder.encode(input, "UTF-8");

在其他地方(某些模板或 JS 或原始标记)生成 URL,您需要从源头修复问题。

You need to "URL encode" the parameters to avoid this problem. The format of the URL query string is:
...?<name>=<value>&<name>=<value>&<etc>
All <name>s and <value>s need to be URL encoded, which basically means transforming all the characters that could be interpreted wrongly (like the &) into %-escaped values. See this page for more information:
http://www.w3schools.com/TAGS/ref_urlencode.asp

If you're generating the problem URL with Java, you use this method:
String str = URLEncoder.encode(input, "UTF-8");

Generating the URL elsewhere (some templates or JS or raw markup), you need to fix the problem at the source.

白龙吟 2024-08-26 21:25:37

您可以使用 Spring Web 中的 UriUtils#encode(String source, String encoding)。该实用程序类还提供了仅对 URL 的某些部分进行编码的方法,例如 UriUtils#encodePath

You can use UriUtils#encode(String source, String encoding) from Spring Web. This utility class also provides means for encoding only some parts of the URL, like UriUtils#encodePath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文