Ajax技术概要

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

Ajax技术概要

1.Ajax原理

Ajax即Asynchronous JavaScript and XML,异步Javascript和XML.

Ajax不是一种新的技术,而是几种已有技术的整合,它采用XMLHttpRequest对象进行数据的异步交互,使用javascript增强用户体验,使用DOM组织内容,CSS显示外观,使用XML封装数据。

Ajax的创新点在于,无须刷新页面即可更新页面的部分内容,而传统方式页面内容改变时必须刷新整个页面。应用Ajax技术的包括Google Maps,Gmail,Youtube,and Facebook tabs.

重点需要掌握的概念:

同步机制:传统的Web交互中,首先用户通过界面触发一个HTTP请求到服务器,服务器根据请求内容进行相应的处理,然后将处理完毕的内容再返回给客户端,这是一个同步的过程,当服务器处理客户端提交的请求时,客户端的用户只能空闲等待,无论用户是想处理很少的数据,还是要进行大批量的数据处理,服务器都会给用户返回一个完整的页面

异步机制:Ajax采取异步机制,使得用户可以一边等待反应,以便查看网页中的内容。Ajax的工作原理相当于在用户和服务器之间加了一个引擎,这个引擎用来统一处理提交的数据,它把不需要服务器端处理的数据直接返回给页面,而只把需要后台处理的数据发送给服务器端。在这种异步交互下,客户端不需要重新读取整个页面,而只读取需要的那部分内容,带来了更好的用户体验。

Ajax工作原理如下图(来自:http://www.w3schools.com/ajax/ajax_intro.asp)所示:

下面通过获取时间信息的传统处理方式和利用Ajax的异步方式的示例,来增加理解。

示例1-1传统Web方式

timeMsgOld.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ page import="java.sql.*,java.util.*,javax.servlet.*" %>
  4. <%
  5. //获取服务器传来的数据
  6. String msg = (String)((request.getAttribute("msg")==null)
  7. ?"":(String)request.getAttribute("msg"));
  8. %>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  10. <html>
  11. <head>
  12. <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  13. <title>传统的Web开发方式</title>
  14. </head>
  15. <body>
  16. <form action="/ServletDemos/timeMsgOld.do" method="get">
  17. <span id="msg"><%=msg %></span>
  18. <input type = "submit" value="单击此按钮">
  19. </form>
  20. </body>
  21. </html>

TimeMsgOld.java
  1. package com.wangdq;
  2.  
  3. import java.io.IOException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6.  
  7. import javax.servlet.RequestDispatcher;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. /**
  15. * Servlet implementation class,TimeMsgOld
  16. */
  17. @WebServlet("/timeMsgOld.do")
  18. public class TimeMsgOld extends HttpServlet {
  19. private static final long serialVersionUID = 1L;
  20. public TimeMsgOld() {
  21. super();
  22. // TODO Auto-generated constructor stub
  23. }
  24. protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
  25. // TODO Auto-generated method stub
  26. response.setContentType("text/plain;charset=UTF-8");
  27. request.setAttribute("msg","现在时间是: "+getCurrentDateAndTime()+" 欢迎您!");
  28. RequestDispatcher rd = request.getRequestDispatcher("/timeMsgOld.jsp");
  29. rd.forward(request,response);
  30. }
  31. /**
  32. * 获取当前系统时间
  33. */
  34. private String getCurrentDateAndTime() {
  35. String currentDate = "";
  36. SimpleDateFormat sdf = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
  37. currentDate = sdf.format(new Date( ));
  38. return currentDate;
  39. }
  40. protected void doPost(HttpServletRequest request,IOException {
  41. // TODO Auto-generated method stub
  42. this.doGet(request,response);
  43. }
  44.  
  45. }

执行结果如下:

此种方式下,要获取时间信息,整个页面将刷新。


示例1-2Ajax异步刷新

timeMsg.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Ajax</title>
  8. <script type="text/javascript">
  9. function ok() {
  10. var xmlhttp;
  11. if(window.XMLHttpRequest) {
  12. // code for IE7+,Firefox,Chrome,Opera,Safari
  13. xmlhttp = new XMLHttpRequest();
  14. }else if(window.ActiveXOjbect) {
  15. // code for IE6,IE5
  16. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  17. }
  18. //设定回调函数,当数据返回时系统会执行这个回调函数
  19. xmlhttp.onreadystatechange = function() {
  20. if(xmlhttp.readyState==4 && xmlhttp.status==200) {
  21. document.getElementById("msg").innerHTML = xmlhttp.responseText ;
  22. }
  23. }
  24. //设定请求
  25. xmlhttp.open("get","http://localhost:8080/ServletDemos/timeMsg.do",true);
  26. //设定http头
  27. xmlhttp.setRequestHeader("Content-Type","application/x-www-form-unlencoded");
  28. //发送请求
  29. xmlhttp.send(null);
  30. }
  31. </script>
  32. </head>
  33. <body>
  34. <span id="msg"></span><br>
  35. <input type = "button" value="单击此按钮" onClick="ok()">
  36. </body>
  37. </html>

TimeMsg.java
  1. package com.wangdq;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7.  
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. /**
  15. * Servlet implementation class,TimeMsg
  16. */
  17. @WebServlet("/timeMsg.do")
  18. public class TimeMsg extends HttpServlet {
  19. private static final long serialVersionUID = 1L;
  20. public TimeMsg() {
  21. super();
  22. }
  23. protected void doGet(HttpServletRequest request,IOException {
  24. response.setContentType("text/plain;charset=UTF-8");
  25. PrintWriter out = response.getWriter();
  26. out.write("现在时间是: "+getCurrentDateAndTime()+" 欢迎您!");
  27. out.flush();
  28. out.close();
  29. }
  30. /**
  31. * 获取当前系统时间
  32. */
  33. private String getCurrentDateAndTime() {
  34. String currentDate = "";
  35. SimpleDateFormat sdf = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
  36. currentDate = sdf.format(new Date( ));
  37. return currentDate;
  38. }
  39. protected void doPost(HttpServletRequest request,HttpServletResponse response)
  40. throws ServletException,IOException {
  41. this.doGet(request,response);
  42. }
  43.  
  44. }

运行结果如下图所示:


此时获取实现信息,不必刷新整个页面

猜你在找的Ajax相关文章