Copy Code code as follows:
Package Com.ajaxlab.ajax;
Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.Iterator;
Import org.jdom.Document;
Import org.jdom.Element;
Import Org.jdom.input.SAXBuilder;
Import Com.ajaxlab.ajax.ProductClass;
public class Classservice ... {
Private Document Dom;
Public Classservice () ... {
Try ... {
Saxbuilder builder=new Saxbuilder ();
This.dom=builder.build (ClassService.class.getResource ("Product.xml"));
}catch (Exception e) ... {
E.printstacktrace ();
}
}
Public productclass[] GetAllClass1 () ... {
Collection products=new ArrayList ();
Iterator Iterator=this.dom.getrootelement (). GetChildren (). iterator ();
Do ... {
Element element= (Element) Iterator.next ();
Productclass product=new Productclass (element.getattributevalue ("id"),
Element.getattributevalue ("ClassName"));
Products.add (product);
}while (Iterator.hasnext ());
Return (productclass[]) Products.toarray (new productclass[0]);
}
Public productclass[] Getallclass2byid (String class1id) ... {
Collection products=new ArrayList ();
Element Classelement=null;
Iterator Iterator=this.dom.getrootelement (). GetChildren (). iterator ();
Do ... {
Element element= (Element) Iterator.next ();
if (Class1id.equalsignorecase element.getattributevalue ("id")) ... {
Classelement=element;
Break
}
}while (Iterator.hasnext ());
if (classelement!=null) ... {
Iterator Iter=classelement.getchildren (). iterator ();
Do ... {
Element element= (Element) Iter.next ();
Productclass product=new Productclass (element.getattributevalue ("id"),
Element.getattributevalue ("ClassName"));
Products.add (product);
}while (Iter.hasnext ());
Return (productclass[]) Products.toarray (new productclass[0]);
}
else ... {
return null;
}
}
Public productclass[] Getallclass3byid (String class1id,string class2id) ... {
Collection products = new ArrayList ();
Element class1element = null;
Element class2element = null;
Iterator iterator = This.dom.getRootElement (). GetChildren (). iterator ();
Do ... {
element element = (Element) Iterator.next ();
if (Class1id.equalsignorecase element.getattributevalue ("id")) ... {
Class1element = element;
Break
}
}while (Iterator.hasnext ());
if (class1element!=null) ... {
Iterator iter = Class1element.getchildren (). iterator ();
Do ... {
element element = (Element) Iter.next ();
if (Class2id.equalsignorecase element.getattributevalue ("id")) ... {
Class2element = element;
Break
}
}while (Iter.hasnext ());
if (class2element!=null) ... {
Iterator iter2 = Class2element.getchildren (). iterator ();
Do ... {
element element = (Element) Iter2.next ();
Productclass Product = new Productclass (Element.getattributevalue ("id"), Element.getattributevalue ("ClassName"));
Products.add (product);
}while (Iter2.hasnext ());
}
Return (productclass[]) Products.toarray (new productclass[0]);
}
else return null;
}
}
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE class SYSTEM "Product.dtd" >
<class>
<class1 classname= "Computer Accessories" id= "1" >
<class2 classname= "Memory" id= "1" >
<CLASS3 id= "1" classname= "Kingmax" ></class3>
<CLASS3 id= "2" Classname= "Kingston" ></class3>
<CLASS3 id= "3" classname= "Samsung" ></class3>
<CLASS3 id= "4" classname= "Hydadi" ></class3>
<CLASS3 id= "5" classname= "IBM" ></class3>
</class2>
<class2 classname= "Hard Drive" id= "2" >
<CLASS3 id= "6" classname= "hithait" ></class3>
<CLASS3 id= "7" classname= "IBM" ></class3>
<CLASS3 id= "8" classname= "Samsung" ></class3>
<CLASS3 id= "9" classname= "Westdata" ></class3>
</class2>
</class1>
<class1 classname= "Food Accessories" id= "2" >
<class2 classname= "Hamburger" id= "1" >
<CLASS3 id= "1" classname= "McDonald's" ></class3>
<CLASS3 id= "2" classname= "Ken Base" ></class3>
<CLASS3 id= "3" classname= "Roger Silk" ></class3>
</class2>
<class2 classname= "Drink" id= "2" >
<CLASS3 id= "4" classname= "CocaCola" ></class3>
<CLASS3 id= "5" classname= "Sprite" ></class3>
<CLASS3 id= "6" classname= "Coffee" ></class3>
<CLASS3 id= "7" classname= "Water" ></class3>
</class2>
</class1>
</class>
<?xml version= "1.0" encoding= "GB2312"?>
<! ELEMENT class (class1+) >
<! ELEMENT Class1 (class2+) >
<! attlist Class1 className NmToken #REQUIRED >
<! attlist Class1 ID nmtoken #REQUIRED >
<! ELEMENT class2 (class3+) >
<! Attlist class2 className NmToken #REQUIRED >
<! attlist class2 ID nmtoken #REQUIRED >
<! ELEMENT CLASS3 empty>
<! Attlist class3 className NmToken #REQUIRED >
<! Attlist class3 ID nmtoken #REQUIRED >
Jsp:
(1) getclass.jsp acts as a business layer for AJAX calls
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ page import= "com.ajaxlab.ajax.*"%>
<%
String class1id = Request.getparameter ("Class1id");
String class2id = Request.getparameter ("Class2id");
if ("". Equals (class1id)) class1id = null;
if ("". Equals (class2id)) class2id = null;
Classservice service = new Classservice ();
if ((class1id!=null) && (class2id==null)) ... {
productclass[] classes = Service.getallclass2byid (class1id);
if (classes!=null) ... {
for (int i=0;i<classes.length;i++) ... {
Out.print (Classes[i].getid () + "," +classes[i].getclassname () + "|");
}
}
}
else if ((class1id!=null) && (class1id!=null)) ... {
productclass[] classes = Service.getallclass3byid (CLASS1ID,CLASS2ID);
if (classes!=null) ... {
for (int i=0;i<classes.length;i++) ... {
Out.print (Classes[i].getid () + "," +classes[i].getclassname () + "|");
}
}
}
%>
(2) divmenu.jsp
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ page import= "com.ajaxlab.ajax.*"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<script type= "Text/javascript" src= "Ajax_func.js" ></script>
<script type= "Text/javascript" >
function Dochange () ... {
var f = document.forms[0];
Send_request ("Get", "getclass.jsp?class1id=" +f.select11.value+ "&class2id=", NULL, "TEXT", populateClass2);
}
function DoChange2 () ... {
var f = document.forms[0];
Send_request ("Get", "getclass.jsp?class1id=" +f.select11.value+ "&class2id=" +f.select12.value,null, "TEXT", POPULATECLASS3);
}
function PopulateClass2 () ... {
var f=document.forms[0];
if (http_request.readystate==4) ... {
if (http_request.status==200) ... {
var List=http_request.responsetext;
var classlist=list.split ("|");
f.select12.options.length=1;
for (Var i=0;i<classlist.length-1;i++) ... {
var Temp=trim (Classlist[i]). Split (",");
F.select12.add (New Option (temp[1],temp[0));
}
}
}
}
function PopulateClass3 () ... {
var f=document.forms[0];
if (http_request.readystate==4) ... {
if (http_request.status==200) ... {
var List=http_request.responsetext;
var classlist=list.split ("|");
f.select13.options.length=1;
for (Var i=0;i<classlist.length-1;i++) ... {
var Temp=trim (Classlist[i]). Split (",");
F.select13.add (New Option (temp[1],temp[0));
}
}
}
}
function LTrim (str)
... {
var whitespace = new String ("");
var s = new String (str);
if (Whitespace.indexof (S.charat (0))!=-1)
... {
var j=0, i = s.length;
while (J < i && Whitespace.indexof (S.charat (j))!=-1)
... {
j + +;
}
s = s.substring (j, I);
}
return s;
}
function RTrim (str)
... {
var whitespace = new String ("");
var s = new String (str);
if (Whitespace.indexof (S.charat (s.length-1))!=-1)
... {
var i = s.length-1;
while (i >= 0 && whitespace.indexof (s.charat (i))!=-1)
... {
i--;
}
s = s.substring (0, i+1);
}
return s;
}
function Trim (str)
... {
Return RTrim (LTrim (str));
}
</script>
<%
Classservice service = new Classservice ();
productclass[] classes = Service.getallclass1 ();
%>
<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 ">
<title>insert title here</title>
<body><center>
<form name= "Classform" method= "POST" action= "" >
<select name= "select11" id= "select11" onchange= "Dochange (this.value)" >
<option value= "" > Please select Category one </option>
<%
for (int i=0;i<classes.length;i++) ... {
Out.println ("<option value=" "+classes[i].getid () +" ' > "+classes[i].getclassname () +" </option> ");
}
%>
</select>
<select name= "select12" id= "select12" onchange= "DoChange2 (this.value)" >
<option value= "" > Please select Category two </option>
</select>
<select name= "select13" id= "select13" >
<option value= "" > Please select Category three </option>
</select>
</form>
</center></body>