This paper proposes a pattern mapping approach using a double guard technique to detect XSS attacks in multi-tier web applications. The double guard deploys intrusion detection systems at both the front-end web server and back-end database server. It uses virtualization to create containers for each user session, mapping patterns between web requests and database queries. A step-wise pattern mapping algorithm is presented to detect XSS attacks by applying rules to identify encoded values and annotations in requests. The approach was tested on sample Java web applications and was able to detect typical XSS attacks.