This line of code should be placed in the HTML <script> in the .jsp file. In this way, JspServlet process scriptlets (and other special JSP / EL expressions).
<script>var myVar = '<%= request.getContextPath() %>';</script>
Note that <%= %> is the correct syntax for printing a variable, <% %> does not.
Or, if it is intended for servicing in a standalone .js file, you need to rename it to .jsp and add the following to the top of the file (and change the URL <script src> accordingly):
<%@page contentType="text/javascript" %> ... var myVar = '<%= request.getContextPath() %>';
Thus, the JspServlet process it, and the browser will be prompted to interpret the body of the JSP response as JavaScript instead of HTML.
Not related to a specific problem, note that scripting is considered bad practice . Use EL.
var myVar = '${pageContext.request.contextPath}';
Balusc
source share