我正在编写一个 JSP/Servlet,我正在尝试获取 URI 的 anchor 部分,例如:
blabla.rdf#mark
我如何从我的请求中得到我的标记?显然 request.getParameter()
不行?
欢迎任何帮助。
最佳答案
这是不可能的,因为客户端不会将“锚定部分”发送到服务器
例如,这是 Chrome 在提交 http://example.com/#foobar
后生成的确切请求(使用 Wireshark 记录):
GET / HTTP/1.1
Host: example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/532.3 (KHTML, like Gecko) Chrome/4.0.223.11 Safari/532.3
Cache-Control: max-age=0
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
If-None-Match: "b300b4-1b6-4059a80bfd280"
If-Modified-Since: Tue, 15 Nov 2005 13:24:10 GMT
看,没有#foobar。所以服务器应用程序无法读取它。
您可以施展一些 JavaScript 魔法,将 anchor 存储在 cookie 中或隐藏的输入字段中,或者您喜欢的任何巫术。但它永远不适用于不是来自您自己站点的请求。在查询字符串的服务器部分制作您需要的任何内容并仅将 anchor 用于仅限 JavaScript 的任务更简单 - 或者使用它在简单的 HTML 文档中导航 - 但那是 90 年代;)。
这是上述 RFC 1808 中的重要部分:
Note that the fragment identifier (and the "#" that precedes it) is not considered part of the URL. However, since it is commonly used within the same string context as a URL, a parser must be able to recognize the fragment when it is present and set it aside as part of the parsing process.
关于java - 从 URI 获取 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1637211/