Thursday, 15 July 2010

java - Get root domain from request -


Consider the URL below as an example.

Is there any way by which I can get "example.com" as an output. I know that a method is servletrequest.getServerName () . This gives me test1.example.com

to appreciate any help.

In

, you can obtain different parts of the URI by using the methods below. You can also use them for rebuilding the URL fragment (debugging, or other tasks):

  // Example: http: // myhost: 8080 / people ? Lastname = Fox and age = 30 string uri = request.getScheme () + ": //" + // "http" + ": // request.getServerName () + //" myhost "": "+ request.getServerPort () "//": "+" 8080 "request.getRequestURI () + //" / people "(request.getQueryString ()! = Tap?"? "+ Request.getQueryString ():" "); // "?" + "Lastname = fox and age = 30"   

So closest we need you

"root domain":

For "root domain", you get back from string with getServerName () Have to work. This is necessary because the Servlet will not have any method to call "Host" before calling or from the time that .com (this is called a com in your network The machine can be - and not just a suffix - who knows?).

For the pattern you provided (one-third + second level + C ohm / net), you should get the following:

  string domain = request .getServerName (). ReplaceAll (". * \\. (? =. * \\.)", "");   

The following input / output will be given:

  www.test.com -> Test.com test1.example.com - & gt; Example.com a.b.c.d.e.f.g.com - & gt; G.com www.com - & gt; Www.com.com - & gt; Com    

No comments:

Post a Comment