Reasonable porous structure and proper heteroatom doping have great influences on electrode materials for supercapacitors. Herein, O, N, and P codoped hierarchical porous carbon (ONPC) was synthesized through a one-pot carbonization−activation strategy by using chitosan as the carbon precursor, phosphoric acid as the activator, and melamine foam as the skeleton. The use of melamine foam facilitated the formation of an interconnected carbon framework and introduced more nitrogen functional groups. Owing to the hierarchical pore structure, large surface area (1588 m 2 g −1 ), and significant heteroatom contents of O (8.61%), N (7.95%), and P (1.34%) in the carbon matrix, the 700 °C carbonized sample ONPC-700 had a specific capacitance of 230 F g −1 at 0.2 A g −1 . A high energy density of 7.5 Wh kg −1 at 100 W kg −1 was achieved for the supercapacitor fabricated with two identical ONPC-700 electrodes. Furthermore, the capacitance retention of the device was evaluated to be as high as 86.53% after 8000 cycles.