Mr. Obama flew to a New Jersey military base to thank troops for their service heading into the holiday season and to defend his handling of national security at a time of multiple challenges. While critics have said the campaign against the Islamic State has not succeeded, the president argued that it had made a difference.

“Because of you, we have blunted their momentum and we have put them on the defensive,” Mr. Obama told 3,000 troops in camouflage uniforms and civilian defense personnel gathered in a hangar here. “And these terrorists are learning the same thing that the leaders of Al Qaeda have learned the hard way. They may think that they can chalk up some quick victories, but our reach is long. We do not give up. You threaten America, you will have no safe haven. We will find you.”