Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Ajaxx question -  'null' is null or not an object.

Posted on 2009-12-31
20
Medium Priority
?
659 Views
Last Modified: 2013-11-24
working with ajax, Can somebody tell me why or how to fix an error code found on line 97 of my js stating
       
                               'null' is null or not an object.  

found in the code section below is my js, html, java (respectively). Brief description:  onchange of first dropdown second dropdown is populated

line 97 is marked and code is as follows:

function PopulateBusinessList(BusinessNode)
{
var t = (xmlhttp.status)
        alert("(4) - inside PopulateBusinessList:::" +t)
      //alert("I am in Populate Country List!!!!!!!!!!!!!!!!!!!!!!")
    var BusinessList = document.getElementById("DD_ORG_BUS_KEY");
   
      // clear the DD_ORG_BUS_KEY list
      for (var count = BusinessList.options.length-1; count >-1;count--)
      {
      alert(count)
            BusinessList.options[count] = null;
      }
      
            
      var BusinessNode = BusinessNode.getElementsByTagName('Business');
//--Line# 97 --var BusinessNode = BusinessNode.getElementsByTagName('BusinessId');
      //alert("BusinessNode ::::"+BusinessNode);
      var idValue;
      var textValue;
      var optionItem;
      // populate the dropdown list with data from the xml doc
      for (var count = 0; count < BusinessNode.length; count++)
var xmlhttp

function showBusinessName(str)
{
	alert("(1) - I am in showBusinessName String is :::"+str);
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }
	var url="T/Drill?DD_ORG_CO_KEY="+str;
	alert("(2) - URL IS :::"+url);
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function showSubBusinessName(str)
{
	alert("(x) - I am in showSubBusinessName  String is :::"+str);
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }
	var url="T/Drill?DD_ORG_BUS_KEY="+str;
	//alert("URL IS :::"+url);
	xmlhttp.onreadystatechange=stateChanged1;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function stateChanged1()
{
alert("need to reach");
alert("need to reach STATUS :::::"+xmlhttp.status);
//alert("IN stateChanged:1111111111111111111111111111:: which is :::"+xmlhttp.readyState);
if (xmlhttp.readyState==4)
  {
	  //alert("xmlhttp.readyStat :::"+xmlhttp.responseText);
  	  var xmlTex1t=xmlhttp.responseText;
  	  var xmlDoc=xmlhttp.responseXML;
	  PopulateSubBusinessList(xmlhttp.responseXML.documentElement);

	  var t = (xmlhttp.status)
	  alert(t)
  }
}



function stateChanged()
{


if (xmlhttp.readyState==4)
  {
  alert("(3) - insde: stateChanged  :::  "+xmlhttp.readyState)
	  //alert("xmlhttp.readyStat :::"+xmlhttp.responseText);
  	  var xmlTex1t=xmlhttp.responseText;
  	  var xmlDoc=xmlhttp.responseXML;
	  PopulateBusinessList(xmlhttp.responseXML.documentElement);
  }
}
							out.print("<Business>");
							out.print("<BusinessName id="+BusinessId+">"+BusinessId+"</BusinessName>");
							out.print("</Business>");
alert("a")
//populate the contents of the Business dropdown list
function PopulateBusinessList(BusinessNode)
{
var t = (xmlhttp.status)
	  alert("(4) - inside PopulateBusinessList:::" +t)
	//alert("I am in Populate Country List!!!!!!!!!!!!!!!!!!!!!!")
    var BusinessList = document.getElementById("DD_ORG_BUS_KEY");
    
	// clear the DD_ORG_BUS_KEY list 
	for (var count = BusinessList.options.length-1; count >-1;count--)
	{
	alert(count)
		BusinessList.options[count] = null;
	}
	
		
	var BusinessNode = BusinessNode.getElementsByTagName('Business');
	//var BusinessNode = BusinessNode.getElementsByTagName('BusinessId');
	//alert("BusinessNode ::::"+BusinessNode);
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < BusinessNode.length; count++)
	{
		textValue = GetInnerText(BusinessNode[count]);
		//alert("In for loop for getting the Business Node :::"+ textValue);
		idValue = BusinessNode[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		BusinessList.options[BusinessList.length] = optionItem;
	}
}

function PopulateSubBusinessList(SubBusinessNode)
{
	//alert("I am in PopulateSubBusinessList  List!!!!!!!!!!!!!!!!!!!!!!")
	var SubBusinessList = document.getElementById("DD_ORG_BUS_SUB_KEY");
	// clear the DD_ORG_BUS_KEY list 
	for (var count = SubBusinessList.options.length-1; count >-1; count--) {
		SubBusinessList.options[count] = null;
	}
	
	//alert("After Clearing the SubBusinessList  List!!!!!!!!!!!!!!!!!!!!!!")
	
	var SubBusinessNode = SubBusinessNode.getElementsByTagName('SubBusinessName');
	//alert(" SubBusinessNode  "+SubBusinessNode);
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < SubBusinessNode.length; count++)
	{
		textValue = GetInnerText(SubBusinessNode[count]);
		//alert("In for loop for getting the Business Node :::"+ textValue);
		idValue = SubBusinessNode[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		SubBusinessList.options[SubBusinessList.length] = optionItem;
	}
}



function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}




<html> 
<head> 
<script src="OrgDrill.js"></script> 
<title>Hello World</title> 
</head> 
<body> 
 
<form method="post"  action="" name="f1"> 

<tr>
     <td class="faxtext" align="right">Company Name: 
		  <select name="DD_ORG_CO_KEY" id="DD_ORG_CO_KEY" style = 'visibility:visible;' onchange="showBusinessName(this.value)">
          <option value="Please Select">Please Select </option>
		  <option value="BAXTER">Baxter</option>
		  <option value="CARDINAL">Cardinal</option>
        </select></td></tr>

 <tr>



<p>

<tr>
<td class="faxtext" align="right">Business Name:
 <select name="DD_ORG_BUS_KEY" id="DD_ORG_BUS_KEY" style = 'visibility:visible;' onchange="showSubBusinessName(this.value)">
         <!--  <option value="Please Select">Please Select</option> -->
 </select>
</td>
</tr>
<hr>
<tr>
<td class="faxtext" align="right">Sub Business Name:
 <select name="DD_ORG_BUS_SUB_KEY" id="DD_ORG_BUS_SUB_KEY" style = 'visibility:visible;'>
    </select>
