title: How to pass a Java String to a JavaScript function in a JSP page
body: <p>I am looking to pass a string from my Java to a javascript showContent function. Both the Java and javascript are contained in a JSP page. The string <code>strLine</code> contains XML content I want to display using the showContent function. </p>
  
  <p>My Java</p>
  
  <pre><code>        try{    
          //Open the file that is the first command line parameter
              FileInputStream fstream = new FileInputStream(table.get(xmlMatch));                 
  
              // Get the object of DataInputStream
              DataInputStream in = new DataInputStream(fstream);
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              String strLine;
  
          //Read File Line By Line
              while ((strLine = br.readLine()) != null)   
              {
                  out.println (strLine);
              }  
  </code></pre>
  
  <p>The Javascript (I have to credit Peter for supplying me with this in another <a href="https://stackoverflow.com/questions/12784193/css-javascript-to-display-content-of-next-previous-div-on-user-click">question</a>)</p>
  
  <pre><code>&lt;script type="text/javascript" language="JavaScript"&gt;
  function showContent()
  {
  document.getElementById('showContent').innerHTML = "printed content";
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>I have tried replacing the above "printed content" with <code>"strLine";</code> <code>(strLine);</code> and <code>("strLine");</code></p>
  
  <p>I also tried set <code>strLine</code> as a session attribute using 
  <code>session.setAttribute("strLine", strLine);</code> and using <code>"&lt;%=strLine%&gt;";</code> but the result was null printed to the screen. </p>
  
  <p>Any help with this would be great.</p>
  
  <p>The HTML</p>
  
  <pre><code>&lt;a href="#" onclick="showContent()"&gt;Next! &lt;%=keywords%&gt; concept &lt;/a&gt;
  &lt;div id="showContent"&gt;&lt;/div&gt;
  </code></pre>