我有一个Java Servlet容器 . 此servlet容器可以提供 localhost:8080 , 192.168.1.2:8080 , www.development.example 或 www.production.example.com . 如何让Servlet容器和/或任何正在运行的servlet感知它所服务的域名?
localhost:8080
192.168.1.2:8080
www.development.example
www.production.example.com
servlet应该能够看到整个请求URL:
方法1:获取请求URL并自行解析 .
String url = request.getRequestURL().toString();
方法2:
String server = request.getServerName()
见this答案
对于Servlet,请在request对象上使用 getServerName() 方法 .
getServerName()
就servlet容器而言,如果你把apache放在容器前面,你就可以用VHost做一些chicanry . 您将不得不查看您的特定servlet容器的文档,以查看他们的HTTP服务器是否支持该级别的VHosts .
String host = request.getHeader("Host");
3 回答
servlet应该能够看到整个请求URL:
方法1:获取请求URL并自行解析 .
方法2:
见this答案
对于Servlet,请在request对象上使用
getServerName()
方法 .就servlet容器而言,如果你把apache放在容器前面,你就可以用VHost做一些chicanry . 您将不得不查看您的特定servlet容器的文档,以查看他们的HTTP服务器是否支持该级别的VHosts .