关于ajax传参的问题!!急!!

chenwei023 2006-11-15 05:36:47
我写了个jsp文件和一个servlet,具体代码如下:
getAndPostExample.jsp
<html>
<head>
</head>
<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.open("GET",queryString,true);
xmlHttp.onreadystatechange=handleStateChange;
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-urlencode;");
xmlHttp.send(queryString);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
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"/>
</tr>
<tr>
<td>Middle name:</td>
<td><input type="text" id="middleName"/>
</tr>
<tr>
<td>Birthday</td>
<td><input type="text" id="birthday"/>
</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 test;

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 GetAndPostExample extends HttpServlet {

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

processRequest(request, response, "GET");
}

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

processRequest(request,response,"POST");
}

protected void processRequest(HttpServletRequest request,
HttpServletResponse response, String method) throws IOException {

response.setContentType("text/xml");
String firstName = request.getParameter("firstName");
String middleName = request.getParameter("middleName");
String birthday = request.getParameter("birthday");
String responseText = "Hello " + firstName + " " + middleName
+ " Your birthday is " + birthday + "." + "[Method: " + method
+ "]";
PrintWriter out = response.getWriter();
out.println(responseText);
out.close();

}

}
可是当点击按钮的时候出不来我输入的东西,出来的东西很奇怪,如下:
<html><head><title>Apache Tomcat/5.0.28 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - /test/GetAndPostExample</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>/test/GetAndPostExample</u></p><p><b>description</b> <u>The requested resource (/test/GetAndPostExample) is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.0.28</h3></body></html>
请问各位大侠这是什么错误啊??
...全文
470 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
windvscloud 2006-11-15
  • 打赏
  • 举报
回复
HTTP Status 404 -
/test/GetAndPostExample
Status report
message
/test/GetAndPostExample
description
The requested resource (/test/GetAndPostExample) is not available
看看是不是ajax的url写错了

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