title: How to set the value for textfield in JSP from Javascript
body: <p>I need to generate a random number when user selects on the "Generate" button and set the value in the textfield. How do i set the "output variable" in the "randomNum" textfield.</p>
  
  <p>HTML codes:</p>
  
  <pre><code>&lt;form action="submit.jsp" method= "post" name="privateKey"&gt;
              Random String: &lt;input type="text" name="randomNum" onkeyleave="generate()"&gt;
              &lt;input type="button" name="generate" value="Generate!" &gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>     function generate() {
             char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
             StringBuilder sb = new StringBuilder();
             Random random = new Random();
             for (int i = 0; i &lt; 20; i++) {
                 char c = chars[random.nextInt(chars.length)];
                 sb.append(c);
             }
             String output = sb.toString();
          }
  </code></pre>