Also, when playing with pila armed infantry, always remember to set them to Fire At Will so they throw their pila before charging automatically - you'll dish out some decent casualties and disrupt their formation as they slowly advance towards you as well. If you want to just charge them without first throwing your pila, hold down the Alt key when ordering the attack (this activates the 'secondary' attack - in the case of pila armed troops, this is their sword).

The rest of the advice posted here is spot on - pinning and flanking after first wearing them down with archers/javs. You could also try hiring some mercenary hoplites to fight fire with fire - they will be better at pinning the enemy phalanx than your hastati.