ajax实例01
ajax 实例
项目结构图:
各文件内容如下
Ajax.js:
var xhr;
function createXHR(){
var xhr;
try{
xhr=new ActiveXObject("Msxml2.XMLHTTP");
return xhr;
}catch(e){
try{
xhr=new ActiveXObject("Microsoft.XMLHTTP");
return xhr;
}catch(ee){
xhr=false;
}
}
if(!xhr&&typeof XMLHttpRequest!='undefined'){
xhr=new XMLHttpRequest();
return xhr;
}
return xhr;
}
AjaxServlet01.java:
package com.ajax.hw.web;
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;
/**
* Servlet implementation class AjaxServlet01
*/
public class AjaxServlet01 extends HttpServlet
{
private static final long serialVersionUID = 1L;
public AjaxServlet01()
{
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("request!");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("来自servlet的信息!");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException
{
doGet(request, response);
}
}
test01.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/Ajax.js"></script>
<script type="text/javascript">
var xhr = createXHR();
xhr.onreadystatechange = onResponse;
xhr.open("GET", "AjaxServlet01", true);
xhr.send(null);
function onResponse2(){
//alert(xhr);
}
function onResponse() {
var obj = xhr;
&nb
- 发表评论
-
- 最新评论 更多>>