"on the last line of defense," 1909, from an image attested by 1715, from a quote attributed to William of Orange (1650-1702), who is said to have uttered it defiantly during the French invasion of 1672; if so, originally in a Netherlands context.
We have no space to enter into the detail of the heroic struggle maintained by the young stadtholder and his faithful Dutchmen; how they laid their country under water, and successfully kept the powerful invader at bay. Once the contest seemed utterly hopeless. William was advised to compromise the matter, and yield up Holland as the conquest of Louis XIV. "No," replied he; "I mean to die in the last ditch." A speech alone sufficient to render his memory immortal. [Agnes Strickland, "Lives of the Queens of England," London, 1847]
Half-wits are fleas; so little and so light,
We scarce could know they live, but that they bite.
[Dryden, "All for Love"]
Phrase out of half wit "half out of one's mind" was in Middle English (late 14c.). Half-witted "lacking common sense" is from 1640s.