How to get the domain name from the request in a J

2019-06-16 21:08发布

问题:

If a single servlet serves data from two domains example1.com and example2.com, how do you retrieve the domain information from the request in a Java servlet?

The purpose is to perform different actions depending on the domain.

回答1:

Very easy, javax.servlet.ServletRequest.getServerName(). It also provides further methods to retrieve request information, getScheme(), getServerPort()...

Edit: If you have a web server guarding your application server, it must be configured to support this, otherwise getServerName() returns the name of your application server which may not be what you want...