欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ajax AjaxServletJavaXMLJavaScript 

程序员文章站 2022-05-30 12:07:25
...
ajax(Asynchronous JavaScript and XML, 非同步 JavaScript 及 XML技术);两大功能:
   在不重新读取页面的情況下对服务器送出请求,如连动下拉列表呀;
   解析、使用 XML 文件.

function getActDefined(stateVal); {
		var url = "/servlet/InitAddCustSelect?servCode=" + stateVal;//stateVal是从页面上传到后台servlet的值。
        if (window.XMLHttpRequest); {//// Mozilla, Safari.....
			req = new XMLHttpRequest();;
        } else if (window.ActiveXObject); {//IE(可跨浏览器);
			req = new ActiveXObject("Microsoft.XMLHTTP");;
        }
        if(req); {
	req.open("GET", url, true);;
	req.onreadystatechange = complete;//指定的函式名称后不加括号
	req.send(null);;
        }
}

complete函数,接受返回值并解析。
function complete(); {
         if (req.readyState == 4); {//request的返回状态;4表示数据已传回
	if (req.status == 200); {//http接受数据状态;200ok,404文件未找到;500服务器内部错
	var values = req.responseXML.getElementsByTagName("actDefinedId");;//解析
	var lables = req.responseXML.getElementsByTagName("name");;			document.theForm.Act_define_id.length = values.length;
	for(var i = 0; i < values.length; i++); {			        document.theForm.Act_define_id.options[i].value = values[i].firstChild.data;//读入下拉表。		document.theForm.Act_define_id.options[i].text = lables[i].firstChild.data;
	}}
}
}


处理业务的servlet:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import topway.common.Helper;
import topway.business.SelectParam;
public class InitAddCustSelect
    extends HttpServlet {
  public InitAddCustSelect(); {
    super();;
  }
  public void destroy(); {
    super.destroy();;
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response); throws
      ServletException, IOException {
      response.setContentType("text/xml;CHARSET=utf8");;
    response.setHeader("Cache-Control", "no-cache");;
    String state = request.getParameter("servCode");;//接收request传来的参数
       StringBuffer sb = new StringBuffer(
        "<?xml version=\"1.0\" encoding=\"GB2312\"?>");;
    sb.append("<addCustSelect>");;
    Connection conn = null;
    try {
      conn = Helper.getConnection();;
      SelectParam selectBean = new SelectParam();;
      ResultSet actDefine = selectBean.getActDefine(conn, state);;
      while (actDefine.next();); {
        sb.append("<actDefinedId>" + actDefine.getString(1); + "</actDefinedId>");;
        sb.append("<name>" + actDefine.getString(2); + "</name>");;
        sb.append("<prepayFee>" + actDefine.getString(3); + "</prepayFee>");;
        sb.append("<firstInstallFee>" + actDefine.getString(4); +
                  "</firstInstallFee>");;
        sb.append("<ipnums>" + actDefine.getString(5); + "</ipnums>");;
      }
    }
    catch (SQLException ex); {
      ex.printStackTrace();;
    }
    finally {
      try {
        conn.close();;
      }
      catch (SQLException ex); {
        ex.printStackTrace();;
      }
    }
    sb.append("</addCustSelect>");;
    PrintWriter out = response.getWriter();;
    out.write(sb.toString(););;//输出
    out.close();;
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response); throws
      ServletException, IOException {
    doGet(request, response);;
  }
}


加载webapplication时初始化servlet,在web.xml中:
<servlet>
                <servlet-name>ServletNames</servlet-name>
	<servlet-class>cn.common.InitAddCustSelect</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>ServletNames</servlet-name>
	<url-pattern>/servlet/InitAddCustSelect</url-pattern>
</servlet-mapping>