Results 1 to 4 of 4
- 07-04-2010, 05:46 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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:
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'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]
Thanks.
- 07-04-2010, 05:49 PM #2
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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.
- 07-04-2010, 05:51 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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>
- 04-20-2011, 03:14 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 32
- Rep Power
- 0
populate select list problem
Hi Jeremy,
Since u said :
andString book = request.getParameter("dropDown"); System.out.println(book); is printing 'null'
I recommend you try changing a bit of code in your ajaxRequest.js. Replace the lines: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.
with this piece of code:Java Code:xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("POST",url,true); xmlHttp.send("dropDown=" + selectedValue); //Name/value pair
Try this and report back. Hope it'll work!!..Java Code:url=url+"?dropDown="+selectedValue; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("GET",url,true); xmlHttp.send(null);
Good luck.
Similar Threads
-
Ripping apart a array list to populate a vector
By Adrien in forum AWT / SwingReplies: 0Last Post: 03-07-2010, 09:55 PM -
Multi-Select List Box
By balleda in forum Advanced JavaReplies: 0Last Post: 03-03-2010, 07:10 AM -
Error using ajax call in onchange event of select box.... struts 1.x?
By prabhurangan in forum Web FrameworksReplies: 0Last Post: 07-07-2008, 07:10 AM -
Displaying a Java List in Html:select tag
By ramitmehra123 in forum New To JavaReplies: 0Last Post: 02-07-2008, 05:48 AM -
how to populate html:select with values from database
By sathya_k_83 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 11-07-2007, 10:53 AM


LinkBack URL
About LinkBacks

Bookmarks