I first want to know if there is a built-in way of getting a subdomain from a url using pure servlets?
And then if spring has any helpers?
So my urls would be like:
jonskeet.stackoverflow.com
Where JonSkeet is the subdomain.
I will create a filter that will load a object based on the subdomain value.
BTW, when creating a filter, is there a way to order the filters to make sure they all fire in a specific order?
While a subdomain will appear before your TLD, a subdirectory link will include the subdirectory name after the original TLD. For example: Subdomain example: mysubdomain.mywebsite.com. Subdirectory example: mywebsite.com/mysubdirectory.
To recap, a subdomain is the portion of a URL that comes before the “main” domain name and the domain extension. For example, docs.themeisle.com .
Yes it is. Spring-MVC uses DispatcherServlet under the hood. Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service exporters.
By implementing subdomains on your website, you can create separate sections of content and services for your website without having to create new domain names for each part. Subdomains will also make it easier for users to find what they need, all in one place.
I doubt there is a special API for this. But you can get it from HttpRequest using request.getServerName().split("\\.")[0]
. It seems it is easy enough.
Limitation is that you cannot support "subdomain" that contains dot characters, e.g. jon.skeet.stackoverflow.com
.
Use Guava.
Gradle:
dependencies {
compile group: 'com.google.guava', name: 'guava', version: '19.0'
...
}
Java:
private String getSubdomain(HttpServletRequest req) {
String site = req.getServerName();
String domain = InternetDomainName.from(site).topPrivateDomain().toString();
String subDomain = site.replaceAll(domain, "");
subDomain = subDomain.substring(0, subDomain.length() - 1);
return subDomain;
}
So, "jon.skeet.stackoverflow.com" will return "jon.skeet".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With