【问题标题】:How would I ignore a single quotation mark within a string that interacts with my embedded quotation marks?如何忽略与嵌入引号交互的字符串中的单引号?
【发布时间】:2020-11-20 20:11:52
【问题描述】:

【问题讨论】:

  • 你可以使用双引号甚至三引号,更多信息here
  • <a href=\".....\"
  • URLEncoding: John%27s%20Smith
  • 顺便说一句,如果您将价格作为 URL 参数,我一定会在那里购物并以0.01 购买您所拥有的一切
  • @StephenP 这真是太卑鄙和有趣了哈哈

标签: java html


【解决方案1】:

处理 href 中的任意和未知字符的最佳方法是使用 URL 编码,然后使用哪个引号(单引号或双引号)或引号的嵌套方式都无关紧要.

Java 有一个 URLEncoder 类专门用于执行此操作。

如果 URL 的其余部分已知且已修复,通常您会在查询字符串中使用它。我建议构建查询字符串,对其进行 url 编码,然后 然后 将其添加到您的输出中,例如:

final String query =
  "id=" + rst.getString(1) +
  "&name=" + rst.getString(2) +
  "&price=" + rst.getString(4);

out.println("<tr><td class='col-md-1'> <a href='addcart.jsp?" + 
            URLEncoder.encode(query) +
            "etc. the rest");

这个Guide to Java URL Encoding/Decoding 可能会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 2023-03-31
    • 2013-04-02
    相关资源
    最近更新 更多