新手,一个例子,为什么点击没有反应
这是一个ajax基础教程 书上的例子,我照着练习,getAndPostExample.html 和 getAndPostExample.java。
我用javac 编译 getAndPostExample.java 为getAndPostExample.class,
然后getAndPostExample.class和getAndPostExample.html在同一目录下,不知道是哪里出错了,
我访问http://localhost:8080/myapp/getAndPostExample.html
输入First name, Middle name,Birthday,然后点击Send parameters using GET 按钮,没有任何反应
点击Send parameters using POST 按钮,也没有反应。
希望大家指出错误和一些应该注意的东西,谢谢。
getAndPostExample.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sending Request Data Using GET and POST</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp= new XMLHttpRequest();
}
}
function createQueryString(){
var firstName = document.getElementById("firstName").value;
var middleName = document.getElementById("middleName").value;
var birthday = document.getElementById("birthday").value;
var queryString = "firstName=" + firstName + "&middleName=" + middleName + "&birthday=" + birthday;
return queryString;
}
function doRequestUsingGET(){
createXMLHttpRequest();
var queryString = "getAndPostExample?";
queryString = queryString + createQueryString() + "×tamp=" + new Date().getTime();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET",queryString,true);
xmlHttp.send(null);
}
function doRequestUsingPOST(){
createXMLHttpRequest();
var url = "getAndPostExample?timeStamp=" + new Date().getTime();
var queryString = createQueryString();
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(queryString);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
parseResults();
}
}
}
function parseResults(){
var responseDiv = document.getElementById("serverResponse");
if(responseDiv.hasChildNodes()){
responseDiv.removeChild(responseDiv.childNodes[0]);
}
var responseText = document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
}
</script>
</head>
<body>
<h1>Enter your first name,middle name,and birthday:</h1>
<table>
<tbody>
<tr>
<td>First name:</td>
<td><input type="text" id="firstName"></td>
</tr>
<tr>
<td>Middle name:</td>
<td><input type="text" id="middleName"></td>
</tr>
<tr>
<td>Birthday:</td>
<td><input type="text" id="birthday"></td>
</tr>
</tbody>
</table>
<form action="#">
<input type="button" value="Send parameters using GET" onclick="doRequestUsingGET();"/>
<br/><br/>
<input type="button" value="Send parameters using POST" onclick="doRequestUsingPOST();"/>
</form>
<br/>
<h2>Server Response:</h2>
<div id="serverResponse"></div>
</body>
</html>
getAndPostExample.java
package ajaxbook.chap3;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class getAndPostExample extends HttpServlet{
protected void processRequest(HttpServletRequest request,HttpServletResponse response,String method)
throws ServletException,IOException{
//Set content type of the response to text/xml
response.setContentType("text/xml");
//Get the user's input
String firstName = request.getParameter("firstName");
String middleName = request.getParameter("middleName");
String birthday = request.getParameter("birthday");
//Create the response text
String responseText = "Hello " + firstName + " " + middleName + ".Your birthday is " + birthday + "."
+ "[Method:" + method + "]";
//Write the response back to the browser
PrintWriter out = response.getWriter();
out.println(responseText);
//Close the writer
out.close();
}
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//Process the request in method processRequest
processRequest(request,response,"GET");
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//Process the request in method processRequest
processRequest(request,response,"POST");
}
}