I don't think technology or psychology is yet sufficiently advanced to be able to stop this stuff altogether.
However horrible it may sound, I think our best course of action is to accept that these things do happen occasionally. That doesn't mean we shouldn't fight them, and that doesn't mean we shouldn't do our best to prevent them, but it does mean that we must accept that we'll never get to 100%.
If we can move forward with the idea that we'll do our best to reduce these attacks as much as possible, rather than with the idea that we'll eliminate them, I think the result will be much better.
As for what to do, I think the combination of smart intelligence (as opposed to NSA-style dragnets) and emergency response (to minimize the effects of attacks after they happen) are the way to go.
Edit: whee, downvotes. If you think I'm wrong, or even just right but insensitive, would you mind explaining yourself instead of (or, heck, in addition to) downvoting? I honestly don't see anything wrong with my comment.