Java Script And Jsp (Wiki forum at Coderanch) (original) (raw)
A lot of questions get asked in the JSP forum about mixing JSP and JavaScript. The simple answer is that it just cannot be done!
JSP is a server-side templating system that creates an HTML page (or other text document) on the server. Anything in this page that is not a JSP action or directive is simply template text that is ignored. HTML, CSS, and JavaScript code is just so much gobble-dee-gook to the JSP engine. It doesn't know anything about it, and it is simply passed into the response as is.
Once the HTML page has been formatted, it is sent to the client (usually a browser) for evaluation and display. All JSP-ness is completely gone at this point.
It is at the browser that the JavaScript is evaluated. And the browser doesn't even know that the page was generated by a JSP, let alone interact with it.
So, the JSP executes on the server and doesn't pay any attention whatsoever to JavaScript code and other page markup. And the JavaScript executes on the client, where even the fact that JSP is being used is unknown.
Never shall they meet! It's even worse than Romeo and Juliet, who at least got to get it on for a short time.
For more information about exactly how a JSP is processed, read The Secret Life of Java Server Pages