HttpServletRequest request = ...; int remotePort = request.getRemotePort(); System.out.println("Remote port: " + remotePort);
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int remotePort = request.getRemotePort(); if (remotePort == 80) { // Send a redirect response to HTTPS response.sendRedirect("https://" + request.getServerName() + request.getRequestURI()); } // ... }In this example, we use the getRemotePort() method to determine if the request was made over port 80 (HTTP). If it was, we send a redirect response to HTTPS by constructing a URL with the HTTPS protocol and the same server name and request URI as the original request. The getRemotePort() method is part of the javax.servlet.http.HttpServletRequest interface, which is part of the Java Servlet API.