INDEX.JSP:
Copy Code code as follows:
<%@ page language= "java" pageencoding= "UTF-8"%>
<title> level Two Menu linkage demo </title>
<script type= "Text/javascript" >
var req;
Window.onload=function ()
{//Page load function
}
function Change_select () {//Called when the option of the first Drop-down box changes
var province = document.getElementById (' province '). Value;
var url = "select?id=" + Escape (province);
if (window. XMLHttpRequest) {
req = new XMLHttpRequest ();
}else if (window. ActiveXObject) {
req = new ActiveXObject ("Microsoft.XMLHTTP");
}
if (req) {
Req.open ("Get", url,true);
Specifies that the callback function is callback
Req.onreadystatechange = callback;
Req.send (NULL);
}
}
callback function
function callback () {
if (Req.readystate ==4) {
if (Req.status ==200) {
Parsemessage ()//parsing XML document
}else{
Alert ("Cannot get descriptive information:" + req.statustext);
}
}
}
Resolving methods to return XML
function Parsemessage () {
var xmldoc = req.responsexml.documentelement;//Gets the returned XML document
var Xsel = xmldoc.getelementsbytagname (' select ');
Get all the <select> tags in an XML document
var select_root = document.getElementById (' city ');
Get the second Drop-down box in the Web page
select_root.options.length=0;
Each time the new data is obtained, the length of each two drop frame is cleared 0
for (Var i=0;i<xsel.length;i++) {
var xvalue = Xsel[i].childnodes[0].firstchild.nodevalue;
Gets the value of the first tag in each <select> tag, which is the value of the <value> tag
var xText = Xsel[i].childnodes[1].firstchild.nodevalue;
Gets the value of the second tag in each <select> tag, which is the value of the <text> tag
var option = new option (XText, Xvalue);
Create an Option object based on the values of each group of value and text tags
try{
Select_root.add (option)://Add Option object to second Drop-down box
}catch (e) {
}
}
}
</script>
<body>
<div align= "center" >
<form name= "Form1" method= "Post" action= "" >
<table width= "70%" border= "0" cellspacing= "0" cellpadding= "0" >
<tr>
<TD align= "center" >
Two-Level linkage example
</td>
</tr>
<tr>
<td>
<select name= "province" id= "province" onchange= "Change_select ()" >
<!--first Drop-down menu-->
<option value= "0" >
Please select
</option>
<option value= "1" >
Beijing
</option>
<option value= "2" >
Tianjin
</option>
<option value= "3" >
Shandong
</option>
</select>
<select name= "City" id= "City" >
<!--The second Pull-down menu-->
<option value= "0" >
Please select
</option>
</select>
</td>
</tr>
<tr>
<td>
</td>
<tr>
</table>
</form>
</div>
</body>
Selectservlet:
Copy Code code as follows:
Package com;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
/***
*
* @author ZDW
*
*/
public class Selectservlet extends HttpServlet
{
Private static final long serialversionuid = 1L;
Public Selectservlet ()
{
Super ();
}
public void Destroy ()
{
Super.destroy ();
}
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException
{
Response.setcharacterencoding ("GBK");
Response.setcontenttype ("Text/xml");
Response.setheader ("Cache-control", "No-cache");
Request.setcharacterencoding ("GBK");
Response.setcharacterencoding ("UTF-8");
String Targetid = request.getparameter ("id"). toString ();
System.out.println (Targetid);
Get the value of the parameter ID in the request
String Xml_start = "<selects>";
String xml_end = "</selects>";
String xml = "";
if (Targetid.equalsignorecase ("0"))
{
XML = "<select><value>0</value><text> please choose </text></select>";
else if (Targetid.equalsignorecase ("1"))
{
XML = "<select><value>1</value><text> changping </text></select>";
XML = "<select><value>2</value><text> Fengtai </text></select>";
XML + = "<select><value>3</value><text> Haidian </text></select>";
XML = "<select><value>4</value><text> Chaoyang </text></select>";
else if (Targetid.equalsignorecase ("2"))
{
XML = "<select><value>1</value><text> Tanggu District </text></select>";
XML + = "<select><value>2</value><text> Hangu District </text></select>";
XML + = "<select><value>3</value><text> Grand Port District </text></select>";
XML + = "<select><value>4</value><text> Dongli District </text></select>";
} else
{//If 3, return the following character
XML = "<select><value>1</value><text> Jinan </text></select>";
XML = "<select><value>2</value><text> qingdao </text></select>";
XML = "<select><value>3</value><text> Zibo </text></select>";
XML = "<select><value>4</value><text> Zaozhuang </text></select>";
}
String Last_xml = xml_start + xml + xml_end;
Response.getwriter (). write (Last_xml);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException
{
Doget (request, response);
}
public void Init () throws Servletexception
{
}
}
Xml:
Copy Code code as follows:
<?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>
<servlet-name>SelectServlet</servlet-name>
<servlet-class>com. Selectservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> Selectservlet</servlet-name>
<url-pattern>/select</url-pattern>
</servlet-mapping >
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</ Welcome-file-list>
</web-app>