`

ajax学习

    博客分类:
  • ajax
阅读更多
w3cschool学的
package com.isvi.web.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;

public class AjaxServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("print ajax");
		PrintWriter pw=resp.getWriter();
		pw.println("ajax request");
		pw.flush();
		pw.close();
	}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>ajaxTest.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
	<script type="text/javascript"><!--
var currentHttp;
function ajaxFunction()
 {
 try
    {
   // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
 catch (e)
    {

  // Internet Explorer
   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {

      try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的浏览器不支持AJAX!");
         return false;
         }
      }
    }
   return xmlHttp;
 }
 function sendMessage(){
 	alert("send")  
 	var xmlHttp=ajaxFunction();
 	currentHttp=xmlHttp;
 	alert(xmlHttp);
 	var url="http://localhost:8080/WebStrutsGuoDeGang/servlet/AjaxServlet";
 	xmlHttp.onreadystatechange=stateChanged;
 	xmlHttp.open("GET",url,true);
 	xmlHttp.send(null);
 }
 function stateChanged(){
 	
 	
 	if (currentHttp.readyState==4){
 		alert(currentHttp.responseText);
 	}
 }
 
--></script>
  </head>
  <html>
<body>

<form name="myForm">
用户: <input type="text" name="username" />
时间: <input type="text" name="time" />
     <input type="button" onclick="sendMessage()"> 
</form></body>
</html>

 }
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics