Which file defines the Tomcat Port?

The configuration of tomcat presents in server.xml and located under {Tomcat installation folder}\conf\, open the server.xml file and search the following statement:

<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />

The default HTTP port is 8080 configured in the HTTP connector, these types of servers are designed to be able to listen on (almost) arbitrary ports, for example, says we want to change the port to 80, which is the standard HTTP port. You can change the value of port attribute as showed below.

<Connector port="80" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />

How do I Get  Tomcat Port Number in Java Programming Code?

I created a static method getTomcatPortFromConfigXml to read the tomcat port defined in server.xml.

public static Integer getTomcatPortFromConfigXml(File serverXml) {
   Integer port;
   try {
      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
      domFactory.setNamespaceAware(true); // never forget this!
      DocumentBuilder builder = domFactory.newDocumentBuilder();
      Document doc = builder.parse(serverXml);
      XPathFactory factory = XPathFactory.newInstance();
      XPath xpath = factory.newXPath();
      XPathExpression expr = xpath.compile
        ("/Server/Service[@name='Catalina']/Connector[count(@scheme)=0]/@port[1]");
      String result = (String) expr.evaluate(doc, XPathConstants.STRING);
      port =  result != null && result.length() > 0 ? Integer.valueOf(result) : null;
   } catch (Exception e) {
     port = null;
   }
   return port;
}

You can call this method anytime to get the tomcat published port, notice you need pass the tomcat configurations xml file into this method.