欢迎光临极品网,更多、更新的资源信息尽在Jpinw.Com!本站所有信息资源每日更新新的内容,请大家继续关注www.Jpinw.com!如果觉得本站还不错,对您有帮助,别忘了向您的朋友推荐本站!请记好本站网址:http://www.Jpinw.com,网站发展靠大家多多的支持!!!

极品网 极品课件 极品论文 极品文学 极品游戏 极品美容 极品手机资源 极品股票

#
免费资源: 免费域名 | 免费空间 | 免费推广 | 免费邮箱 | 免费硬盘 | 免费论坛 | 免费留言 | 免费统计 | 在线投稿 | 更多...
电脑学院: 操作系统 | 安全相关 | 网页设计 | 编程开发 | 建站经验 | 服务器类 | 黑客攻防 | 菜鸟入门 | 教你网赚 | 更多...
文章导航: 网赚学堂 | 网赚秘笈 | 网赚三维 | 网赚先锋 | 网赚资讯 | 感悟网赚 | 众生百态 | 经典美文 | 范文中心 | 更多...
图酷天下: 时事图酷 | 娱乐图酷 | 搞笑图酷 | 时尚图酷 | 体育图酷 | 另类经典 | 论文资源 | 课件下载 | 文学知识 | 更多...

您现在的位置: 极品网 >> 电脑学院 >> 编程开发 >> 编程综合 >> 教程正文

AJAX实践之与服务器通信(1)            【字体:
AJAX实践之与服务器通信(1)
作者:佚名    教程来源:不详    点击数:    更新时间:2008-1-14    

 








AJAX实践之与服务器通信(1)。

首先看下看下相对简单些的——向服务器发送一个包含有名/值对的简单查询串,在这种情况下XHP即可以用GET也可以用POST。

GET

function doRequestUsingGET() {

 createXMLHttpRequest();

 var queryString = " GetAndPostExample? " ;

 queryString = queryString + createQueryString()+

" &timeStamp= " + new Date().getTime();

 xmlHttp.onreadystatechange = handleStateChange;

 xmlHttp.open( " GET " , queryString, true );

 xmlHttp.send( null );

}

POST

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);

}

queryString就是名/值对的参数形式了(如name=LiLin&age=23),在调用OPEN方法中,当请求方法是用POST的时候为了确保服务器知道请求体中有请求参数,需要调用setRequestHeader,将Content-Type值设置为application/x-www-form-urlencoded.当然也可不放在请求体中(那就不要用POST啦!)

此时server处理:

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 " );

 }

}  

对get and post方法都用processRequest来处理。

要向服务器发送相关复杂的查询串,可以将模型变化为XML发送到server 。

client端:

function createXML() {

 var xml = " <pets> " ;

 var options = document.getElementById( " petTypes " ).childNodes;

 var option = null ;

 for ( var i = 0 ; i < options.length; i ++ ) {

option = options[i];

if (option.selected) {

 xml = xml + " <type> " + option.value + " <\/type> " ;

}

 }

 xml = xml + " <\/pets> " ;

 return xml;

}

function sendPetTypes() {

 createXMLHttpRequest();

 var xml = createXML();

 var url = " PostingXMLExample?timeStamp= " + new Date().getTime();

 xmlHttp.open( " POST " , url, true );

 xmlHttp.onread

[1] [2] 下一篇

 

教程录入:admin    责任编辑:admin 
  • 上一篇教程:

  • 下一篇教程:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关教程
    Java,javascript,JScrip
    javascript和Java区别
    Java技巧(一):会变色的超
    javascript和Java的区别
    javascript调用JAVA
    javascript 和 Java 的区
    JavaServerPages白皮书
    JavaApplet实例:水波
    javascript与JavaApplet
    javascript与Java在浏览
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    极品网
    | 设为首页 | 加入收藏 | 友情链接 | 版权声明 |
    极品网

    Copyright 2006 Jpinw.com 极品网

    备案号:浙ICP备07010375号

    极品网