title: how to set an ArrayList object in a dropdown through ajax
body: <p>index.jsp</p>
  
  <pre><code>&lt;%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1" contentType="text/html; charset=UTF-8"%&gt;
  &lt;%@ taglib uri="/struts-tags" prefix="s" %&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  
  
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
  &lt;html&gt;
    &lt;head&gt;
    &lt;!-- &lt;META HTTP-EQUIV="Refresh" CONTENT="10;URL=regd.action"&gt;  --&gt;
      &lt;title&gt;cascading dropdown&lt;/title&gt;
      &lt;script language="javascript" type="text/javascript" src="getDeptNameUsingId.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
  
      &lt;form action="#" method="get" id="regdform" name="regdform"&gt;
      &lt;table&gt;
      &lt;tr&gt;
      &lt;td colspan="2"&gt;&lt;strong&gt;&lt;em&gt;&lt;font color="#800000"&gt;Department of the user&lt;/font&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;
  
                      &lt;tr &gt;
                      &lt;td&gt;&lt;strong&gt;Department :&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;
                      &lt;select name="department" style="color: #800000;font-size: medium;width:150px" onchange="findDeptDetails2(this.options[this.selectedIndex].value)"&gt;
                      &lt;option value=""&gt;-----------------&lt;/option&gt;             
                  &lt;c:forEach items="${allLocations}" var="x"&gt;
                      &lt;option value="${x.getVch_LevelName()}"&gt;${x.getVch_LevelName()}&lt;/option&gt;
                  &lt;/c:forEach&gt;
                      &lt;/select&gt;
                      &lt;/td&gt;&lt;/tr&gt;
  
                      &lt;tr&gt;
                      &lt;td&gt;&lt;strong&gt;Line Department :&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;
                      &lt;select  name="linedepartment" id="linedepartment" style="color: #800000;font-size: medium;width:150px"&gt;
                      &lt;option value=""&gt;-----------------&lt;/option&gt;
                      &lt;/select&gt;
  
  
                      &lt;/td&gt;&lt;/tr&gt;
  
                      &lt;tr&gt;
                      &lt;td&gt;&lt;strong&gt;Office :&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;select name="office" style="color: #800000;font-size: medium;width:150px"&gt;
                      &lt;option value=""&gt;-----------------&lt;/option&gt;
                      &lt;option value="A"&gt;A&lt;/option&gt;
                      &lt;option value="B"&gt;B&lt;/option&gt;
                      &lt;option value="C"&gt;C&lt;/option&gt;
                      &lt;option value="D"&gt;D&lt;/option&gt;
                      &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Section :&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;select name="section" style="color: #800000;font-size: medium;width:150px"&gt;
                      &lt;option value=""&gt;-----------------&lt;/option&gt;
                      &lt;option value="A"&gt;A&lt;/option&gt;
                      &lt;option value="B"&gt;B&lt;/option&gt;
                      &lt;option value="C"&gt;C&lt;/option&gt;
                      &lt;option value="D"&gt;D&lt;/option&gt;
                      &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
  
                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Designation :&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;
                      &lt;select name="designation" style="color: #800000;font-size: medium;width:150px"&gt;
                      &lt;option value=""&gt;-----------------&lt;/option&gt;
                      &lt;option value="A"&gt;A&lt;/option&gt;
                      &lt;option value="B"&gt;B&lt;/option&gt;
                      &lt;option value="C"&gt;C&lt;/option&gt;
                      &lt;option value="D"&gt;D&lt;/option&gt;
                      &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;
                      &lt;td&gt;&lt;strong&gt;Grade :&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;select name="grade" style="color: #800000;font-size: medium;width:150px"&gt;
                      &lt;option value=""&gt;-----------------&lt;/option&gt;
                      &lt;option value="A"&gt;A&lt;/option&gt;
                      &lt;option value="B"&gt;B&lt;/option&gt;
                      &lt;option value="C"&gt;C&lt;/option&gt;
                      &lt;option value="D"&gt;D&lt;/option&gt;
                      &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Physical location :&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;
                      &lt;select name="physicallocation" style="color: #800000;font-size: medium;width:150px"&gt;
                      &lt;option value=""&gt;-----------------&lt;/option&gt;
                      &lt;option value="A"&gt;A&lt;/option&gt;
                      &lt;option value="B"&gt;B&lt;/option&gt;
                      &lt;option value="C"&gt;C&lt;/option&gt;
                      &lt;option value="D"&gt;D&lt;/option&gt;
                      &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;
                      &lt;td&gt;&lt;strong&gt;Employee type :&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;
                      &lt;select name="emptype" style="color: #800000;font-size: medium;width:150px"&gt;
                      &lt;option value=""&gt;-----------------&lt;/option&gt;
                      &lt;option value="A"&gt;A&lt;/option&gt;
                      &lt;option value="B"&gt;B&lt;/option&gt;
                      &lt;option value="C"&gt;C&lt;/option&gt;
                      &lt;option value="D"&gt;D&lt;/option&gt;
                      &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;                
     &lt;td&gt; &lt;input type="submit" value="submit"/&gt;&lt;/td&gt;&lt;/tr&gt;
  
  
      &lt;/table&gt;
  
      &lt;/form&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>getDeptnameUsingId.js</p>
  
  <pre><code>    function findDeptDetails2(v){
  
          var xmlhttp;
          if(v=="")
              {           
              document.getElementById("linedepartment").selectedValue="";
              return;
              }
          if(window.XMLHttpRequest)
              {
              xmlhttp=new  XMLHttpRequest();
              }
          else
              {
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
          xmlhttp.onreadystatechange=function()
          {
              if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
                  {
                  var y=xmlhttp.responseText;
  
  
                  var x=document.getElementById("linedepartment");
  
                  var option=document.createElement("option");
                  option.text=y;
                  try
                    {
                    // for IE earlier than version 8
                    x.add(option,x.options[null]);
                    }
                  catch (e)
                    {
                    x.add(option,null);
                    }
  
                      }
  
  
  
          };
          xmlhttp.open("GET","linedept.action?q="+v,true );
          xmlhttp.send();
  
      }
  </code></pre>
  
  <p>action class</p>
  
  <pre><code>package action;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import org.apache.struts2.ServletActionContext;
  import org.hibernate.Query;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  
  import connection.AdminLevelDetails;
  
  public class DropDownData {
      private List&lt;AdminLevelDetails&gt; allLocations;
      private List&lt;AdminLevelDetails&gt; allDepts;
      private String location;
      private List&lt;String&gt; allDetlocation;
  
  
  
  
  
  
      public String getLocation() {
          return location;
      }
  
  
      public void setLocation(String location) {
          this.location = location;
      }
  
  
      public List&lt;AdminLevelDetails&gt; getAllDepts() {
          return allDepts;
      }
  
  
      public void setAllDepts(List&lt;AdminLevelDetails&gt; allDepts) {
          this.allDepts = allDepts;
      }
  
  
      public List&lt;AdminLevelDetails&gt; getAllLocations() {
          return allLocations;
      }
  
  
      public void setAllLocations(List&lt;AdminLevelDetails&gt; allLocations) {
          this.allLocations = allLocations;
      }
  
  
  
      public String execute()
      {
          try {
              SessionFactory sf=(SessionFactory)ServletActionContext.getServletContext().getAttribute("sfkey");
              Session s=sf.openSession();
              Query q=s.createQuery("from AdminLevelDetails where int_PIdId=0");
              allLocations=(ArrayList&lt;AdminLevelDetails&gt;)q.list();
  
  
  
              /*Query q1=s.createQuery("from AdminLevelDetails where int_PIdId=1 and int_LevelId=2");
              allDepts=(ArrayList&lt;AdminLevelDetails&gt;)q1.list();*/
  
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          return "success";
      }
  
  
  public String lineDepartmentDetail()
  {
      try {
          SessionFactory sf=(SessionFactory)ServletActionContext.getServletContext().getAttribute("sfkey");
          Session s=sf.openSession(); 
          Query q1=s.createQuery("from AdminLevelDetails where int_PIdId=1 and int_LevelId=2");
          allDetlocation=(ArrayList&lt;String&gt;)q1.list();
          Iterator itr=allDetlocation.iterator();
           while(itr.hasNext())
              {
                  System.out.println(itr.next());
              }
          HttpServletResponse response=ServletActionContext.getResponse();
          response.getWriter().println(allDetlocation);
          //allDepts=(ArrayList&lt;AdminLevelDetails&gt;)q1.list();
  
          //response.getWriter().println(name);
  
  
      /*  HttpServletRequest request=ServletActionContext.getRequest();
          HttpSession hs=request.getSession();
          hs.setAttribute("username", name);
          hs.setAttribute("userid", uid);*/
  
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  
  
  
  }
  
  
  
  
  
  
  
  }
  </code></pre>
  
  <p>first i have index.jsp  in my first drop down in onchange event i call  ajax in javascript file i want to set the arraylist value in second dropdown but in bellow code only object are appeared in dropdown</p>