But it's not working. any suggestion on how to do this?
Moving the script (learn to indent!) to an external file should not be an issue. Chances are you are not addressing the file properly and so it's not actually getting loaded.
Gander at the JSP FAQ for info on successfully using server-relative URLs to address resources such as JS files.