The original ecology of simple JavaScript Ajax is only for the record:
Page code
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Background
public class Provinceaction extends Action {/* generated Methods//** * Method Execute * @param mapping * @param form * @param request * @param response * @return Actionforward/Public Actionforward Execute ( actionmapping mapping, Actionform form, httpservletrequest request, httpservletresponse response) {//Provinceform
Provinceform = (provinceform) Form;
System.out.println ("---------------Province is requested!------------------");
String method = Request.getparameter (' method ');
String pr[] = {"Beijing", "Tianjin", "Shanghai", "Guangzhou", "Hebei", "Shandong"};
String city[] = {"1", "2", "3", "4", "5", "6"};
StringBuffer sb = new StringBuffer (); if (Method!=null&&method.trim (). Length () >0) {try {Response.setcontenttype () ("Text/xml;charset=utf -8 ");
This sentence and the following must not be reversed position, otherwise the page will appear garbled printwriter pw = Response.getwriter ();
Sb.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>"); if (Method.equalsignorecase ("innitProvince ")) {sb.append (" <province> ");
for (Object obj:pr) {String pro = (string) obj;
Sb.append ("<name>" +pro+ "</name>");
} sb.append ("</province>");
Pw.write (Sb.tostring ());
Pw.flush ();
else {sb.append ("<province>");
for (Object obj:city) {String pro = (string) obj;
Sb.append ("<name>" +pro+ "</name>");
} sb.append ("</province>");
Pw.write (Sb.tostring ());
Pw.flush ();
} catch (IOException e) {e.printstacktrace ();
} return null; }
}