Before doing a small Ajax demo, today to see a dynamic Update drop-down list, or also called cascading update drop-down list, this is the use of Ajax asynchronous call to the background to implement data requests, and then back to the foreground to complete the drop-down list of data updates to enhance the interactivity of the Web application.
background servlet
Package Com.ajax;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.ServletException; Import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet implementation Class Createxml */@WebServlet ("/createxml") public class Createxml extends HttpServlet {private static final long serialve Rsionuid = 1L; /** * @see httpservlet#httpservlet () */public Createxml () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {doPost (request, response);} /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) */protected void DoPost ( HttpServletRequest request, httpservletresponse response) thRows Servletexception, IOException {response.setcontenttype ("Text/xml"); Response.setcharacterencoding ("UTF-8"); String selected = Request.getparameter ("selected"); PrintWriter out = Response.getwriter () out.println ("<response>");//The following are two provinces to create a city if (Selected.equals ("1")) {// If the selection is "Hunan Province" Out.println ("<city>"); Out.println ("<cityname> Changsha </cityname>"); Out.println ("< Cityvalue>1</cityvalue> "), Out.println (" </city> "), Out.println (" <city> "); Out.println (" < cityname> Loudi </cityname> ") out.println (" <cityvalue>2</cityvalue> "); Out.println (" </city > "), Out.println (" <city> "), Out.println (" <cityname> changde </cityname> "), Out.println (" < Cityvalue>3</cityvalue> "); Out.println (" </city> ");} else{//If the selection is "Guangdong province" Out.println ("<city>") out.println ("<cityname> Guangzhou </cityname>"); Out.println (" <cityvalue>1</cityvalue> "), Out.println (" </city> "); Out.println (" <city> "); Out.println (" <cityname>, Shenzhen </cityname> "), Out.println (" <cityvalue>2</cityvalue> "); Out.println (" </city> "); o Ut.println ("<city>"); Out.println ("<cityname> Foshan </cityname>"); Out.println ("<cityvalue>3 </cityvalue> "); Out.println (" </city> ");} Out.println ("</response>"); Out.flush (); Out.close ();}}
We pushed the data to the front desk for <response><city><cityname> Changsha </cityname><cityvalue>1</cityvalue ></city></response> XML format.
Front Page
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
the above program, the initialization page effect is
This allows the original provinces to not see their own city, not perfect, how to deal with it?
After the page is loaded, let the program go backstage to request the current province of the city. Page load complete, load event, to the body binding load method can be.
Our approach to the process is
<body onload= "Updateselect ()" >
In this way, when the page is loaded, you can see the city of Guangdong Province on the page.
Ajax Dynamic Update drop-down list