By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

ajax, JSP, object expected, linked drop down list

nmm32
P: 9
I am trying to use Ajax with JSP to populate a 2nd drop down list based on the chosen option of the first one. I am getting the error "Object Expected" on the line 13-14 of the HTML:

Expand|Select|Wrap|Line Numbers
  1.  <script language="javascript">
  2.  
  3.  // Global Variable for XmlHttp Request Object   
  4.       var xmlhttp;
  5.       var yearDDL;
  6.  
  7. function handleOnChangepType(ddl)
  8. {
  9.   //index y texto del item seleccionado
  10.   var ddlIndex = ddl.selectedIndex;
  11.   var chosenType = ddl[ddlIndex].text;
  12.   var frmSelect = document.forms["frmSelect"];
  13.   var frmSelectElem = frmSelect.elements;   
  14.   yearDDL  = frmSelectElem["year"];  
  15.  
  16.   if ( chosenType != "Select Type")
  17.   { 
  18.               xmlhttp = null 
  19.             // code for initializing XmlHttpRequest Object On Browsers like  Mozilla, etc. 
  20.             if (window.XMLHttpRequest){  
  21.                  xmlhttp = new XMLHttpRequest()                   
  22.             } 
  23.             // code for initializing XmlHttpRequest Object On Browsers like IE
  24.  
  25.            else if (window.ActiveXObject) {  
  26.                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")           
  27.            }
  28.  
  29.            if (xmlhttp != null){
  30.                 // Setting the Servlet url to get XmlData
  31.                url = "searchAvailableYears.jsp?pType=" + chosenType;               
  32.                // Course of Action That Should be Made if their is a change in XmlHttpRequest Object ReadyState NOTE : it is 4 when it has got request from CGI
  33.                xmlhttp.onreadystatechange = handleHttpResponse;
  34.  
  35.                // Open the Request by passing Type of Request & CGI URL 
  36.                xmlhttp.open("GET",url,true);
  37.  
  38.                // Sending URL Encoded Data
  39.                xmlhttp.send(null);               
  40.            }
  41.            else{  
  42.              // Only Broswers like IE 5.0,Mozilla & all other browser which support XML data Supports AJAX Technology
  43.              // In the Below case it looks as if the browser is not compatiable
  44.               alert("Your browser does not support XMLHTTP.")              
  45.            }  //else        
  46.  
  47.            }  //if chosen
  48.  
  49.     }  //fucntion
  50.  
  51.     //----------------------------
  52.  
  53.       /* Used for verifing right ReadyState & Status of XmlHttpRequest Object returns true if it is verified */
  54.       function verifyReadyState(obj){
  55.  
  56.          // As Said above if XmlHttp.ReadyState == 4 then the Page Has got Response from WebServer
  57.           if(obj.readyState == 4){
  58.  
  59.            // Similarly if XmlHttp.status == 200 it means that we have got a Valid response from the WebServer
  60.             if(obj.status == 200){                
  61.  
  62.                 return true
  63.              }
  64.              else{
  65.  
  66.                 alert("Problem retrieving XML data")
  67.              }             
  68.           }          
  69.       }
  70. //-----------------------------------
  71. function handleHttpResponse() 
  72. {
  73.    if(verifyReadyState(xmlhttp) == true)
  74.    {
  75.  
  76.    // Building a DOM parser from Response Object
  77.    var x = xmlhttp.responseXML.getElementsByTagName("option");
  78.  
  79.    // Checking for the Root Node Tag
  80.              // var x = response.split("|")
  81.             //  var tex
  82.              // var optn
  83.  
  84.    yearDDL.length = 1;
  85.    yearDDL.length = x.length;
  86.    for (o=1; o < x.length; o++)
  87.    {
  88.      yearDDL[o].text = x[o];
  89.    }
  90.   else
  91.   {
  92.    alert("Cannot handle the Ajax call.");
  93.   }
  94. } //if
  95. }//function
  96. </script>
-------------------------------

HTML part:

[HTML]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="ivory">
<form name="frmSelect">
<p><strong><u>Test</u></strong></p>
<p><hr color="orange"></p>
<table border="0">
<tr><td><strong>Type</strong></td>
<td>
<select name="pType" onchange="handleOnChangepType(this);">

<option>Select Type</option>
<%
String allDir[] = {"A", "B", "C"}; //dirs con los que se trabajará

for(int i=0; i<3; i++)
{
File dir = new File("C:/apache-tomcat-6.0.10/webapps/ROOT/p1/db/" + allDir[i] ); //(fenomenos A, B, C)


if (dir.exists() && dir.isDirectory())
{
%>
<option><%=allDir[i]%></option>
<%

} //if
} //for
%>

</select >
</td></tr>

<td><strong>Year</strong></td>
<td>


<select name="year">
<option></option>
</select>

</td>
</tr>
<tr><td><strong>Day</strong></td>
<td>


<select name="day">
<option></option>
</select>

</td></tr>
<tr><td><strong>Times available<br>for the selected day</strong></td>
<td>
<select name="range1">
<option></option>
</select>
</td>

</tr>
</td></tr>
</table>

</body>

</html>[/HTML]---------------------------------------------------------

The JSP code:
Expand|Select|Wrap|Line Numbers
  1. <%@ page contentType="text/html" %>
  2. <%@page import="java.io.*,
  3.                 javax.servlet.*,
  4.                 javax.servlet.http.*,
  5.                 java.lang.*,
  6.                 java.util.*
  7.                 "
  8. %>
  9.  
  10.  
  11. <%
  12.  
  13. String pType = request.getParameter("pType");
  14. response.setContentType("text/xml");    
  15. response.setHeader("Cache-Control", "no-cache");
  16. if (pType == "A" || pType == "B" || pType == "C")
  17.  
  18. {
  19.     File dir = new File("C:/apache-tomcat-6.0.10/webapps/ROOT/p1/db/" + pType); 
  20.     String[] allFilesInDir = dir.list();
  21.     ArrayList years = new ArrayList();
  22.     String option;
  23.  
  24.     for(int i=0; i < allFilesInDir.length; i++)
  25.     {
  26.      if (allFilesInDir[i].substring(0,0) == "d" || allFilesInDir[i].substring(0,0) == "D")
  27.      {
  28.        years.add(allFilesInDir[i].substring(1,4));       
  29.        option = "<option>" + years.get(i) + "</option>";
  30.       response.getWriter().write(option);     
  31.  
  32.      }
  33.  
  34.     }
  35.   } 
  36.   else
  37.   {
  38.    response.getWriter().write("<option>THIS IS A TEST</option>");
  39.    }
  40.  
  41. %>
Any suggestions will be very much appreciated!

Thank you!
Jun 14 '07 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
Is this in all browsers? What do you see in the error console?
Jun 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.