0

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

Community
  • 1
  • 1
Tsundoku
  • 9,104
  • 29
  • 93
  • 127
  • I'm not sure that I understand the question's context. Are you writing a web application with Java and usinga servlet say to get the url? Or are you trying to resolve domain names from a name server? – Vincent Ramdhanie Nov 02 '08 at 14:55

3 Answers3

1

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);
jjnguy
  • 136,852
  • 53
  • 295
  • 323
Paul Croarkin
  • 14,496
  • 14
  • 79
  • 118
1

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

0

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

Jack Leow
  • 21,945
  • 4
  • 50
  • 55