</td>
</tr>



</p>
</form> 
 
<b><span id="companyname"></span></b><br /><b><span id="companyname"></span></b><br /><b><span id="companyname"></span></b><br /> 
<span id="contactname"></span><br /> 
<span id="address"></span> 
<span id="city"></span><br/> 
<span id="country"></span> 
 
 
 
</body> 
</html> 




package com.testajax;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public
    class OrgDrillServlet extends HttpServlet {
	
//		public String getServletInfo() {
//	  return "Create a page that says <i>Hello World</i> and send it back";
//	 }
		public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException  {
		  // System.out.println("g");
		   String key = req.getParameter("DD_ORG_CO_KEY");
		    String businessKey = req.getParameter("DD_ORG_BUS_KEY");
		    System.out.println("DD_ORG_CO_KEY ID IS :::"+key);
		    System.out.println("DD_ORG_BUS_KEY ID IS :::"+businessKey);
		    Statement stmt= null;
		    DBConnection DBConn =null;
		    try {
		    	
		    //Try #1
		    	DBConn  = DBConnection.getInstance();
		    		stmt = DBConn.getStatement();
		    //End of Try#1
		    }catch (SQLException sqle){
		    	
		    } catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (key != null) {
		        res.setContentType("text/xml");
		        res.setHeader("Cache-Control", "no-cache");
		    // System.out.println("g");
		        // code needed between if and eof
		        try {
					
		        	PrintWriter out = res.getWriter();					 
					//res.setContentType("text/xml");					 
					out.print("<?xml version='1.0'?>");
					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
				/*String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB WHERE EXISTS "+
					   "(SELECT * FROM ORG_XREF_TAB WHERE ORG_TAB.ORG_KEY = ORG_XREF_TAB.ORG_BUS_KEY "+
					   " AND ORG_XREF_TAB.ORG_CO_KEY="+key+") ORDER BY ORG_NAME ASC)";
					   */
					
					String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
					
					ResultSet rs=stmt.executeQuery(lsQuery);
					
					out.print("<Business>");
					String BusinessId = "";
					String BusinessName ="";
				
					while ( rs!=null && rs.next()) {
						BusinessId 		= rs.getString("ORG_KEY");
						BusinessName 	= rs.getString("ORG_NAME");
						out.print("<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>");
		            }// End of while Loop
					out.print("</Business>");


				
				
				} catch (Exception e) {
					System.out.println(e);
				}
				/*catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}*/
//				 code needed between eof and if
					
			}//end of if  
		    
//		  System.out.println("g");
		}//end of public void doGet

    }//end of HttpServlet

Open in new window

0
Comment
Question by:gazdzid
  • 11
  • 5
  • 2
  • +1
20 Comments
 
LVL 3

Expert Comment

by:Nithyanandhan_T
ID: 26155631
seems there is no tag called "BusinessId" ... this can happen when your BusinessNode is not having any child node..
look into your actual xml ..


0
 
LVL 3

Author Comment

by:gazdzid
ID: 26155696
here is my xml specific section

<servlet>
  <display-name>OrgDrillServlet</display-name>
  <servlet-name>OrgDrillServlet</servlet-name>
  <servlet-class>com.testajax.OrgDrillServlet</servlet-class>
 </servlet>

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <servlet>
  <description>This is a n CountryResponseServlet</description>
  <display-name>CountryResponseServlet</display-name>
  <servlet-name>CountryResponseServlet</servlet-name>
  <servlet-class>com.testajax.CountryResponseServlet</servlet-class>
 </servlet>
 
 <servlet>
  <display-name>CountryServlet</display-name>
  <servlet-name>CountryServlet</servlet-name>
  <servlet-class>com.testajax.CountryServlet</servlet-class>
 </servlet>

<servlet>
  <display-name>CountryServlet3</display-name>
  <servlet-name>CountryServlet3</servlet-name>
  <servlet-class>com.testajax.CountryServlet3</servlet-class>
 </servlet>
 
<servlet>
  <display-name>CountryServlet4</display-name>
  <servlet-name>CountryServlet4</servlet-name>
  <servlet-class>com.testajax.CountryServlet4</servlet-class>
 </servlet>

<servlet>
  <display-name>OrgDrillServlet</display-name>
  <servlet-name>OrgDrillServlet</servlet-name>
  <servlet-class>com.testajax.OrgDrillServlet</servlet-class>
 </servlet>

 <servlet-mapping>
    <servlet-name>OrgDrillServlet</servlet-name>
    <url-pattern>/T/Drill</url-pattern>
 </servlet-mapping>

 <servlet-mapping>
    <servlet-name>CountryServlet3</servlet-name>
    <url-pattern>/T/C</url-pattern>
 </servlet-mapping>

<servlet-mapping>
    <servlet-name>CountryServlet4</servlet-name>
    <url-pattern>/T/4</url-pattern>
 </servlet-mapping>

 <servlet-mapping>
    <servlet-name>CountryServlet</servlet-name>
    <url-pattern>/TestAjax/CountryServ</url-pattern>
 </servlet-mapping>


 
  <servlet-mapping>
    <servlet-name>CountryResponseServlet</servlet-name>
    <url-pattern>/testajax/CS</url-pattern>
 </servlet-mapping>
 
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>

Open in new window

0
 
LVL 3

Expert Comment

by:Nithyanandhan_T
ID: 26156116
I'm asking for the xml value in "BusinessNode" .. I'm not sure about the xmp which you have posted..
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26156969
just a line before 97 (ie 96 ) is
var BusinessNode = BusinessNode.getElementsByTagName('Business');

where is the BusinessNode getting initialized to a value, so that you can invoke a method on it
BusinessNode.getElementsByTagName('Business');
0
 
LVL 22

Expert Comment

by:kadaba
ID: 26157182
var xmlTex1t=xmlhttp.responseText;

so you are seeing the XML as string when you alert this?

>>
function GetInnerText (node){       return (node.textContent || node.innerText || node.text) ;}

return node.childNodes[0].nodeValue
0
 
LVL 3

Author Comment

by:gazdzid
ID: 26158155
Happy New Yearsday to All!!!!!!!!!!!!!!!

Nithyanandhan_T: You Stated
I'm asking for the xml value in "BusinessNode" .. I'm not sure about the xmp which you have posted..

