Getting server address and application name

ENVIRONMENT NetBeans 6.9.1, GlassFish 3.1

I have a Java web application. How to get server address and application name dynamically? A solution of “2in1” would be best for me: http://localhost:8080/AppName/ .

Is there a practical way to get this information?

EDIT . Let's say the AppName value is fixed, so I only need the host address. Is it possible to get it through JMX? Any other ways?

Thanks in advance, Daniel

+7
source share
2 answers

The HttpServletRequest object will provide you with what you need:

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

Inside the servlet, you can get it like this:

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

All Articles