473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

ajax, JSP, object expected, linked drop down list

nmm32
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
1 8356
acoder
16,027 Expert Mod 8TB
Is this in all browsers? What do you see in the error console?
Jun 15 '07 #2

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

Similar topics

31
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked...
0
by: arunprabu | last post by:
Hi, I have a problecm with the AJAX request in my webpage. I have some filters on top of the page. I have a submit button and an empty div below the filters. Some of the filters have ajax...
1
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
1
by: AndiSmith | last post by:
Hi guys, I wondered if anyone could help me with this problem, or even shed some light on the direction I need to take to resolve it? I'm using .NET 2.0 (C# flavor) to build a large user-based...
1
by: Boris Twila | last post by:
I just want to have 1 drop down list fill the other drop down list without a round trip. Is there some really simple ajax thing i can copy and use, or is it a big deal do i habe to install an...
6
by: Rob Meade | last post by:
Hi all, Looking for a bit of help if possible. For about 2 weeks now I've been investigating the best way to populate related drop down menus, and have their values pre-populated again if the...
0
by: jrnail23 | last post by:
I have a user control which contains an UpdatePanel, which contains a MultiView inside, with a GridView in one of the views. In my GridView, I have a ButtonField which is supposed to trigger a...
2
by: Johnson | last post by:
While I have done a substantial amount of ASP.NET programming, I have only dabbled with AJAX (update panels and a 3rd party JSON setup - jayrock - that directly updates the DOM). In any case, I'm...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.