I appologize, I am not quite sure of what you are asking.  Please note all of the code that I have has been posted.  I am hoping that what you are asking is actually the solution.  If so can you tell me how to fix.

gurvinder372: You stated

just a line before 97 (ie 96 ) is
var BusinessNode = BusinessNode.getElementsByTagName('Business');
where is the BusinessNode getting initialized to a value, so that you can invoke a method on it
BusinessNode.getElementsByTagName('Business');

Your references to line numbers is correct.  I do not know the answer to your question.  Please note I am new to AJAX but I can tell you that the server code is in the original post.  If I am missing the intialization step can you tell me how to fix.  Thanks!!.

kadaba: you asked
var xmlTex1t=xmlhttp.responseText;
so you are seeing the XML as string when you alert this?
>>
function GetInnerText (node)
{  return (node.textContent || node.innerText || node.text) ;}
return node.childNodes[0].nodeValue

-- I added an alert just before line 97 as follows --Not sure if this is what you wanted, if it is not what you wanted, please tell how to change.  Thanks!!
var xmlTex1t=xmlhttp.responseText;
      alert("(4.1) - "+xmlTex1t)
the alert displayed "(4.1) - <?xml version='1.0'?>"
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26161267
If your XML response contains the 'Business' node after the root node, then you need to try the following code

Also, it looks like that you are not getting the proper xml response from the AJAX call.
<<the alert displayed "(4.1) - <?xml version='1.0'?>">>
Check the server-side code if it is sending the proper response



<Script>

function createDocObject(XMLtext)
{
    // code for IE
    if (window.ActiveXObject){
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(XMLtext);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
        var doc;
        var parser=new DOMParser();
        doc = parser.parseFromString(XMLtext,"text/xml");
    }
    return doc;
}

var XMLtext = xmlhttp.responseText;
var doc= createDocObject(XMLtext);

var root =doc.getElementsByTagName("rootNode");
var businessNode =root[0].getElementsByTagName("Business");

</Script>

Open in new window

0
 
LVL 22

Expert Comment

by:kadaba
ID: 26161634
Greetings gazdzid, Have a great year ahead, Thanks for your wishes :)
Alright coming to the question

Have the try block in the mentioned manner as in point 1:

So when you alert
var xmlTex1t=xmlhttp.responseText;
alert("(4.1) - "+xmlTex1t);
you must be getting an xml in this format
<?xml version='1.0'?>
<Business>
    <BusinessName id='1'>Investment Banking</BusinessName>
    <BusinessName id='2'>Mortgage</BusinessName>
    <BusinessName id='3'>Small Scale Industries</BusinessName>
</Business>

In the function to populate drop down list you will be passing the XML document as the parameter
see point 2 for comments.

Go through what I have given here. I hope you will get an insight of how it can be done.
Let know for anything.

point 1:
--------
try{
	PrintWriter out = res.getWriter();
	String resp = "";					
	resp = "<?xml version='1.0'?>";
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
	String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
	ResultSet rs=stmt.executeQuery(lsQuery);
	resp = resp + "<Business>"; // instead of string concatenation this way, you could use a string builder 
	// for better performance and good practice
	String BusinessId = "";
	String BusinessName ="";
	while (rs.next()){
		BusinessId 	= rs.getString("ORG_KEY");
		BusinessName= rs.getString("ORG_NAME");
		resp = resp + "<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>";
	}// End of while Loop
	resp = resp + "</Business>";
	out.print(resp);
}

