Results 1 to 4 of 4
  1. #1
    Jeremy720 is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default Populate select list with Java/Ajax

    Hi. I'm trying to populate a select list based on the selection of another select list using Ajax. The select has an onchange event that passes the selected value to the ajax script. In my java class, which retrieves the second select list's data from the database, I attempt to call request.getParameter("selectedValue"), but it is not populating the select list because request.getParameter("selectedValue") is returning null. I have stumped two people with this so far. Perhaps YOU can give me a clue :). Any advise is appreciated. Here is the code:
    XML Code:
    [CODE]<!--THE index.jsp -->
     <form name ="books" method="post" action="">
                <select name="bookDropDown" id="bookDropDown" onchange="htmlData(this.value, 'getChapters')">
               <c:forEach var="book" items="${bookList}">
                   <option value=${book.bookName}>${book.bookName}</option>
               </c:forEach>
             </select>
               
               <h2>Chapters</h2>
    
             <div id="txtResult">
              <select name="chapterList" id="chapterList">
               <option></option>
              </select>
             </div>
            </form>
    <!-- END index.jsp -->
    
    <!--AJAX ajaxRequest.js -->
    function GetXmlHttpObject(handler)
    {
       var objXMLHttp=null
       if (window.XMLHttpRequest)
       {
           objXMLHttp=new XMLHttpRequest()
       }
       else if (window.ActiveXObject)
       {
           objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
       }
       return objXMLHttp
    }
    
    function stateChanged()
    {
       if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
       {
               document.getElementById("txtResult").innerHTML = xmlHttp.responseText; //"<select><option>Hello</option></select>"; //
       }
       else {
               //alert(xmlHttp.status);
       }
    }
    
    function htmlData(selectedValue, url)
    {
       if (url.length==0)
       {
           document.getElementById("txtResult").innerHTML="";
           alert("URL equals FAIL");
           return;
       }
       xmlHttp=GetXmlHttpObject()
       if (xmlHttp==null)
       {
           alert ("Browser does not support HTTP Request");
           return;
       }
    
       xmlHttp.onreadystatechange=stateChanged;
       xmlHttp.open("POST",url,true);
       xmlHttp.send("dropDown=" + selectedValue); //Name/value pair
    }
    <!-- END AJAX -->
    
    <!-- SERVLETT getChapters.java -->
    public class GetChapters extends HttpServlet {
     ArrayList<Chapter> chapters;
     int bookID;
     DAO dao;
        @Override
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {
            HttpSession session = request.getSession();
    	session.setMaxInactiveInterval(60*60);
            dao = new DAO();
            
            String book = request.getParameter("dropDown");
            System.out.println(book);
    
            ArrayList<CookBook> bookList = (ArrayList<CookBook>)getServletContext().getAttribute("bookList");
    
            ListIterator<CookBook> itr = bookList.listIterator();
            while (itr.hasNext()){
                if(itr.next().bookName.equals(book)){
                    bookID = itr.next().bookID;
                    session.setAttribute("bookID", bookID);
                }
            }
    
            chapters = dao.getChapters(bookID); //new ArrayList<Chapter>();//
    
            session.setAttribute("chapters", chapters);
            RequestDispatcher rd = request.getRequestDispatcher("replaceText.jsp");
            rd.forward(request, response);
        }
    
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {}
    }
    <!-- END SERVLET -->[/CODE]
    I would include the database call, but it won't matter until I figure out why the String book = request.getParameter("dropDown"); System.out.println(book); is printing 'null'
    Thanks.

  2. #2
    Jeremy720 is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    I should mention that 1) I've tried hard-coding a value for the ajax send function, but still it prints 'null' 2) When hard coding a value returned by the DAO call, the select list populates correctly.
    hmm.

  3. #3
    Jeremy720 is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Oh...and here is the replaceText.jsp:

    Java Code:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@page isELIgnored="false"%>
    
    <select name="chapterDropDown" id="chapterDropDown">
         <c:forEach var="chapters" items="${chapters}">
                   <option value=${chapters.chapterName}>${chapters.chapterName}</option>
         </c:forEach>
    </select>

  4. #4
    go4soumya is offline Member
    Join Date
    Oct 2010
    Posts
    32
    Rep Power
    0

    Default populate select list problem

    Hi Jeremy,

    Since u said :

    String book = request.getParameter("dropDown"); System.out.println(book); is printing 'null'
    and

    I should mention that 1) I've tried hard-coding a value for the ajax send function, but still it prints 'null' 2) When hard coding a value returned by the DAO call, the select list populates correctly.
    I recommend you try changing a bit of code in your ajaxRequest.js. Replace the lines:

    Java Code:
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("POST",url,true);
    xmlHttp.send("dropDown=" + selectedValue); //Name/value pair
    with this piece of code:

    Java Code:
    url=url+"?dropDown="+selectedValue;  
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    Try this and report back. Hope it'll work!!..

    Good luck.

Similar Threads

  1. Ripping apart a array list to populate a vector
    By Adrien in forum AWT / Swing
    Replies: 0
    Last Post: 03-07-2010, 10:55 PM
  2. Multi-Select List Box
    By balleda in forum Advanced Java
    Replies: 0
    Last Post: 03-03-2010, 08:10 AM
  3. Replies: 0
    Last Post: 07-07-2008, 08:10 AM
  4. Displaying a Java List in Html:select tag
    By ramitmehra123 in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 06:48 AM
  5. how to populate html:select with values from database
    By sathya_k_83 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-07-2007, 11:53 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •