English 中文(简体)
如何把 Java的URL碎块同浏览器一样结合起来?
原标题:How do you combine URL fragments in Java the same way browsers do?

NOTE: I am asking for real world problem, not for theoretical purpose; see the last part of the question -- the same way browsers do the job.


Usually you would see the answer:
new java.net.URL(new java.net.URL(base_url),rel_url).toString

(base_urlrel_url,。 在我的案件中,base_url是I fetdated,rel_url。 来源于“和;a href=. 等值,因此甚至可能是单一“#”性质(例如)。

然而,这种法典并不涉及URL碎块,如两件:

htpp://www.hello.com/1.html

?p=2

我测试了< Firefox>、歌剧、Konqueror,“Web Browser”(Gnome miniy ;-D)——所有这些都把“URL”合并为:

htpp://www.hello.com/1.html?p=2

上面的代码如下:

htpp://www.hello.com/?p=2

Question

How do you combine URL fragments, in a ready for world manner?

我希望,在我开始由自己做教之前,已经有这方面的图书馆。

最佳回答

你们误解了URL。 <代码>?p=2? (也可找到<代码>#foo,通常称为fragment ID<>>或参引<>>>,而且最常用的是跳跃到长期文件的一个部分。) http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax” rel=“nofollow” 在其他许多地方(青年还发现不同地方的URIs和URLs之间的差别)。

无论如何,“URLs”仅指“路程>,“URL-it”部分是指这条道路是绝对还是相对的。 如果你有疑问,并希望附上现有的URL(没有问题说明),那么,这只是为了说明。 如果你不了解你是否有 que,你可以使用URL类的方法进行测试。

如果希望复制浏览器做些什么,则考虑到全程的URLcode>url和严格的

if (s.startsWith("?") || s.startsWith("#")) new java.net.URL(url.toString + s)
else new java.net.URL(url, s)

我们应该 do。 (我不知道不同的浏览器使用的确切代码,但是,这仿照了你描述的如果所有情况都包含在册子里的话,如何使用 que。) 如果你不知道你现有的URLs是否会有问题,那么你可以这样做。

if (s.startsWith("#")) new java.net.URL(url.toString.takeWhile(_ !=  # ) + s)
else if (s.startsWith("?")) new java.net.URL(url.toString.takeWhile(_ !=  ? ) + s)
else new java.net.URL(url, s)
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...