使用 Java 获取 URL

发布于 2024-07-08 02:26:39 字数 350 浏览 7 评论 0原文

有没有办法使用获取网站绝对 URL (http://www.domain.com/)爪哇? 因为我用谷歌搜索了一下,但我只遇到必须创建 2 或 3 个类来创建该函数=/

更新:

问题是我正在尝试创建一个爬虫,它将为我提供一些信息,其中我会比如获取从中获取信息的网页的 URL。 我正在用 JAVA 开发这个,我的意思是我想知道是否有一些 getUrl(); 或任何类似的方法来获取 Url,因为我知道这是可以完成的,但我只是编写了一个完整的其他类来检索 url,然后继承它并进一步使用它......希望它能让它更清楚

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 =/

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

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

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

发布评论

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

评论(3

留蓝 2024-07-15 02:26:40

我假设您只需要 JSP 中的域,但是您可能会发现您需要整个 URL,包括前缀、域、路径和参数。 快速获得此信息的最简单方法是使用 Request 对象并构建它。 请参阅此处了解更多信息:

http://www.exforsys。 com/tutorials/jsp/jsp-request-object.html

这是 Sun 的 HttpServletRequest 接口上的 API:

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html

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:

http://www.exforsys.com/tutorials/jsp/jsp-request-object.html

Here is Sun's API on the HttpServletRequest interface:

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html

落叶缤纷 2024-07-15 02:26:40

这个问题不太清楚,但我会假设您正在尝试从 Servlet 内获取路径。

String realPath = getServletConfig().getServletContext().getRealPath(relativePath);

The question is not really clear, but I'll make the assumption that you are trying to get the path from within a Servlet.

String realPath = getServletConfig().getServletContext().getRealPath(relativePath);
你列表最软的妹 2024-07-15 02:26:40

你可以再详细一点吗? 你的问题指出:

有没有办法获取网站
绝对 URL (http://www.domain.com/)
使用Java?

通过“网站”您要查询哪个网站? 我可以看到多种解释你的问题的方法:

  1. 给定一个 URL,是否有办法获取它的主机名部分?
  2. 给定相对路径,如何获得完整路径?
  3. 在 Servlet 的上下文中,有没有办法获取已部署服务器的名称?

ETC...

Could you be more specific? Your question states:

Is there a way of getting the websites
absolute URL (http://www.domain.com/)
using Java?

By "the website" which website are you asking for? I can see multiple ways of interpreting your question:

  1. Given a URL, if there a way to get the hostname portion of it?
  2. Given a relative path, how do you get the full path?
  3. Within the context of a Servlet, is there a way to get the name of the deployed server?

etc...

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