ajax
程序员文章站
2022-07-12 18:07:44
...
ajax(Asynchronous JavaScript and XML, 非同步 JavaScript 及 XML技术);两大功能:
在不重新读取页面的情況下对服务器送出请求,如连动下拉列表呀;
解析、使用 XML 文件.
complete函数,接受返回值并解析。
处理业务的servlet:
加载webapplication时初始化servlet,在web.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>
上一篇: Ajax
推荐阅读
-
基于Ajax用户名验证、服务条款加载、验证码生成的实现方法
-
支持Ajax跨域访问ASP.NET Web Api 2(Cors)的示例教程
-
jQuery使用ajax方法解析返回的json数据功能示例
-
php+ajax 文件上传代码实例
-
Servlet获取AJAX POST请求中参数以form data和request payload形式传输的方法
-
jQuery+ajax的资源回收处理机制分析
-
jquery与ajax获取特殊字符实例详解
-
HTML5中使用postMessage实现Ajax跨域请求的方法
-
深入Ajax代理的Java Servlet的实现详解
-
SpringBoot+SpringSecurity处理Ajax登录请求问题(推荐)