Extracting Host Name and Port from HTTP/HTTPS Requests: A Java Servlet Guide

Dec 03, 2025 · Programming · 11 views · 7.8

Keywords: Java | HTTP | Servlet | URL extraction | reverse proxy

Abstract: This article provides an in-depth exploration of how to accurately extract host name, port, and protocol information from HTTP or HTTPS requests in Java Servlet environments. By analyzing core methods of the HttpServletRequest interface, such as getScheme(), getServerName(), and getServerPort(), it explains how to construct base URLs. Specifically for reverse proxy or load balancer scenarios, practical strategies for handling SSL termination are discussed, including using the X-Forwarded-Proto header, configuring RemoteIpValve, and setting up multiple connectors. With code examples, the article offers solutions ranging from simple to complex, assisting developers in meeting URL reconstruction needs across different deployment environments.

Introduction

In modern web application development, it is often necessary to extract host name, port, and protocol information from incoming HTTP or HTTPS requests to construct base URLs for subsequent request forwarding or redirection. For example, in a multi-application deployment within a JBoss container, when Application 1 receives a request, it may need to send a corresponding request to Application 2, requiring accurate extraction of URL prefixes such as http://example.com/. Based on the Java Servlet specification, this article delves into how to achieve this functionality and discusses handling strategies in complex network architectures.

Core Method Analysis

The Java Servlet API provides the HttpServletRequest interface, which includes several methods for obtaining URL components of a request. The most basic method is getScheme(), which returns the string "http" or "https", directly indicating the protocol used by the request. Combined with getServerName() (returning the host name) and getServerPort() (returning the port number), the base part of the URL can be reconstructed. For instance, for a request like http://example.com/context?param1=123, the following code demonstrates how to extract http://example.com:

String scheme = request.getScheme();
String serverName = request.getServerName();
int port = request.getServerPort();
String baseUrl = scheme + "://" + serverName;
if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
    baseUrl += ":" + port;
}

This code first retrieves the protocol, host name, and port, then constructs the base URL. Note that standard ports (80 for HTTP, 443 for HTTPS) are typically omitted in URLs, so conditional checks avoid adding unnecessary port numbers. This approach is straightforward and suitable for most direct-access scenarios.

Handling Reverse Proxies and Load Balancers

In real-world production environments, Servlet containers are often behind reverse proxies or load balancers, which may terminate SSL connections, converting HTTPS requests to HTTP before forwarding them to the container. In such cases, getScheme() might return "http" instead of "https", leading to incorrect protocol information. To address this, several common strategies are available:

  1. Using the X-Forwarded-Proto Header: Many load balancers (e.g., Apache) set the X-Forwarded-Proto header to indicate the original request's protocol. This value can be obtained via request.getHeader("x-forwarded-proto") and used preferentially if present. For example:
    String scheme = request.getHeader("x-forwarded-proto");
    if (scheme == null) {
        scheme = request.getScheme();
    }
    This ensures correct identification of HTTPS requests even in proxy environments.
  2. Configuring RemoteIpValve: In JBoss or Tomcat, RemoteIpValve can be configured to automatically process proxy headers, making methods like getScheme() return correct values based on headers such as X-Forwarded-Proto. This requires setup in the server configuration file, e.g., adding Valve configuration in Tomcat's server.xml. This method simplifies code but relies on proper load balancer configuration.
  3. Setting Up Multiple Connectors: Another approach is to configure two connectors in the Servlet container, one for HTTP and one for HTTPS, allowing the container to receive HTTPS requests directly. This avoids dependency on proxy headers but increases deployment complexity, typically used for specific architectural needs.

These strategies should be chosen based on the specific environment to ensure accurate URL extraction.

Supplementary Methods and Considerations

In addition to the above methods, HttpServletRequest provides getRequestURL() and getRequestURI(), which can be used to extract URLs. For example, getRequestURL() retrieves a StringBuffer of the full URL, from which the base URL can be derived by removing the URI portion. The following code illustrates this process:

StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
int idx = (((uri != null) && (uri.length() > 0)) ? url.indexOf(uri) : url.length());
String host = url.substring(0, idx); // base URL
idx = host.indexOf("://");
if (idx > 0) {
    host = host.substring(idx); // remove scheme if present
}

This method allows direct manipulation of the URL string but may be less flexible than component-wise extraction, especially when dealing with non-standard ports or proxy scenarios. In practice, it is recommended to prioritize the combination of getScheme(), getServerName(), and getServerPort(), as they more clearly separate URL components, facilitating logic control and error handling.

Conclusion

Extracting host name and port from HTTP or HTTPS requests is a common task in Java web development. By appropriately using methods from HttpServletRequest, such as getScheme(), getServerName(), and getServerPort(), base URLs can be easily constructed. In reverse proxy or load balancer environments, extra attention is needed to ensure protocol accuracy, leveraging the X-Forwarded-Proto header or server configurations for adaptation. The code examples and strategies provided in this article aim to assist developers in addressing scenarios from simple to complex, ensuring reliability and maintainability in URL extraction. In real applications, it is advisable to select the most suitable method based on the deployment architecture and conduct thorough testing to validate its behavior.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.