Getting server address and application name
Environment: NetBeans 6.9.1, GlassFish 3.1
I have a Java Web Application. How to get the server address and开发者_JS百科 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?
The HttpServletRequest
object will give you what you need:
HttpServletRequest#getLocalAddr()
: The server's IP address as a stringHttpServletRequest#getLocalName()
: The name of the server receiving the requestHttpServletRequest#getServerName()
: The name of the server that the request was sent toHtppServletRequest#getLocalPort()
: The port the server received the request onHttpServletRequest#getServerPort()
: The port the request was sent toHttpServletRequest#getContextPath()
: The part of the path that identifies the application
Inside a servlet you can get it like this
public static String getUrl(HttpServletRequest request) {
return request.getRequestURL().toString();
}
精彩评论