The phrase can be understood more clearly by adding punctuation and quotation marks: The meaning could thus be rendered, after some reordering and changing a few words, "While John had used 'had,' James had used 'had had.' The teacher had preferred 'had had.'"
A similar, punctuated repetition of had had appears in Jasper Fforde's the Well of Lost Plots; 'Good. Item seven. The had had and that that problem. [...] 'Okay,' said the Bellman, whose head was in danger of falling apart like a chocolate orange, 'let me get this straight: David Copperfield, unlike Pilgrim's Progress, which had had had, had had had had. Had had had had TGC's approval?'
There was a very long pause.
'Right,' said the Bellman with a sigh.