Point 2:
---------
//populate the contents of the Business dropdown list
function PopulateBusinessList(BusinessNode)
{
	var BusinessList = document.getElementById("DD_ORG_BUS_KEY");
    
	// clear the DD_ORG_BUS_KEY list 
	for (var count = BusinessList.options.length-1; count >-1;count--)
	{
		BusinessList.options[count] = null;
	}
	
	/*
	When you say var BusinessNode = BusinessNode.getElementsByTagName('Business');
	you will be getting the root element only , instead you should look out for 
	BusinessName as this is the node that will repeat and will contain the data
	*/
	//var BusinessNode = BusinessNode.getElementsByTagName('Business'); -- OLD
	var BusinessNode = BusinessNode.getElementsByTagName('BusinessName');
	var idValue;
	var textValue; 
	var optionItem;

	// populate the dropdown list with data from the xml doc
	/*
	From the example taken here are three BusinessName elements
	*/
	for (var count = 0; count < BusinessNode.length; count++)
	{
		/*
		<BusinessName id='1'>Investment Banking</BusinessName> -- BusinessNode[0]
		GetInnerText -- should yield you : Investment Banking
		BusinessNode[count].getAttribute("id") -- should yield you 1
		
		<BusinessName id='2'>Mortgage</BusinessName> -- BusinessNode[1]
		GetInnerText -- should yield you : Mortgage
		BusinessNode[count].getAttribute("id") -- should yield you 2	
		
		<BusinessName id='3'>Small Scale Industries</BusinessName> -- BusinessNode[2]
		GetInnerText -- should yield you : Small Scale Industries
		BusinessNode[count].getAttribute("id") -- should yield you 3
		*/
		textValue = GetInnerText(BusinessNode[count]);
		
		idValue = BusinessNode[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		BusinessList.options[BusinessList.length] = optionItem;
	}
}

function GetInnerText(node)
{
	return node.childNodes[0].nodeValue
}

Open in new window

0
 
LVL 3

Author Comment

by:gazdzid
ID: 26161640
Hello gurvinder372;
  You stated
If your XML response contains the 'Business' node after the root node, then you need to try the following code  Also, it looks like that you are not getting the proper xml response from the AJAX call.
<<the alert displayed "(4.1) - <?xml version='1.0'?>">>Check the server-side code if it is sending the proper response

I did not see evidence of "return doc"; so I added some alerts to make sure the function was called.  Found below is my attempt at your request.  Please let me know if something needs to be changed.

1.past function (above rest of functions)
2. Placed call to function just below alert 4.1
3 placed alert just above "return doc;"   alert(doc).value; which popsup with "object"
*. pasted js code below

var xmlhttp

function createDocObject(XMLtext)
{
//alert("hi")
    // code for IE
    if (window.ActiveXObject){
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(XMLtext);
        alert(doc).value;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
        var doc;
        var parser=new DOMParser();
        doc = parser.parseFromString(XMLtext,"text/xml");
        
    }
     alert(doc).value;
    return doc;
}

var XMLtext = xmlhttp.responseText;
var doc= createDocObject(XMLtext);

var root =doc.getElementsByTagName("rootNode");
var businessNode =root[0].getElementsByTagName("Business");




function showBusinessName(str)
{
	alert("(1) - I am in showBusinessName String is :::"+str);
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }
	var url="T/Drill?DD_ORG_CO_KEY="+str;
	alert("(2) - URL IS :::"+url);
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function showSubBusinessName(str)
{
	alert("(x) - I am in showSubBusinessName  String is :::"+str);
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }
	var url="T/Drill?DD_ORG_BUS_KEY="+str;
	//alert("URL IS :::"+url);
	xmlhttp.onreadystatechange=stateChanged1;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function stateChanged1()
{
alert("need to reach");
alert("need to reach STATUS :::::"+xmlhttp.status);
//alert("IN stateChanged:1111111111111111111111111111:: which is :::"+xmlhttp.readyState);
if (xmlhttp.readyState==4)
  {
	  //alert("xmlhttp.readyStat :::"+xmlhttp.responseText);
  	  var xmlTex1t=xmlhttp.responseText;
  	  var xmlDoc=xmlhttp.responseXML;
	  PopulateSubBusinessList(xmlhttp.responseXML.documentElement);

	  var t = (xmlhttp.status)
	  alert(t)
  }
}



function stateChanged()
{


if (xmlhttp.readyState==4)
  {
  alert("(3) - insde: stateChanged  :::  "+xmlhttp.readyState)
	  //alert("xmlhttp.readyStat :::"+xmlhttp.responseText);
  	  var xmlTex1t=xmlhttp.responseText;
  	  var xmlDoc=xmlhttp.responseXML;
	  PopulateBusinessList(xmlhttp.responseXML.documentElement);
  }
}
							out.print("<Business>");
							out.print("<BusinessName id="+BusinessId+">"+BusinessId+"</BusinessName>");
							out.print("</Business>");
alert("a")
//populate the contents of the Business dropdown list
function PopulateBusinessList(BusinessNode)
{
var t = (xmlhttp.status)
	  alert("(4) - inside PopulateBusinessList:::" +t)
	//alert("I am in Populate Country List!!!!!!!!!!!!!!!!!!!!!!")
    var BusinessList = document.getElementById("DD_ORG_BUS_KEY");
    
	// clear the DD_ORG_BUS_KEY list 
	for (var count = BusinessList.options.length-1; count >-1;count--)
	{
	alert(count)
		BusinessList.options[count] = null;
	}
	var xmlTex1t=xmlhttp.responseText;
	alert("(4.1) - "+xmlTex1t)
	createDocObject(xmlTex1t);
		
	var BusinessNode = BusinessNode.getElementsByTagName('Business');
	//var BusinessNode = BusinessNode.getElementsByTagName('BusinessId');
	//alert("BusinessNode ::::"+BusinessNode);
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < BusinessNode.length; count++)
	{
		textValue = GetInnerText(BusinessNode[count]);
		//alert("In for loop for getting the Business Node :::"+ textValue);
		idValue = BusinessNode[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		BusinessList.options[BusinessList.length] = optionItem;
	}
}

function PopulateSubBusinessList(SubBusinessNode)
{
	//alert("I am in PopulateSubBusinessList  List!!!!!!!!!!!!!!!!!!!!!!")
	var SubBusinessList = document.getElementById("DD_ORG_BUS_SUB_KEY");
	// clear the DD_ORG_BUS_KEY list 
	for (var count = SubBusinessList.options.length-1; count >-1; count--) {
		SubBusinessList.options[count] = null;
	}
	
	//alert("After Clearing the SubBusinessList  List!!!!!!!!!!!!!!!!!!!!!!")
	
	var SubBusinessNode = SubBusinessNode.getElementsByTagName('SubBusinessName');
	//alert(" SubBusinessNode  "+SubBusinessNode);
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < SubBusinessNode.length; count++)
	{
		textValue = GetInnerText(SubBusinessNode[count]);
		//alert("In for loop for getting the Business Node :::"+ textValue);
		idValue = SubBusinessNode[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		SubBusinessList.options[SubBusinessList.length] = optionItem;
	}
}



function GetInnerText (node)
{
alert("I am inside the:::: GetInnerText()")
	 //return (node.textContent || node.innerText || node.text) ;
	
}

Open in new window

0
 
LVL 3

Author Comment

by:gazdzid
ID: 26161769
Hello kadaba:

         
0
 
LVL 3

Author Comment

by:gazdzid
ID: 26161827
Hello kadaba:

         sorry some how submitted without content.  I looked through your feedback and explainations, which did help me understand how it should work and part of my error.  At this time I chose not to change the java as others are assisting (I am sure it would not matter to them if I did or did not make the change) but will consider your change oncee I get my dropdown to work:  I did follow you suggestions for the js which I did the following:

1. I did comment-out var BusinessNode = BusinessNode.getElementsByTagName('Business');
2. I did add var BusinessNode = BusinessNode.getElementsByTagName('BusinessName');
3. I did add return node.childNodes[0].nodeValue to function GetInnerText (node)
*.Please let me know if I missed anything:

After testing I am still receiving a null error as originally described null, which appears on the commented line after  BusinessNode = BusinessNode.getElementsByTagName('BusinessName');.  
0
 
LVL 22

Expert Comment

by:kadaba
ID: 26161856
The problem with the earlier code might be that when you say out.print("<?xml version='1.0'?>") , the response will be back to the view layer. I think the problem is with the java code returning the XML,(Sure, Do test this and just make sure the xml is coming to the view layer, i.e. in the format I mentioned).

When ever possible consider all changes I told and check, let me know.

thanks.
0
 
LVL 3

Author Comment

by:gazdzid
ID: 26161940
Hello Kadaba,

  I did manke the changes as requested with the original error displaying.  my popup

alert("(4.1) - "+xmlTex1t)  does popup with the following response  (4.1) -

The changes I made are as follows:
1. comment my code in the try statement
2. added your code (exactly as you have it)
3. build project
4. stop tomcat
5. start tomcact
6. test (results seem to be the same) pop-up displays "(4.1) - "
*. Please let me know if I missed anything my coe for js and java can be found in code below:


var xmlhttp

function createDocObject(XMLtext)
{
//alert("hi")
    // code for IE
    if (window.ActiveXObject){
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(XMLtext);
        alert(doc).value;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
        var doc;
        var parser=new DOMParser();
        doc = parser.parseFromString(XMLtext,"text/xml");
        
    }
     alert(doc).value;
    return doc;
}

var XMLtext = xmlhttp.responseText;
var doc= createDocObject(XMLtext);

var root =doc.getElementsByTagName("rootNode");
var businessNode =root[0].getElementsByTagName("Business");




function showBusinessName(str)
{
	alert("(1) - I am in showBusinessName String is :::"+str);
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }
	var url="T/Drill?DD_ORG_CO_KEY="+str;
	alert("(2) - URL IS :::"+url);
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function showSubBusinessName(str)
{
	alert("(x) - I am in showSubBusinessName  String is :::"+str);
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }
	var url="T/Drill?DD_ORG_BUS_KEY="+str;
	//alert("URL IS :::"+url);
	xmlhttp.onreadystatechange=stateChanged1;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function stateChanged1()
{
alert("need to reach");
alert("need to reach STATUS :::::"+xmlhttp.status);
//alert("IN stateChanged:1111111111111111111111111111:: which is :::"+xmlhttp.readyState);
if (xmlhttp.readyState==4)
  {
	  //alert("xmlhttp.readyStat :::"+xmlhttp.responseText);
  	  var xmlTex1t=xmlhttp.responseText;
  	  var xmlDoc=xmlhttp.responseXML;
	  PopulateSubBusinessList(xmlhttp.responseXML.documentElement);

	  var t = (xmlhttp.status)
	  alert(t)
  }
}



function stateChanged()
{


if (xmlhttp.readyState==4)
  {
  alert("(3) - insde: stateChanged  :::  "+xmlhttp.readyState)
	  //alert("xmlhttp.readyStat :::"+xmlhttp.responseText);
  	  var xmlTex1t=xmlhttp.responseText;
  	  var xmlDoc=xmlhttp.responseXML;
	  PopulateBusinessList(xmlhttp.responseXML.documentElement);
  }
}
							out.print("<Business>");
							out.print("<BusinessName id="+BusinessId+">"+BusinessId+"</BusinessName>");
							out.print("</Business>");
alert("a")
//populate the contents of the Business dropdown list
function PopulateBusinessList(BusinessNode)
{
var t = (xmlhttp.status)
	  alert("(4) - inside PopulateBusinessList:::" +t)
	//alert("I am in Populate Country List!!!!!!!!!!!!!!!!!!!!!!")
    var BusinessList = document.getElementById("DD_ORG_BUS_KEY");
    
	// clear the DD_ORG_BUS_KEY list 
	for (var count = BusinessList.options.length-1; count >-1;count--)
	{
	alert(count)
		BusinessList.options[count] = null;
	}
	var xmlTex1t=xmlhttp.responseText;
	alert("(4.1) - "+xmlTex1t)
	//createDocObject(xmlTex1t);
		
	//var BusinessNode = BusinessNode.getElementsByTagName('Business');
	//var BusinessNode = BusinessNode.getElementsByTagName('BusinessId');
	var BusinessNode = BusinessNode.getElementsByTagName('BusinessName');
	//alert("BusinessNode ::::"+BusinessNode);
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < BusinessNode.length; count++)
	{
		textValue = GetInnerText(BusinessNode[count]);
		//alert("In for loop for getting the Business Node :::"+ textValue);
		idValue = BusinessNode[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		BusinessList.options[BusinessList.length] = optionItem;
	}
}

function PopulateSubBusinessList(SubBusinessNode)
{
	//alert("I am in PopulateSubBusinessList  List!!!!!!!!!!!!!!!!!!!!!!")
	var SubBusinessList = document.getElementById("DD_ORG_BUS_SUB_KEY");
	// clear the DD_ORG_BUS_KEY list 
	for (var count = SubBusinessList.options.length-1; count >-1; count--) {
		SubBusinessList.options[count] = null;
	}
	
	//alert("After Clearing the SubBusinessList  List!!!!!!!!!!!!!!!!!!!!!!")
	
	var SubBusinessNode = SubBusinessNode.getElementsByTagName('SubBusinessName');
	//alert(" SubBusinessNode  "+SubBusinessNode);
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < SubBusinessNode.length; count++)
	{
		textValue = GetInnerText(SubBusinessNode[count]);
		//alert("In for loop for getting the Business Node :::"+ textValue);
		idValue = SubBusinessNode[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		SubBusinessList.options[SubBusinessList.length] = optionItem;
	}
}



function GetInnerText (node)
{
alert("I am inside the:::: GetInnerText()")
	 //return (node.textContent || node.innerText || node.text) ;
	 return node.childNodes[0].nodeValue
	 
	
}




package com.testajax;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public
    class OrgDrillServlet extends HttpServlet {
	
//		public String getServletInfo() {
//	  return "Create a page that says <i>Hello World</i> and send it back";
//	 }
		public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException  {
		  // System.out.println("g");
		   String key = req.getParameter("DD_ORG_CO_KEY");
		    String businessKey = req.getParameter("DD_ORG_BUS_KEY");
		    System.out.println("DD_ORG_CO_KEY ID IS :::"+key);
		    System.out.println("DD_ORG_BUS_KEY ID IS :::"+businessKey);
		    Statement stmt= null;
		    DBConnection DBConn =null;
		    try {
		    	
		    //Try #1
		    	DBConn  = DBConnection.getInstance();
		    		stmt = DBConn.getStatement();
		    //End of Try#1
		    }catch (SQLException sqle){
		    	
		    } catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (key != null) {
		        res.setContentType("text/xml");
		        res.setHeader("Cache-Control", "no-cache");
		    // System.out.println("g");
		        // code needed between if and eof
		        try {
		        	
		        	PrintWriter out = res.getWriter();
		        	String resp = "";					
		        	resp = "<?xml version='1.0'?>";
		        	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
		        	String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
		        	ResultSet rs=stmt.executeQuery(lsQuery);
		        	resp = resp + "<Business>"; // instead of string concatenation this way, you could use a string builder 
		        	// for better performance and good practice
		        	String BusinessId = "";
		        	String BusinessName ="";
		        	while (rs.next()){
		        		BusinessId 	= rs.getString("ORG_KEY");
		        		BusinessName= rs.getString("ORG_NAME");
		        		resp = resp + "<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>";
		        	}// End of while Loop
		        	resp = resp + "</Business>";
		        	out.print(resp);

				/*	
		        	PrintWriter out = res.getWriter();
		        	
					//res.setContentType("text/xml");					 
					out.print("<?xml version='1.0'?>");
					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();*/
				/*String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB WHERE EXISTS "+
					   "(SELECT * FROM ORG_XREF_TAB WHERE ORG_TAB.ORG_KEY = ORG_XREF_TAB.ORG_BUS_KEY "+
					   " AND ORG_XREF_TAB.ORG_CO_KEY="+key+") ORDER BY ORG_NAME ASC)";
					   */
					
				/*	String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
					
					ResultSet rs=stmt.executeQuery(lsQuery);
					
					out.print("<Business>");
					String BusinessId = "";
					String BusinessName ="";
				
					while ( rs!=null && rs.next()) {
						BusinessId 		= rs.getString("ORG_KEY");
						BusinessName 	= rs.getString("ORG_NAME");
						out.print("<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>");
		            }// End of while Loop
					out.print("</Business>");

				*/
				
				
				} catch (Exception e) {
					System.out.println(e);
				}
				/*catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}*/
//				 code needed between eof and if
					
			}//end of if  
		    
//		  System.out.println("g");
		}//end of public void doGet

    }//end of HttpServlet

Open in new window

0
 
LVL 22

Expert Comment

by:kadaba
ID: 26162179
hmm ok I will test this from my side, one last question before i do so

instead of
out.print(resp);
have
out.write(resp);
out.flush();

let me know what is the response coming when you alert


0
 
LVL 3

Author Comment

by:gazdzid
ID: 26163652
Hello Kadaba,

Thank you for your continued support and your willingness to try it on your system. I did make the requested changes.  My code can be found in the code section.  I made the following changes:

1.) comment out "//out.print(resp);"
2.) Added  out.write(resp);
                  out.flush();
