Ajax allows you to dynamically load the instance code of a combo box.
1. province. jsp
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%>
Ii. CityByXMLServlet. java
Package servlet; import java. io. IOException; import java. io. printWriter; import java. util. *; 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 CityByXMLServlet */@ WebServlet ("/CityByXMLServlet") public class CityByXMLServlet extends HttpServlet {private static final long serialVersionUID = 1L; private static final String CONTENT_TYPE = "text/xml; charset = UTF-8";/*** @ see HttpServlet # HttpServlet () */public CityByXMLServlet () {super (); // TODO Auto-generated constructor stub}/*** @ see HttpServlet # doGet (HttpServletRequest request, response) */public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType (CONTENT_TYPE); // set the server response type String province = request. getParameter ("province"); StringBuffer city = new StringBuffer ("<citys>"); // record returned XML string object if ("gx ". equals (province) {List list = cityInit (); // obtain the city list for (int I = 0; I <List. size (); I ++) {city. append ("<city>" + list. get (I) + "</city>") ;}} else if ("hn ". equals (province) {List list = cityInit1 (); // obtain the city list for (int j = 0; j <List. size (); j ++) {city. append ("<city>" + list. get (j) + "</city>") ;}} else if ("hb ". equals (province) {List list = cityInit2 (); // obtain the city list for (int j = 0; j <List. size (); j ++) {city. append ("<city>" + list. get (j) + "</city>") ;}} city. append ("</citys>"); PrintWriter out = response. getWriter (); out. println (city. toString (); out. flush (); // refresh the output stream out. close (); // close the output stream}/** initialize city */public List <String> cityInit2 () {List <String> cityList = new ArrayList <String> (); // Add the cityList. add ("Wuhan"); cityList. add ("Xiangyang"); cityList. add ("Huanggang"); cityList. add ("Jingmen"); cityList. add ("Shiyan"); cityList. add ("Huangshi"); return cityList;} public List <String> cityInit () {List <String> cityList = new ArrayList <String> (); // Add the cityList. add ("Nanning"); cityList. add ("Guilin"); cityList. add ("Beihai"); cityList. add ("river pool"); cityList. add ("Wuzhou"); cityList. add ("Yulin"); return cityList;} public List <String> cityInit1 () {List <String> cityList = new ArrayList <String> (); // Add the cityList. add ("Changsha"); cityList. add ("Xiangtan"); cityList. add ("Yueyang"); cityList. add ("Changde"); cityList. add ("Hengyang"); cityList. add ("Shaoyang"); return cityList;}/*** initialize the current Servelt. <br> ** @ throws ServletException when ServletExceptio occurs */public void init () throws ServletException {} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}
Web. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: web = "http://xmlns.jcp.org/xml/ns/javaee" xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version = "2.4"> <servlet-name> CityByXMLServlet </servlet-n Ame> <servlet-class> servlet. CityByXMLServlet </servlet-class> <! -- Class location --> </servlet> <servlet-mapping> <servlet-name> CityByXMLServlet </servlet-name> <! -- The class name you created --> <url-pattern>/CityByXMLServlet </url-pattern> </servlet-mapping>
Summary
The above is the example code of Ajax to dynamically load a combo box. I hope it will be helpful to you. If you have any questions, please leave a message. The editor will reply to you in time!