从 URI 获取锚点 [英] Get anchor from URI

查看:32
本文介绍了从 URI 获取锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

blabla.rdf#mark

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

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

欢迎任何帮助.

推荐答案

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

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

例如,以下是 Chrome 在提交 http://example.com/#foobar 后生成的确切请求(使用 Wireshark 记录):

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

看,没有#foobar.所以服务器应用程序无法读取它.

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

您可以使用一些 JavaScript 魔法将锚点存储在 cookie 或隐藏的输入字段中,或者您喜欢的任何巫毒教义中.但是对于不是来自您自己的站点的请求,它永远不会起作用.在查询字符串的服务器部分制作您需要的任何内容并将锚点仅用于仅用于 JavaScript 的任务 - 或者使用它在简单的 HTML 文档中导航 - 但那是 90 年代;)

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 ;).

这里是提到的 RFC 1808 的重要部分:

Here's the important part from the mentioned RFC 1808:

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

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.

这篇关于从 URI 获取锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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