AJAX最简单的原理以及应用

Ajax是创建快速动态网页的技术,通过后台与服务器少量的数据交互,是网页实现异步更新。也就是在不整个刷新页面的情况下,可以更新网页中的局部区域。

在原始web应用的模式中:

浏览器       以 http的形式向服务器发送请求,然后服务器处理请求,然后以响应(HTML+CSS)数据返回给客户端;

AJXA应用中:

浏览器  以http发送的请求到达AJAX引擎,由Ajax向服务器进行请求发送数据,处理完成后,把数据继续返回给Ajax引擎,再以XML或者字符串数据,返回给浏览器;


ajax.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>




 
    
    My JSP 'ajax.jsp' starting page
   
    

 
 
 
   
    
   

 


AjaxServlet.java

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;

public class AjaxServlet extends HttpServlet
{

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

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        System.out.println("doGet invoked");
        out.println("Hello World AJAX");
        
        out.flush();
        out.close();
    }

}

web.xml



    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 
    This is the description of my J2EE component
    This is the display name of my J2EE component
    AjaxServlet
    com.zhaoming.shopping.servlet.AjaxServlet
 


    AjaxServlet
    /servlet/AjaxServlet
 

    
    
        Index.jsp
        index.html
    


你可能感兴趣的:(java,Web)