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

2019-06-16 21:13发布

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条回答
我命由我不由天
2楼-- · 2019-06-16 21:47

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...

查看更多
登录 后发表回答