从 URI 获取锚点

发布于 2024-08-09 02:58:38 字数 169 浏览 2 评论 0原文

我正在编写一个 JSP/Servlet,并且尝试获取 URI 的锚点部分,例如:

blabla.rdf#mark

如何从请求中获取标记?显然 request.getParameter() 不起作用?

欢迎任何帮助。

I'm writing a JSP/Servlet and I'm trying to get the the anchor part of the URI e.g:

blabla.rdf#mark

How do I get my mark from my request? Obviously request.getParameter() doesn't work?

Any help would be welcome.

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

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

发布评论

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

评论(3

简单 2024-08-16 02:58:38

这是不可能的,因为客户端不会将“锚点部分”发送到服务器

作为示例,以下是 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 魔法,将锚点存储在 cookie 中、隐藏的输入字段中或任何您喜欢的巫术中。但它永远不会适用于并非源自您自己网站的请求。更简单的做法是在服务器上将您需要的内容作为查询字符串的一部分,并仅将锚点用于仅 JavaScript 任务 - 或者使用它在简单的 HTML 文档中导航 - 但这已经是 90 年代的事了;)。

以下是提到的 RFC 1808 中的重要部分:

请注意,片段标识符(及其前面的“#”)是
不被视为 URL 的一部分。不过由于它是常用的
在与 URL 相同的字符串上下文中,解析器必须能够
当片段存在时识别它并将其作为片段的一部分放在一边
解析过程。

This isn't possible as clients don't send the "anchor part" to the server

As an example, here's the exact request that Chrome generated after submitting http://example.com/#foobar (recorded using 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

See, no #foobar. So there's no way a server application can read it.

You could do some JavaScript magic to store the anchor in a cookie or in a hidden input field or whatever voodoo you're into. But it won't ever work for requests that don't originate from your own sites. It's simpler to make whatever you need on the server part of the query string and use the anchor only for JavaScript-only tasks - or use it to navigate inside a simple HTML document - but that's so 90s ;).

Here's the important part from the mentioned 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.

淡淡绿茶香 2024-08-16 02:58:38

我想您想要制作一个可添加书签的 Ajax Web 应用程序,并且想要替换 DOM 的某些片段而不重新加载整个页面。 (否则您可以使用查询参数。)正如 sfussenegger 提到的,浏览器不会将“anchor”传输到服务器。解决方案是在客户端。

Javascript window.location.hash 提供锚点信息。您可以将事件处理程序附加到 window.onload 事件,该事件获取 window.location.hash 并将其通过 Ajax XHttpRequest 传输到服务器。您捕获响应并将其构建到 DOM 中。

不幸的是,这是两次客户端-服务器往返。

有关此内容的更多信息,请访问 ajaxpatterns

I guess you want to make a bookmarkable Ajax web-application and you want to replace certain fragments of the DOM without reloading the full page. (Otherwise you could use query parameters.) As sfussenegger mentions, browser doesn't transmit 'anchor' to the server. The solution is on the client-side.

Javascript window.location.hash gives the anchor information. You can append an event handler to the window.onload event which grabs window.location.hash and transmits it to the server in an Ajax XHttpRequest. You catch the response and build it into the DOM.

Unfortunately, it's two client-server roundtrips.

More on this at ajaxpatterns.

又怨 2024-08-16 02:58:38

此解决方案仅在按下提交按钮后才有效,但是:您可以使用 JavaScript 在表单上放置一个隐藏值,该值设置为 document.location 的值。这将准确地告诉您浏览器将您的地址视为什么。

This solution would only work after a submit button has been pushed, but: you could use javascript to place a hidden value on your form that is set to the value of document.location. That would tell you exactly what the browser is seeing as your address.

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