Inside a dreary Lower Manhattan courtroom on a recent Wednesday, Justice Juan M. Merchan convened a special session for people with mental health troubles who had landed in legal jeopardy. He calmly counseled them, praised any signs of progress and shook the hand of one man who, thanks to medication, had turned his life around.

But on April 15, a different type of criminal defendant will enter the same courtroom and test the judge’s equanimity: Donald J. Trump.

Mr. Trump’s trial on charges that he covered up a sex scandal before and after the 2016 presidential election will bring a weekslong maelstrom that no other judge in New York’s vast judiciary has ever experienced. It will be the first prosecution of a former U.S. president, a man who revels in attacking the legal system and its judges.

Since the Manhattan district attorney charged Mr. Trump last year, the former president has used campaign emails, social media invective and repetitive legal filings to attack the judge’s integrity and family. Last week, the former president demanded for a second time that Justice Merchan step aside, citing his daughter’s position at a Democratic consulting firm that worked for the 2020 Biden campaign.

Known as a no-nonsense, drama-averse jurist, Justice Merchan, 61, could reject the recusal request in the coming days, as he has so many of Mr. Trump’s last-ditch bids to delay the trial. The judge, who has already reprimanded Mr. Trump’s lawyers for arguments that he considered frivolous, has also issued a gag order to protect prosecutors, witnesses and his own family from Mr. Trump’s vitriol — and yet the former president has continued to post articles with pictures of the judge’s daughter.

The turmoil punctuates the former president’s yearslong assault on the judiciary, an antipathy that intensified with his political rise and mounting legal peril. Facing four criminal indictments in four different cities, he has demonized the judicial system, stoking anger in his base as he seeks to retake the White House as the presumptive Republican nominee this year.