首页 文章

如何让服务器感知它所服务的域名?

提问于
浏览
1

我有一个Java Servlet容器 . 此servlet容器可以提供 localhost:8080192.168.1.2:8080www.development.examplewww.production.example.com . 如何让Servlet容器和/或任何正在运行的servlet感知它所服务的域名?

3 回答

  • 2

    servlet应该能够看到整个请求URL:

    方法1:获取请求URL并自行解析 .

    String url = request.getRequestURL().toString();
    

    方法2:

    String server = request.getServerName()
    

    this答案

  • 1

    对于Servlet,请在request对象上使用 getServerName() 方法 .

    就servlet容器而言,如果你把apache放在容器前面,你就可以用VHost做一些chicanry . 您将不得不查看您的特定servlet容器的文档,以查看他们的HTTP服务器是否支持该级别的VHosts .

  • 0
    String host = request.getHeader("Host");
    

相关问题