The healthcare industry has recently shown much interest in the Internet of Things (IoT). The Internet of Medical Things (IoMT) is a component of the IoTs in which medical appliances transmit information to communicate critical information. The growth of the IoMT has been facilitated by the inclusion of medical equipment in the IoT. These developments enable the healthcare sector to interact with and care for its patients effectively. Every technology that relies on the IoT can have a serious security challenge. Critical IoT connectivity data may be exposed, changed, or even made unavailable to authenticated users in the case of such attacks. Consequently, protecting IoT/IoMT systems from cyber-attacks has become essential. Thus, this paper proposes a machine-learning- and a deep-learning-based approach to creating an effective model in the IoMT system to classify and predict unforeseen cyber-attacks/threats. First, the dataset is preprocessed efficiently, and the Harris Hawk Optimization (HHO) algorithm is employed to select the optimized feature. Finally, machine learning and deep learning algorithms are applied to detect cyber-attack in IoMT. Results reveal that the proposed approach achieved an accuracy of 99.85%, outperforming other techniques and existing studies.