Can someone test this example and share the results?
http://timothypowell.net/blog/?p=23
When I do:
var myVar = '<% request.getContextPath(); %>';
alert(myVar);
I get : '<% request.getContextPath(); %>'.
Removing the enclosing single quotes from ‘<% request.getContextPath(); %>’;
gives syntax error.
How can I use the scrptlet or expresion inside a js function?
EDIT: this link has an explanation that helped me:
http://www.codingforums.com/showthread.php?t=172082
It sounds like you are placing the JSP code within a JavaScript page, or at least in a non-JSP page. Scriptlets can only be included in a JSP page (typically configured to be *.jsp).
The statement as presented, if processed by the JSP compiler, would result in myVar being equal to ” as the scriptlet format you are using <% … %> executes Java code between the tags, but does not return a result.
So, to use this tag you would need to manually write a value to the request output stream. To get the desired functionality you need to do the following:
With all that said, scriptlets are viewed as bad practice in most cases. For most cases, your should be using JSTL expressions and custom tags.