AJAX的异步请求小例子
生活随笔
收集整理的這篇文章主要介紹了
AJAX的异步请求小例子
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
JSP頁面:
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>AJAX異步請求</title><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="cache-control"content="no-cache"><metahttp-equiv="expires"content="0"><!--<linkrel="stylesheet" type="text/css"href="styles.css">-->
<scripttype="text/javascript">window.onload =function(){//異步請求document.getElementById("btn").onclick = function(){//ajax開發(fā)步驟//1、創(chuàng)建一個(gè)異步對象//低版本IE(6.0或以前)或IE高版本: ActiveXObject對象//火狐或IE高版本(7.0或以上): XMLHttpRequest對象varajax;try{ajax =newXMLHttpRequest();}catch(e){ajax =newActiveXObject("microsoft.xmlhttp");} //2、準(zhǔn)備請求,打開請求//url:請求地址//method:提交方式:get和postvarurl ="<c:url value='/getTime'/>";varmethod="GET";ajax.open(method, url);//3、設(shè)置并發(fā)送的請求內(nèi)容(正文)//如果是GET一般為null,POST才需要指定的請求正文ajax.send(null);//4、先監(jiān)聽服務(wù)器的返回狀態(tài),只有狀態(tài)ok的請求才回處理ajax.onreadystatechange =function(){//alert("服務(wù)器請求的狀態(tài):"+ajax.readyState);//alert("服務(wù)器響應(yīng)的狀態(tài):"+ajax.status);if(ajax.readyState==4 && ajax.status==200){//5.接收服務(wù)器的數(shù)據(jù)varcurTime = ajax.responseText; //網(wǎng)頁局部刷新document.getElementById("time").innerHTML = curTime;}}alert(ajax);}}
</script></head><body><inputid="btn"type="button"name="btn"value="異步請求"/><br><spanid="time"></span><br><inputtype="text"/></body>
</html>
創(chuàng)建一個(gè)Servlet,在web.xml配置好訪問路徑 package star.july.ajax; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; publicclass getTimeServlet extends HttpServlet {@Overrideprotectedvoid doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Date curTime= new Date();//格式化日期SimpleDateFormat sf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String format= sf.format(curTime);//設(shè)置頁面編碼response.setContentType("text/html;charset=utf-8");//傳遞數(shù)據(jù)回頁面response.getWriter().write("現(xiàn)在時(shí)間是:"+format);}@Overrideprotectedvoid doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doGet(req, resp);} }
創(chuàng)建一個(gè)Servlet,在web.xml配置好訪問路徑 package star.july.ajax; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; publicclass getTimeServlet extends HttpServlet {@Overrideprotectedvoid doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Date curTime= new Date();//格式化日期SimpleDateFormat sf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String format= sf.format(curTime);//設(shè)置頁面編碼response.setContentType("text/html;charset=utf-8");//傳遞數(shù)據(jù)回頁面response.getWriter().write("現(xiàn)在時(shí)間是:"+format);}@Overrideprotectedvoid doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doGet(req, resp);} }
總結(jié)
以上是生活随笔為你收集整理的AJAX的异步请求小例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring中的简单实现AOP小例子
- 下一篇: JSON与Struts2的结合使用