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

ajax AjaxServletCacheMicrosoftSQL 

程序员文章站 2022-07-15 16:02:48
...
脚本
function getActDefined(stateVal) {
var url = "/servlet/InitAddCustSelect?servCode=" + stateVal;
        if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if(req) {
req.open("GET", url, true);
req.onreadystatechange = complete;
req.send(null);
        }
}

function complete() {
if (req.readyState == 4) {
if (req.status == 200) {
var values = req.responseXML.getElementsByTagName("actDefinedId");
var lables = req.responseXML.getElementsByTagName("name");
var ips = req.responseXML.getElementsByTagName("ipnums");
var firstInstallFee = req.responseXML.getElementsByTagName("firstInstallFee");
var prepayFee = req.responseXML.getElementsByTagName("prepayFee");
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;
}
var actDefinedVal = document.theForm.Act_define_id.value;
for(var i = 0; i < values.length; i++) {
var tempActDefined = values[i].firstChild.data;
if (actDefinedVal == tempActDefined) {
document.theForm.Ipnum.value = ips[i].firstChild.data;
document.theForm.firstfee.value = firstInstallFee[i].firstChild.data;
document.theForm.prepfee.value = prepayFee[i].firstChild.data;;
}
}
            }
        }
}

java:
package topway.common;

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;

/**
* @author will
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class InitAddCustSelect
    extends HttpServlet {

  public InitAddCustSelect() {
    super();
  }

  public void destroy() {
    super.destroy();
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    //request.setCharacterEncoding("text/html;CHARSET=utf8");
    response.setContentType("text/xml;CHARSET=utf8");
    response.setHeader("Cache-Control", "no-cache");
    String state = request.getParameter("servCode");
    System.out.println(state);
    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);
  }

}