Semantics and Types for Safe Web Programming