3.) the popup shows "(4.1) - ".
4.) build/stop/start tomcat
*.) test results appear to be the same
*. Please let me know if additional tests need to be run, as I am kind of clueless to how to solve.
package com.testajax;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public
    class OrgDrillServlet extends HttpServlet {
	
//		public String getServletInfo() {
//	  return "Create a page that says <i>Hello World</i> and send it back";
//	 }
		public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException  {
		  // System.out.println("g");
		   String key = req.getParameter("DD_ORG_CO_KEY");
		    String businessKey = req.getParameter("DD_ORG_BUS_KEY");
		    System.out.println("DD_ORG_CO_KEY ID IS :::"+key);
		    System.out.println("DD_ORG_BUS_KEY ID IS :::"+businessKey);
		    Statement stmt= null;
		    DBConnection DBConn =null;
		    try {
		    	
		    //Try #1
		    	DBConn  = DBConnection.getInstance();
		    		stmt = DBConn.getStatement();
		    //End of Try#1
		    }catch (SQLException sqle){
		    	
		    } catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (key != null) {
		        res.setContentType("text/xml");
		        res.setHeader("Cache-Control", "no-cache");
		    // System.out.println("g");
		        // code needed between if and eof
		        try {
		        	
		        	PrintWriter out = res.getWriter();
		        	String resp = "";					
		        	resp = "<?xml version='1.0'?>";
		        	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
		        	String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
		        	ResultSet rs=stmt.executeQuery(lsQuery);
		        	resp = resp + "<Business>"; // instead of string concatenation this way, you could use a string builder 
		        	// for better performance and good practice
		        	String BusinessId = "";
		        	String BusinessName ="";
		        	while (rs.next()){
		        		BusinessId 	= rs.getString("ORG_KEY");
		        		BusinessName= rs.getString("ORG_NAME");
		        		resp = resp + "<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>";
		        	}// End of while Loop
		        	resp = resp + "</Business>";
		        	//out.print(resp);
		        	out.write(resp);
		        	out.flush();

				/*	
		        	PrintWriter out = res.getWriter();
		        	
					//res.setContentType("text/xml");					 
					out.print("<?xml version='1.0'?>");
					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();*/
				/*String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB WHERE EXISTS "+
					   "(SELECT * FROM ORG_XREF_TAB WHERE ORG_TAB.ORG_KEY = ORG_XREF_TAB.ORG_BUS_KEY "+
					   " AND ORG_XREF_TAB.ORG_CO_KEY="+key+") ORDER BY ORG_NAME ASC)";
					   */
					
				/*	String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
					
					ResultSet rs=stmt.executeQuery(lsQuery);
					
					out.print("<Business>");
					String BusinessId = "";
					String BusinessName ="";
				
					while ( rs!=null && rs.next()) {
						BusinessId 		= rs.getString("ORG_KEY");
						BusinessName 	= rs.getString("ORG_NAME");
						out.print("<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>");
		            }// End of while Loop
					out.print("</Business>");

				*/
				
				
				} catch (Exception e) {
					System.out.println(e);
				}
				/*catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}*/
//				 code needed between eof and if
					
			}//end of if  
		    
//		  System.out.println("g");
		}//end of public void doGet

    }//end of HttpServlet

