title: Passing a servlet variable into javascript
body: <p>I'm looking to pass a servlet variable, myVar that is passed into a JSP page, and pass it to  JavaScript. The JavaScript is an external javascript that is included into the JSP page.</p>
  
  <p>I have a button that calls the JavaScript function, but I'm unable to pass any of the variables that are passed into the JSP page through the servlet. The button is not a part of a form. </p>
  
  <p>I've tried in a function in JavaScript to call:</p>
  
  <pre><code>var x = '&lt;%=myVar%&gt;';
  </code></pre>
  
  <p>AND</p>
  
  <pre><code>var x = '${myVar}';
  </code></pre>
  
  <p>AND </p>
  
  <pre><code>var x = '&lt;%= (String)request.getParameter("myVar") %&gt;';
  </code></pre>
  
  <p>However, <code>x</code> is always a string of what I inputted.</p>
  
  <p>I'm not using AJAX or JQuery. Any ideas?</p>
  
  <p>Example Code is a simplified version: (so the button is actually a drop down that calls the js when I change the value, however, I want other variables that are not part of the drop down to be called in changeCLass)</p>
  
  <p>Servlet side:</p>
  
  <pre><code>request.setAttribute("otherVars","tests");
  </code></pre>
  
  <p>JSP:</p>
  
  <pre><code>&lt;script type="text/javascript" src="external.js"&gt;&lt;/script&gt;
  
  &lt;select name="vars" id="myVars" onchange="changeClass(this)"&gt;
  &lt;option value='1' selected="selected"&gt;1&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>external.js included in JSP:</p>
  
  <pre><code>function changeClass(newVarX) {
  
      var newVarId =newVarX.value;
      var tID = '${otherVars}';
  
      alert(newVarId + " " + tID);
  }
  </code></pre>
  
  <p>Output:
  1 $(otherVars}</p>
  
  <p>but output should be:
  1 tests</p>