Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting server address and application name

Tags:

java

servlets

jmx

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?

like image 637
Daniel Szalay Avatar asked Apr 13 '11 07:04

Daniel Szalay


People also ask

How do you find the IP address of a server?

Following are the 3 ways you can check website IP address:Check your Welcome Mail: IP address of the server is typically mentioned in the welcome email by the company. Use Ping Command: You can ping the webserver with the CLI, and find the webserver. Global DNS Checker for IP Lookup: Use Global DNS checker tool online ...

How do I find my Java server URL?

In Java, you can use InetAddress. getLocalHost() to get the Ip Address of the current Server running the Java app and InetAddress. getHostName() to get Hostname of the current Server name.


1 Answers

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
like image 109
Simon G. Avatar answered Sep 22 '22 03:09

Simon G.