Sword were used in battle. But, to be pedantic, there is difference between being used and being primary weapons. Humble spear was most often chosen primary weapon because one simple reason - reach (and because it was cheap and easy to make). From old phalanx in classic period to heavy cavalry with lances to pike formations. Swords were expensive and harder to produce, That's why they were also status symbol as much as weapon until 13-14th century. They became standard sidearm in 14th ct, even for common soldiers. Now, there was certainly times when swords were better suited (like indoors) then polearms, but in open warfare, sword were secondary weapons.
Handguns were used even in trench warfare of WW. While rifle was main weapon of choice, in close quarters of trench fighting, handgun was more practical in lot's of cases.
Spear but used more like javelin. Some models had wooden peg holding iron shank so it would bend and brake. Only tip was made of hardened metal to penetrate shield. Even Romans moved away from gladius and pilum. In late period (3-5th ct) they switched to hasta (normal spear) and shield. Hasta became primary weapon, so much so that hastati was sinonimous term for infantry. Same time they moved away from gladius to spatha.