使用 Java 获取 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/256932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getUrl using Java
提问by Tsundoku
Is there a way of getting the websites absolute URL (http://www.domain.com/) using Java? because I've googled a bit but I only come across with having to make 2 or 3 classes to create that function =/
有没有办法使用 Java获取网站的绝对 URL(http://www.domain.com/)?因为我在谷歌上搜索了一下,但我只遇到必须创建 2 或 3 个类来创建该函数 =/
Update:
更新:
The thing is I am trying to create a crawler that will give me some information and among that I'd like to get the URL of the webpage it's getting the information from. I'm developing this in JAVA and what I meant to say was that I was wondering if there was some getUrl(); or any method like that to get me the Url, because I know it can be done but I've only done it writing a whole other class to retrieve the url and then inherit it and use it further...hope it made it clearer
问题是我正在尝试创建一个爬虫,它会给我一些信息,其中我想获取它从中获取信息的网页的 URL。我正在用 JAVA 开发这个,我想说的是我想知道是否有一些 getUrl(); 或任何类似的方法来获取 URL,因为我知道它可以完成,但我只编写了一个完整的其他类来检索 url,然后继承它并进一步使用它......希望它更清楚
回答by Paul Croarkin
The question is not really clear, but I'll make the assumption that you are trying to get the path from within a Servlet.
问题不是很清楚,但我会假设您正在尝试从 Servlet 中获取路径。
String realPath = getServletConfig().getServletContext().getRealPath(relativePath);
回答by Paul Croarkin
I'm assuming you just want the domain from a JSP, however you may find you need the entire URL including the prefix, domain, path and parameters. The easiest way to get this quickly is to use the Request object and build it. Have a look here for more info:
我假设您只需要来自 JSP 的域,但是您可能会发现您需要整个 URL,包括前缀、域、路径和参数。快速获取此信息的最简单方法是使用 Request 对象并构建它。在这里查看更多信息:
http://www.exforsys.com/tutorials/jsp/jsp-request-object.html
http://www.exforsys.com/tutorials/jsp/jsp-request-object.html
Here is Sun's API on the HttpServletRequest interface:
这是 Sun 在 HttpServletRequest 接口上的 API:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html
回答by Hyman Leow
Could you be more specific? Your question states:
你可以再详细一点吗?你的问题说:
Is there a way of getting the websites absolute URL (http://www.domain.com/) using Java?
有没有办法使用 Java获取网站的绝对 URL(http://www.domain.com/)?
By "the website" which website are you asking for? I can see multiple ways of interpreting your question:
通过“网站”,您要求的是哪个网站?我可以看到多种解释您的问题的方式:
- Given a URL, if there a way to get the hostname portion of it?
- Given a relative path, how do you get the full path?
- Within the context of a Servlet, is there a way to get the name of the deployed server?
- 给定一个 URL,是否有办法获取它的主机名部分?
- 给定一个相对路径,你如何获得完整路径?
- 在 Servlet 的上下文中,有没有办法获取已部署服务器的名称?
etc...
等等...

