title: Creating a dropdown program in JSF
body: <p>I need to create a program that accesses a database and passes the values to a dropdown in JSF. I am trying to figure out how that can be done. I Got it working for the 1st dropdown, but the second dropdown is tricky, as it needs a input parameter to work. Here is my JSF code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"&gt;
  &lt;head&gt;    
  &lt;title&gt;Dropdown List&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;select id="mySelect" onchange="myFunction()"&gt;
          &lt;h:outputText value="#{helloWorld.getResultSet1()}" escape="false" /&gt;    
          &lt;f:attribute name="action" value="1" /&gt;
      &lt;/select&gt;
      &lt;br /&gt;
      &lt;select id="mySelect1"&gt;
      &lt;/select&gt;
      &lt;script&gt;
          var x1 = document.getElementById("mySelect1");    
          x1.style.display = 'none';
          function myFunction() {
              x1.style.display = 'block';
              x1.innerHtml = '&lt;h:outputText value="#{helloWorld.getResultSet2()}" escape="false" /&gt;';
          }
          document.getElementById('myForm:hidden2').value = new_value;
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is my java code:</p>
  
  <pre><code>public String id = "1";
  
      public void print(String name) {
          System.out.println(name);
      }
      public String getResultSet2(String name) {
          String toReturn = "";
  
          try {
              rs = st.executeQuery("SELECT * FROM CS_EXAMS where name ="+ name);
              System.out.println(name);
              while (rs.next()) {
                  toReturn = toReturn + "&lt;option&gt;" + rs.getString("exam_name") + "&lt;/option&gt;";
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
          return toReturn;
      }
  
      public String getResultSet1() {
          String toReturn = "";
  
          try {
              rs = st.executeQuery("SELECT * FROM CS_EXAMS");
              System.out.println("name");
              while (rs.next()) {
                  toReturn = toReturn + "&lt;option&gt;" + rs.getString("exam_name") + "&lt;/option&gt;";
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
          return toReturn;
  
      }
  </code></pre>