日期:2014-05-17 浏览次数:20656 次
function change(v1){ var xmlhttp = getXmlHttpRequest(); xmlhttp.open('post','city.do',true); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState == 4){ var txt = xmlhttp.responseText; var arr = txt.split(';'); $('s2').innerHTML = ''; for(i=0;i<arr.length;i++){ var arr1 = arr[i]; var arr2 = arr1.split(','); var op = new Option(arr2[1],arr2[0]); $('s2').options[i] = op; } } }; xmlhttp.send('name = ' +v1); } </script> </head> <body style="font-size:30px;"> <select id="s1" style="width:120px;" onchange="change(this.value);"> <option value="hn">湖南</option> <option value="bj">北京</option> </select> <select id="s2" style="width:120px;"></select> </body>
public class Action extends HttpServlet{ public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ String uri = request.getRequestURI(); String path = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf(".")); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); if(path.equals("/city")){ String name = request.getParameter("name"); if(name.equals("hn")){ out.println("yy,岳阳;hh,怀化;cs,长沙"); }else if(name.equals("bj")){ out.println("cy,朝阳;hd,海淀;dc,东城;xc,西城"); } } out.close(); } }
<welcome-file-list> <welcome-file>city.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Action</servlet-name> <servlet-class>web.Action</servlet-class> </servlet> <servlet-mapping> <servlet-name>Action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>