转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
Ajax using XMLHttpRequest and Struts         ★★★★

Ajax using XMLHttpRequest and Struts

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3163 更新时间:2009/4/23 11:30:49
 

Ajax using XMLHttpRequest and Struts

Frank W. Zammetti

 

About five years ago I worked on a web app project where one of the primary requirements was that it should look, work and feel like a Windows-based fat client.  Now, ignoring the question of why it was not in fact done as a Windows-based fat client in the first place, that can be a rather tough requirement in the world of web development, certainly it was five years ago when not very many examples of such a thing existed.

 

As a result of a number of proof-of-concepts, I stumbled upon some techniques for doing things that were at the time rather atypical approaches to web development.  The end result was an application that, to this day, many people cannot even tell is web-based, aside from the fact that you access it with a browser!

 

Little did I know that only a few years later the basic concepts behind what I had done would re-emerge in the world as something called AjaxAjax is a term coined by the folks at Adaptive Path and is shorthand for Asynchronous Javascript + XML. 

 

This just goes to show, patenting every idea you ever have is indeed a good way to riches!  If only I would have thought what I did was anything special!  But I digress…

 

Google is doing it.  So are many others.  But what is it?  In a nutshell, the Ajax concept, which is not a concrete technology implementation but rather a way of thinking and a set of techniques that go along with the mindset, is concerned with the idea that rebuilding a web page for every user interaction is inefficient and should be avoided.

 

For instance, say you have a web page with two <select> elements on it.  You want the contents of the second one to change when a selection is made in the first.  Something like this is not uncommon and there are a number of ways you can handle it.

 

The Ajax approach to this is to only redraw a small portion of the web page, namely the contents of the second <select>.

 

The Ajax concept is based around something called the XMLHttpRequest component.  All you Microsoft haters get ready to yell because this was a Microsoft creation!  Yes, Microsoft got something right, and did so before anyone else!  Microsoft first implemented the XMLHttpRequest object in Internet Explorer 5 for Windows as an ActiveX object (ok, so they didn’t get it QUITE right!). The Mozilla project later implemented a native version with the release of Mozilla 1.0, and by extension, Netscape 7. Apple has now done the same as of Safari 1.2.  Opera is now following suite with their version 7.60.  All released versions of Firefox contain it as well.

 

Let’s just cut to the chase and get to some code, shall we?

 

The XMLHttpRequest component, being a client-side component, must be instantiated via scripting before it can be user.  Thankfully, the process is as simple as can be… For IE, use the following Javascript:

 

var req = new ActiveXObject("Microsoft.XMLHTTP");

 

…for any other browser, use:

 

var req = new XMLHttpRequest();

 

You will of course want to perform some branching logic in your code.  There are a couple of ways to do it, but I prefer a simple approach, which to me is just an object existence check:

 

var req;

if (window.XMLHttpRequest) { // Non-IE browsers

  req = new XMLHttpRequest();

} else if (window.ActiveXObject) { // IE

  req = new ActiveXObject("Microsoft.XMLHTTP");

}

 

However you choose to do it, after this code executes you will find that the variable req is now a reference to an XMLHttpRequest object.  This object has a number of properties and methods, which are summarized below:

 

Property                                  Description

onreadystatechange                  Event handler for an event that fires at every state change

readyState                                Status:

0 = uninitialized

1 = loading

2 = loaded

3 = interactive

4 = complete

responseText                            Data returned from server in string form

responseXML                          DOM-compatible document object of data returned

status                                        HTTP status code (i.e., 200, 404, 500, etc.)

statusText                                 String message associated with the status code

 

Method                                   Description

abort()                                      Stops the current request

getAllResponseHeaders()         Returns all headers (name and value) as a string

getResponseHeader(                Returns the value of the specified header

"<headerName>")

open("<method>", "URL"[,       Opens a connection and retrieves response from the specified URL.

asyncFlag[, "<username>"[,      Can also specify optional values method (GET/POST), username and

"<password>"]]])                     password for secured sites

 send(content)                           Transmits request (can include postable string or DOM object data)

setRequestHeader                    Assigns values to the specifed header

("<name>", "<value>")

 

Before we go any further I highly suggest checking out the webapp referenced at the end of this article.  If you haven’t already done so, download the sample webapp at the URL referenced at the end of this article.  Install it into your servlet container of choice.  It is supplied in exploded format, so just copying the directory crea

[1] [2] [3] [4]  下一页


[Web开发]AJAX来判断dedecms用户是否登录  [系统软件]Explanation of UFT-8 and Unicode
[系统软件]Using dllimport and dllexport in C++ Classes  [常用软件]神奇 我家的照片会唱歌 照片会唱歌
[VB.NET程序]VB.NET and C# 语法比较手册  [Delphi程序]IntraWeb And ActiveForms
[Delphi程序]different between BPL and DLL  [VB.NET程序]How to setup a basic Struts project using Ecli…
[网页制作]Classes and Objects in PHP5  [网页制作]新手解读:认识XML,AJAX,SNS,Tag
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台