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

查看:365
本文介绍了从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天全站免登陆