Open in new window

0
 
LVL 3

Author Comment

by:gazdzid
ID: 26164010
Hello Kadaba,
I have been playing with my java code found in the try statement

1. commented your suggestions
2. uncommented the original code "(4.1) - <?xml version='1.0'?>">>

then the strange thig happened
3. commented out my code
4 uncommented your code  "(4.1) - <?xml version='1.0'?>">>

I am not sure why but alert 4.1 has changed considering that I build project after each java change /stop/start tomcat.  the one thing that I did different was, i opened a new browser window and pasted the link into the address bar, which leads me to think that this could be a cache issue.
 try {
		        	
		        	PrintWriter out = res.getWriter();
		        	String resp = "";					
		        	resp = "<?xml version='1.0'?>";
		        	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
		        	String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
		        	ResultSet rs=stmt.executeQuery(lsQuery);
		        	resp = resp + "<Business>"; // instead of string concatenation this way, you could use a string builder 
		        	// for better performance and good practice
		        	String BusinessId = "";
		        	String BusinessName ="";
		        	while (rs.next()){
		        		BusinessId 	= rs.getString("ORG_KEY");
		        		BusinessName= rs.getString("ORG_NAME");
		        		resp = resp + "<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>";
		        	}// End of while Loop
		        	resp = resp + "</Business>";
		        	//out.print(resp);
		        	out.write(resp);
		        	out.flush();
		        

				/*	
		        	PrintWriter out = res.getWriter();
		        	
					//res.setContentType("text/xml");					 
					out.print("<?xml version='1.0'?>");
					Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();*/
				/*String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB WHERE EXISTS "+
					   "(SELECT * FROM ORG_XREF_TAB WHERE ORG_TAB.ORG_KEY = ORG_XREF_TAB.ORG_BUS_KEY "+
					   " AND ORG_XREF_TAB.ORG_CO_KEY="+key+") ORDER BY ORG_NAME ASC)";
					   */
					
				/*	String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
					
					ResultSet rs=stmt.executeQuery(lsQuery);
					
					out.print("<Business>");
					String BusinessId = "";
					String BusinessName ="";
				
					while ( rs!=null && rs.next()) {
						BusinessId 		= rs.getString("ORG_KEY");
						BusinessName 	= rs.getString("ORG_NAME");
						out.print("<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>");
		            }// End of while Loop
					out.print("</Business>");

				*/
				
				
				} 

