Ajax案例(1)

前端之家收集整理的这篇文章主要介绍了Ajax案例(1)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文地址:http://xiongjiajia.iteye.com/blog/1319245

1.简单的级联下拉框

Xml代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appxmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.5"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  3. servletservlet-name>SelectCityServlet</servlet-class>com.demo.servlet.SelectCityServletservlet-mappingurl-pattern>/servlet/SelectCityServletwelcome-file-listwelcome-file>index.jspweb-app>

Java代码
    packagecom.demo.servlet;
  1. importjava.io.IOException;
  2. importjava.io.PrintWriter;
  3. importjavax.servlet.ServletException;
  4. importjavax.servlet.http.HttpServlet;
  5. importjavax.servlet.http.HttpServletRequest;
  6. importjavax.servlet.http.HttpServletResponse;
  7. publicclassSelectCityServletextendsHttpServlet{
  8. publicSelectCityServlet(){super();}
  9. voiddestory(){super.destroy();}
  10. @Override
  11. protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
  12. throwsServletException,IOException{
  13. //TODOAuto-generatedmethodstub
  14. response.setContentType("text/xml");
  15. response.setHeader("Cache-Control","no-cache");
  16. response.setCharacterEncoding("UTF-8");
  17. Stringstate=request.getParameter("state");
  18. StringBufferbuffer=newStringBuffer("<state>");
  19. if("hn".equals(state)){
  20. buffer.append("<city>郑州</city><city>洛阳</city>");
  21. }elseif("hb".equals(state)){
  22. buffer.append("<city>武汉</city><city>荆州</city><city>黄冈</city>");
  23. }
  24. buffer.append("</state>");
  25. PrintWriterout=response.getWriter();
  26. out.write(buffer.toString());
  27. out.close();
  28. }

HTML代码 <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%<%
  • Stringpath=request.getContextPath();
  • StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  • %@H_982_301@ <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"htmlheadscripttype="text/javascript"@H_982_301@ functiongetResult(stateVal){
  •   varurl="servlet/SelectCityServlet?state="+stateVal;
  •   if(window.XMLHttpRequest){
  •   req=newXMLHttpRequest();
  •   }elseif(window.ActiveXObject){
  •   req=newActiveXObject("Microsoft.XMLHTTP");
  •   }
  •   if(req){
  •   req.open("GET",url,true);
  •   req.onreadystatechange=complete;
  •   req.send(null);
  • functioncomplete(){
  •   if(req.readyState==4){
  •   if(req.status==200){
  •   varcity=req.responseXML.getElementsByTagName("city");
  •   file://alert(city.length);
  •   varstr=newArray();
  •   for(vari=0;icity.length;i++){
  •   str[i]=city[i].firstChild.data;
  •   file://alert(document.getElementById("city"));
  •   buildSelect(str,document.getElementById("city"));
  • functionbuildSelect(str,sel){
  •   sel.options.length=0;
  • str.length;i++){
  •   sel.options[sel.options.length]=newOption(str[i],str[i])
  • scriptbodyselectname="state"onChange="getResult(this.value)"@H_982_301@   optionvalue="">Selectoptionoptionvalue="hn">河南optionvalue="hb">湖北selectselectid="city">CITY>
  • 原文链接:https://www.f2er.com/ajax/166905.html

    猜你在找的Ajax相关文章