0

I would like to know in GWT is there an easy way to get host name from url WITHOUT java.net.*; Since client side doesn't support this package.

Input ejw.example.com/anotherexample?fun=no
output example.com

Input https://www3.example.com/yeteagainanotherexample?fun=miserable
output example.com
5
  • ejw.example.com, www3.example.com and example.com are all different domain names. Commented Jun 20, 2014 at 20:00
  • I meant host. And I am looking for a built-in method if it exist. Commented Jun 20, 2014 at 20:04
  • They are different host names as well :), because they can - and often do - resolve to different IP addresses. What you look for in your examples is a second-level domain name, but you have to make sure that this is really what you need. Commented Jun 20, 2014 at 20:05
  • Correct. 2nd level domain name. I would write my own method but if there was a built-in class that replaces java.net that would be great. Commented Jun 20, 2014 at 20:13
  • You may find an answer to this question very helpful: stackoverflow.com/questions/4452916/… Commented Jun 20, 2014 at 20:50

2 Answers 2

1
com.google.gwt.user.client.Window.Location.getHost()
Sign up to request clarification or add additional context in comments.

Comments

0

Try with com.google.gwt.regexp.shared.RegExp that is used for regular expressions with features like Javascript's RegExp, plus Javascript String's replace and split methods (which can take a RegExp parameter).

Try below regex that captures all the everything between first dot and first forward slash and get it at match index 1.

https*://\w+\.(.*?)/

Here is demo on debuggex and regexr

Sample code:

String url="https://www3.example.com/yeteagainanotherexample?fun=miserable";

System.out.println(RegExp.compile("https*://\\w+\\.(.*?)/").exec(url).getGroup(1));

Note: I am not good in regex so please change the regex pattern as per your need. Read more about

6 Comments

Why do you think he needs his own domain name? I think the OP is talking about URLs in general.
And you did not provide the second-level domain name that the OP is looking for.
What about blog.inside.example.co.uk? Your regex fails. There can be up to 127 dots in domain name.
@AndreiVolgin Sorry I have very little knowledge about URL domain name. It's just a example. The regex can be modified or simply use String#split() method.
String.splt() is also not trivial, because you need to distinguish between blog.example.com and `blog.example.com.me" - just to make up an example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.