Usually Java comes along with a large set of sensible constants to use. HTTP and HTTPS default port numbers as assigned by IANA (80 and 443) would be sensible constants. I checked the javadoc of java.net.URL, java.net.HttpURLConnection and javax.net.ssl.HttpsURLConnection but didn’t find them there. Are those constants somewhere in the JavaSE? If not, are they somewhere available in the classes a web application on Tomcat has access to, e.g. org.apace.catalina or coyote? Just because I don’t like magic numbers in my code…
Do I have to do this in URL composition?
if(port != new URL("http://example.com/").getDefaultPort() &&
port != new URL("https://example.com/").getDefaultPort()){
stringBuilder.append(":");
stringBuilder.append(port);
}