You can't use JSP in a static .js
file.
You can use JSP in a .jsp
file that outputs a JavaScript file.
Output an application/javascript
content type, and then write your JS as normal in the .jsp
file using JSP directives are you wish.
Note that, since the JavaScript file will be requested separately to the HTML file you won't be able to read things like the query string used to request the HTML file from the JSP directives in the JavaScript file (although you could pass them via a session, or add them to the src
of the <script>
element).
As a rule of thumb, however, it is often better to stick to static JS files and include dynamically generated JS as inline <script>
within the HTML document (although keeping such JS to a minimum - just enough to store the data you need). That makes sorting caching rules simpler.