JAVA : Get Host Name and IP address of Machine?

Below examples is to get local machine host name and IP  address by java api’s. Here also explained about to get host name and IP address by HttpServletRequest.

Classes and Methods

  • : class represents an Internet Protocol (IP) address.
  • : Returns the address of the local host. This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.
  • : Returns the IP address string in textual presentation.
  • : Gets the host name for this IP address.




public class GetIPAndHostName {

public static void main(String[] args) {
InetAddress inetAddress = InetAddress.getLocalHost();
System.out.println("Local IP Address:- " + inetAddress.getHostAddress());
System.out.println("Local Host Name:- " + inetAddress.getHostName());
catch( ex)


Local IP Address:-
Local Host Name:- LAPTOP-FacingIssuesOnIT

Host Name from HttpServletRequest

in below example code for web application will get host name from HttpServletRequest.

public void getAppStatus(HttpServletRequest request, HttpServletResponse response) {

String hostName=request.getServerName();
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {


Here we explained about to get host name and ip address of local machine and server machine from HttpServletRequest.

More Samples

For more other JAVA/JDBC sample code follow link JAVA/JDBC Issues.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s