Getting server address and application name

和自甴很熟 提交于 2019-12-18 04:13:20

问题


Environment: NetBeans 6.9.1, GlassFish 3.1

I have a Java Web Application. How to get the server address and the application name dynamically? The '2in1' solution would be the best for me: http://localhost:8080/AppName/.

Is there a practical way to get that information?

Let's say the value of AppName will be fixed, so I only need the host address. Is it possible to retrieve it via JMX? Any other ways?


回答1:


The HttpServletRequest object will give you what you need:

  • HttpServletRequest#getLocalAddr(): The server's IP address as a string
  • HttpServletRequest#getLocalName(): The name of the server receiving the request
  • HttpServletRequest#getServerName(): The name of the server that the request was sent to
  • HtppServletRequest#getLocalPort(): The port the server received the request on
  • HttpServletRequest#getServerPort(): The port the request was sent to
  • HttpServletRequest#getContextPath(): The part of the path that identifies the application



回答2:


Inside a servlet you can get it like this

public static String getUrl(HttpServletRequest request) {
    return request.getRequestURL().toString();
}


来源:https://stackoverflow.com/questions/5645916/getting-server-address-and-application-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!