ajax带参数请求服务器问题

再出发666 2008-03-17 02:13:39
我初学ajax,拿着一本叫做《ajax基础教程》来学,做到70多页那,做那个带参数请求服务器的例子,拿着原书自带的代码都不好用。问题是这样的,两个文件,一个叫GetAndPostExample.html,另一个叫GetAndPostExample.java文件,两个文件放在一个包中,但是Html文件就是不能访问java的服务器代码。两个文件如下:
<!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"/>
</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>
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");
}
}
...全文
198 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cbl117 2009-02-10
  • 打赏
  • 举报
回复
楼主请问这个问题你是怎么解决的啊?我的tomcat和java都配置好了 可是还是无法访问成功
骑蚊子旅游 2008-03-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 whyf521 的回复:]
我初学ajax+java,不知道环境怎么搭建,我的机器上已经安装了tomcat了,我将怎样布置上面两个文件,才能在ie中运行那个html页面?等、
[/Quote]

看看下面两个链接

http://www.baidu.com.cn/s?wd=java+%BB%B7%BE%B3%B4%EE%BD%A8&cl=3

http://www.google.cn/search?hl=zh-CN&q=java+%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA&btnG=Google+%E6%90%9C%E7%B4%A2&meta=&aq=f
  • 打赏
  • 举报
回复
please ask google
再出发666 2008-03-18
  • 打赏
  • 举报
回复
我初学ajax+java,不知道环境怎么搭建,我的机器上已经安装了tomcat了,我将怎样布置上面两个文件,才能在ie中运行那个html页面?等、
再出发666 2008-03-18
  • 打赏
  • 举报
回复
我搭建好了,正常运行,第一次接触servlet,谢谢大家,给分!
骑蚊子旅游 2008-03-17
  • 打赏
  • 举报
回复
楼主学的是asp还是.net还是jsp?

若源码的后缀是.java的话,应该属jsp的网页程序,这方面我不是很熟,至少可以确定的是,你要让你的代码测试通过,你就得先让你的IIS或电脑支持.java的编译运行
myvicy 2008-03-17
  • 打赏
  • 举报
回复
你的java能直接在ie里访问到吗?
等能直接访问到了再用ajax去试试,否则无论如何都是不行的。

52,797

社区成员

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

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