Open in new window

0
 
LVL 3

Author Comment

by:gazdzid
ID: 26164038
cancel last post I still seeing " 4.1 - "
0
 
LVL 22

Accepted Solution

by:
kadaba earned 2000 total points
ID: 26166150
Hi gazdzid,

I tested from my side and it seems to work fine.
I made similar set up and tested this.

Go through the java file and also the HTML file I have documented things so you will be able to follow.

Let me know.

kadaba



----------- The Servlet File:OrgDrillServlet.java ---------------
package com.testajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class OrgDrillServlet
 */
public class OrgDrillServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrgDrillServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String key = request.getParameter("DD_ORG_CO_KEY");
		String businessKey = request.getParameter("DD_ORG_BUS_KEY");
		
		System.out.println("DD_ORG_CO_KEY ID IS :::"+key);
		System.out.println("DD_ORG_BUS_KEY ID IS :::"+businessKey);
		
		/*
		Statement stmt= null;
		DBConnection DBConn =null;
		try{
			DBConn  = DBConnection.getInstance();
			stmt = DBConn.getStatement();
		}catch (SQLException exception){
			System.out.println("SQL Exception while connecting to DB!");    	
		} catch (InstantiationException exception) {
			System.out.println("Cannot create a DB connection!");    			
		} catch (IllegalAccessException exception) {
			System.out.println("Acess Denied to connect!");    			
		} catch (ClassNotFoundException exception) {
			System.out.println("Class not found!");    			
		}
		*/
		
		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
		
		// If the key is null we will assume that this is a call to populate sub business names
	    if (key != null) 
	    {
	    	System.out.println("Populating the business names...");	
			String resp = "";
			resp = "<?xml version='1.0'?>";
			try {

				PrintWriter out = response.getWriter();
				
				/*
				 	resp = resp + "<Business>"; 
					String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
					ResultSet rs=stmt.executeQuery(lsQuery);
				
					String BusinessId = "";
					String BusinessName ="";
			
					while (rs.next()) {
						BusinessId 		= rs.getString("ORG_KEY");
						BusinessName 	= rs.getString("ORG_NAME");
						resp = resp + "<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>";
	            	}
	            	resp = resp + "</Business>";	
	            */
				
				/*
				 * Example block of code which would be
				 * derived from the Data base as above
				 */
				resp = resp + "<Business>";
				resp = resp + "<BusinessName id='1'>Investment Banking</BusinessName>";
				resp = resp + "<BusinessName id='2'>Mortgage</BusinessName>";
				resp = resp + "<BusinessName id='3'>Small Scale Industries</BusinessName>";
				resp = resp + "</Business>";
				
				System.out.println("The XML data retrieved is:");
				System.out.println(resp);
				
				out.write(resp);
				out.flush();
			} catch (Exception exception) {
				System.out.println("Error occured when fecthing the business names:"
						+ exception);
			}
	    }
	    // request for displaying sub business names
	    else
	    {
	    	System.out.println("Populating the sub business names...");	
			String resp = "";
			resp = "<?xml version='1.0'?>";
			
			try {

				PrintWriter out = response.getWriter();
					
				/*
				 * Example block of code which would be
				 * derived from the Data base 
				 */
				resp = resp + "<Business>";
				resp = resp + "<SubBusinessName id='1'>Sub - Investment Banking</SubBusinessName>";
				resp = resp + "<SubBusinessName id='2'>Sub - Mortgage</SubBusinessName>";
				resp = resp + "<SubBusinessName id='3'>Sub - Small Scale Industries</SubBusinessName>";
				resp = resp + "</Business>";
				
				System.out.println("The XML data retrieved is:");
				System.out.println(resp);
				
				out.write(resp);
				out.flush();
			} catch (Exception exception) {
				System.out.println("Error occured when fecthing the sub business names:"
						+ exception);
			}
			
	    }
	}	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
-------------End of the Servlet file -------------------

------------ The HTML File-------------------------------
<html> 
<head> 
<title>AJAX Drill Test</title>
<script type="text/javascript">
var xmlhttp;

/**
 * Function to return the reference to the object by passing the Id of the 
 * object
 */
function $(elementId){
	return document.getElementById(elementId);
}

/**
 * Function to initialize the http xml request object.
 * The function will return true if the object is valid else will
 * return false	
 */
function initHttpReqObject()
{
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp==null)
	{
		  alert ("Your browser does not support AJAX!");
		  return false;
	}
	return true;	
}

/**
 * Function to show the business name.
 * comanpyName - the name of the comapny for which the business needs to be 
 * shown.	
 */
