Nowadays many web sites makes extensive use of client side scripts and offer numerous features that contribute rich content to enhance the user experience. This trend makes the web developers fail to sanitize user inputs properly and increase the frequency of cross-site scripting attack (XSS). In Cross-site scripting an attacker causes a victim's browser to execute the malicious code with the privileges of a trusted host. Hence we have proposed a Ant colony approach to detect and prevent XSS attacks. At first a web page is scanned for the attack prone html attributes and the nodes are referred to as sensitive links. The Ant during their travel classifies the paths as vulnerable or most vulnerable according to their strength. Hence such paths are then replaced using ESAPI rules to prevent attack.