Blog

Discovering hostname in Vaadin

By  
Sami Ekblad
Sami Ekblad
·
On Oct 2, 2023 11:42:05 AM
·

Have you ever needed to know where your application is being run? Like the typical localhost:8080 or vaadin-form-example.demo.vaadin.com? As you likely know, Vaadin allows you to build web applications in Java, and the UI runs on the server-side JVM environment. Compared to client-side frameworks, this enables you to access server resources directly. One such thing is the name of the server or host we are running. But which hostname do you mean? 

Since applications are typically run in cloud environments, clusters, and behind proxies, you should consider the suitable method. In this post, we look into six different ways to retrieve the server's hostname where your application is running. This post will explore several approaches, discuss their pros and cons, and provide minimal code examples and expected outputs. 

1. VaadinServletRequest

HttpServletRequest httpServletRequest = ((VaadinServletRequest) VaadinRequest.getCurrent()).getHttpServletRequest();
String hostname = httpServletRequest.getServerName();

This is likely the default you would use in a Vaadin application. It returns the server name without the port number where your Vaadin application is running. E.g., “localhost”. 

Pros:

  • Direct and straightforward.
  • Uses Vaadin’s built-in classes.

Cons:

  • Not working in non-Vaadin applications.
  • Depending on whether you are behind the front-proxy, this can be different from what the user sees. 

2. HTTP Header

String hostname = VaadinRequest.getCurrent().getHeader("host");

Easy way to get the value of the "host" HTTP header, typically containing the server name and possibly the port number. E.g., “localhost:8080.” Depending on whether you are behind the front-proxy this can be different from what the user sees. 

Pros:

  • Simple and concise.
  • Relies on standard HTTP headers, making it broadly applicable.

Cons:

  • It may include a port number, requiring additional parsing if only the hostname is needed.

3. InetAddress

InetAddress addr = InetAddress.getLocalHost();
String hostname = addr.getHostName();

This retrieves the hostname of the local machine from the network layer. In MacOS, this is the name you have assigned to the computer. Something like “SamisLaptop.local

Pros:

  • Uses Java’s native networking library.
  • Not Vaadin-specific and works in any Java app.

Cons:

  • May throw UnknownHostException, which needs to be handled separately.
  • Retrieves the local machine's hostname, which might not always be the same as the server name.

4. Environment Variable

String hostname = System.getenv("HOSTNAME"); // Unix-like
if (hostname == null) {
    hostname = System.getenv("COMPUTERNAME"); // Windows
}

The hostname is typically determined by the system's environment variables, and this value is likely the same as when using InetAddress.

Pros:

  • Platform-independent, works on Unix-like systems and Windows.
  • Not Vaadin specific and usable in any Java app.

Cons:

  • Depends on system configuration.
  • May return null if the environment variables are not set.

5. Shell Command

String hostname = new BufferedReader(
    new InputStreamReader(Runtime
        .getRuntime()
        .exec("hostname")
        .getInputStream()))
        .readLine()
        .trim();

The hostname is retrieved by executing the shell command hostname. Typically, it returns the same as the InetAddress method.  

Pros:

  • Utilizes native shell command.
  • Not dependent on Vaadin-specific classes.

Cons:

  • May throw an IOException, which needs to be handled.
  • Platform-dependent, may not work on all operating systems.

6. ActiveViewLocation

String path = UI.getCurrent().getInternals().getLastHandledLocation().getPath();

Okay, this is not exactly the hostname, but since sometimes the applications are deployed in sub-context, this is a way to retrieve that. The path is the last handled location in the Vaadin UI.

Pros:

  • Provides additional information about the UI state.
  • Utilizes Vaadin’s UI class.

Cons:

  • Returns the path, not the actual hostname.
  • Tightly coupled with Vaadin APIs.

7. JavaScript

Page page = UI.getCurrent().getPage();
page.executeJs("return document.location.host;").then(r -> {
    String hostname = r.asString();
});

Our last method is to get the hostname using asynchronous JavaScript in the browser. This will return the hostname that is visible to the user. It is handy when you want to construct public URLs.

Pros:

  • Get exactly the URL the user sees in the browser address bar.
  • The returned hostname is publicly visible.

Cons:

  • Asynchronous, the result is obtained via a callback.
  • Relies on client-side execution, and may vary depending on browser behavior.
  • Hostname does not necessarily point to the same physical server every time. 

Conclusion

Each of these methods has its unique advantages and limitations. You can build more robust and versatile Vaadin applications by understanding the different approaches. Depending on your application's requirements and constraints, you can choose the best method. 

Have you found any other methods? You can use Vaadin to build highly complex networked applications that can be configured for multi-tenancy and can even be used to control and change the hostname. Let us know what you have found!

Sami Ekblad
Sami Ekblad
Sami Ekblad is one of the original members of the Vaadin team. As a DX lead he is now working as a developer advocate, to help people the most out of Vaadin tools. You can find many add-ons and code samples to help you get started with Vaadin. Follow at – @samiekblad
Other posts by Sami Ekblad