function showBusinessName(companyName)
{
	if(!initHttpReqObject()){
		return;	
	}  	
	var url="T/Drill?DD_ORG_CO_KEY="+companyName;
	url=url+"&sid="+Math.random();

	/*
		Testing: Remove Later
		alert("The URL is:"+url);	
	*/
	
	xmlhttp.onreadystatechange=populateBusinessNamesAjaxHandler;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function showSubBusinessName(businessName)
{
	if(!initHttpReqObject()){
		return;	
	}  	
	
	var url="T/Drill?DD_ORG_BUS_KEY="+businessName;
	url=url+"&sid="+Math.random();

	/*
		Testing: Remove Later
		alert("The URL is:"+url);	
	*/
	
	
	xmlhttp.onreadystatechange=populateSubBusinessNamesAjaxHandler;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


/**
 * Function to handle the post processing of the AJAX response
 * when the call is made to Function showBusinessName
 */
function populateBusinessNamesAjaxHandler()
{
	if (xmlhttp.readyState==4)
  	{
  	  	var xmlDoc=xmlhttp.responseXML;
	  	/*
			Testing: Remove Later
			alert("The response XML is:"+xmlhttp.responseText);	
		*/
  	  	PopulateBusinessList(xmlhttp.responseXML.documentElement);
  	}
}

/**
 * Function to handle the post processing of the AJAX response
 * when the call is made to Function showSubBusinessName
 */
function populateSubBusinessNamesAjaxHandler()
{
	if (xmlhttp.readyState==4)
  	{
	  	var xmlDoc=xmlhttp.responseXML;
	  	/*
			Testing: Remove Later
			alert("The response XML is:"+xmlhttp.responseText);	
	  	*/
	  	PopulateSubBusinessList(xmlhttp.responseXML.documentElement);
	}
}

/**
 * Function to populate the drop down with the Business Names
 */
function PopulateBusinessList(BusinessNode)
{
    var businessDropDownList = $('DD_ORG_BUS_KEY');
    // Make the drop down empty
    businessDropDownList.options.length = 0;
		
    /*
	When you say var BusinessNode = BusinessNode.getElementsByTagName('Business');
	you will be getting the root element only , instead you should look out for 
	BusinessName as this is the node that will repeat and will contain the data
	*/
	//var BusinessNode = BusinessNode.getElementsByTagName('Business'); -- OLD
	var BusinessNode = BusinessNode.getElementsByTagName('BusinessName');
	var idValue;
	var textValue; 
	var optionItem;

	/*
		Testing: Remove Later
		alert("The No of BusinessName Nodes found:"+BusinessNode.length);	
	*/
	
	// populate the dropdown list with data from the xml doc
	/*
		From the example taken here are three BusinessName elements
	*/
	for (var count = 0; count < BusinessNode.length; count++)
	{
		/*
		<BusinessName id='1'>Investment Banking</BusinessName> -- BusinessNode[0]
		GetInnerText -- should yield you : Investment Banking
		BusinessNode[count].getAttribute("id") -- should yield you 1
		
		<BusinessName id='2'>Mortgage</BusinessName> -- BusinessNode[1]
		GetInnerText -- should yield you : Mortgage
		BusinessNode[count].getAttribute("id") -- should yield you 2	
		
		<BusinessName id='3'>Small Scale Industries</BusinessName> -- BusinessNode[2]
		GetInnerText -- should yield you : Small Scale Industries
		BusinessNode[count].getAttribute("id") -- should yield you 3
		*/
		textValue = GetInnerText(BusinessNode[count]);
		idValue = BusinessNode[count].getAttribute("id");
		optionItem = new Option( textValue, idValue);
		businessDropDownList.options[businessDropDownList.options.length] = optionItem;
	}
}

function PopulateSubBusinessList(SubBusinessNode)
{
	var SubBusinessDropDownList = $("DD_ORG_BUS_SUB_KEY");
	 // Make the drop down empty
    SubBusinessDropDownList.options.length = 0;
	
	var SubBusinessNode = SubBusinessNode.getElementsByTagName('SubBusinessName');

	/*
		Testing: Remove Later
		alert("The No of SubBusinessName Nodes found:"+SubBusinessNode.length);	
	*/
	
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < SubBusinessNode.length; count++)
	{
		textValue = GetInnerText(SubBusinessNode[count]);
		idValue = SubBusinessNode[count].getAttribute("id");
		optionItem = new Option( textValue, idValue);
		SubBusinessDropDownList.options[SubBusinessDropDownList.options.length] = optionItem;
	}
}


function GetInnerText(node)
{
	return node.childNodes[0].nodeValue;
}


</script>
</head> 
<body> 
<form method="post" action="" name="myForm"> 
<table width="600px">
	<tr>
    	<td class="faxtext" align="right">Company Name: 
			<select name="DD_ORG_CO_KEY" id="DD_ORG_CO_KEY" onchange="showBusinessName(this.value)">
        		<option value="Please Select">Please Select</option>
				<option value="BAXTER">Baxter</option>
		  		<option value="CARDINAL">Cardinal</option>
        	</select>
        </td>
     </tr>
	 <tr>
	<tr>
		<td class="faxtext" align="right">Business Name:
 			<select name="DD_ORG_BUS_KEY" id="DD_ORG_BUS_KEY" onchange="showSubBusinessName(this.value)">
        	</select> 
 		</td>
	</tr>
	<tr>
		<td class="faxtext" align="right">Sub Business Name:
 			<select name="DD_ORG_BUS_SUB_KEY" id="DD_ORG_BUS_SUB_KEY">
    		</select>
		</td>
	</tr>
</table>
</form>	 
</body> 
</html> 
----------------------- End of the HTML File--------------------

Open in new window

0
 
LVL 3

Author Comment

by:gazdzid
ID: 26167129
Hello kadaba:
Thank you for your assistance and patience as I did get it to work on my side!!

I will assign points,  found below is a new question that I have opened

http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_25016348.html

PrintWriter out = response.getWriter();
				
/*
resp = resp + "<Business>"; 
String lsQuery = " (SELECT ORG_KEY , ORG_NAME  FROM ORG_TAB ORDER BY ORG_NAME ASC)";
ResultSet rs=stmt.executeQuery(lsQuery);
String BusinessId = "";
String BusinessName ="";
			
while (rs.next()) {
	BusinessId = rs.getString("ORG_KEY");			BusinessName = rs.getString("ORG_NAME");
	resp = resp + "<BusinessName id='"+BusinessId+"'>"+BusinessName+"</BusinessName>";
	            	}
	 resp = resp + "</Business>";	
	            */
				
	/*
	* Example block of code which would be
	* derived from the Data base as above
		*/		

Open in new window

0
 
LVL 3

Author Closing Comment

by:gazdzid
ID: 31671661
Thanks, I thank you for your expetise (alot of work for 500 points but appreciated)!!  found below is the link to my question of making it dynamic.

http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_25016348.html

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is basically two types of AJAX request in mootools. Request  and Request.HTML Request: Request is the basic XHR request class in MooTools. While not extremely useful on its own, it provides the basic functionality for both Request.HTM…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question