The project i am working on have multiple instances(i.e different website) running in single code base. Based on the URL we show corresponding website.
For example, if http://www.uswebsite.com/ the we show US website. and ifhttp://www.cawebsite.com/ will show Canadian website. The code that is written to detect this is
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
String server` = httpRequest.getServerName();
If request is from the http://www.uswebsite.com/ then according to above code String server = uswebsite so we have additional code written to pop up the corresponding site
we are now planning to include the European instance in the same code base and i see that the URL for the europe site is going to be like this http://www.europewebsite.co.uk/. With getServer() as above will it fetch String server=europewebsite. By having the .co.uk at the end will the above code still get String server=europewebsite or String server=europewebsite.co Please advise as i can’t test it out this in my local.
The wording of your question is rather confusing, so I’m assuming that you simply want to extract the hostname from a hierarchical URL string.
The most reliable way to do it is to construct an instance of
java.net.URLorjava.net.URIfor the url string, and use the relevant getter to extract the hostname.On the other hand, if you are trying to detect the virtual hostname for the current request in a servlet, then your
httpRequest.getServerName()gives you that hostname as it appears in the first line of the HTTP request (unless something has rewritten it …).On the other hand, if you simply want to extract
"europewebsite"from a DNS name like"www.europewebsite.co.uk", there is no general solution. I’d recommend converting the DNS to all lowercase and doing a lookup in a table that you have pre-initialized with all of the variations that you are prepared to recognize.