How can I get a the host name (with port) that a servlet is at

后端 未结 5 1477
滥情空心
滥情空心 2020-12-08 19:06

I thought ServletContext might provide a method. Does the getAttribute() method of ServletContext provide any help i.e. is there an attribute name (maybe \"host\", \"port\")

5条回答
  •  北海茫月
    2020-12-08 19:12

    The ServletRequest object that has been passed to your doGet, or doPost method has getServerName and getServerPort methods that provide this information.

    eg

    public void doGet(ServletRequest request, ServletResponse response) {
        System.out.println("Host = " + request.getServerName());
        System.out.println("Port = " + request.getServerPort());
    }
    

提交回复
热议问题