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푑(yng)ȱسĽ푑(yng)Ҫö
3Ajaxڿ͑\(yn)УГ(dn)һԭɷ(w)̎ĹĶpٷ(w)˵Ĕ(sh)(j)ؓ(f)dM(jn)һW(wng)j(lu)ЧAjax(yng)óH(w)˰l(f)Ͳ@ȡĔ(sh)(j)Ķʹ(w)͞g[gQĔ(sh)(j)pÑܿ푑(yng)đ(yng)^̡һںܶ̎ڿ͑ɣҲs˷(w)̎r(sh)g
3 Ajax(sh)F(xin)Ñ䛹
3.1 ÑϢ
惦(ch)ÑϢĔ(sh)(j)usersа3(g)քe̖(ho)xuhaoÑxingmingܴamima鄓(chung)˱SQLZ䣺
create table users xuhao int primary key unique not null xingming varchar50 not null mima varchar50 not null
3.2 (w)˴a
ÑTW(wng)վ^ǣ͑˴a(w)˰l(f)һ(g)Ո(qng)Ո(qng)ЂfÑϢ(w)˫@ȡ@һՈ(qng)Ҫ_ϢǷ___ʼ(chung)퓣Ϣ_ͽo͑l(f)سe(cu)Ϣ
JavaZԞservletbc(w)˵Ľaһ(g)^VzUser(du)Ƿ(jng)ڕ(hu)Ԓǣͽt(j)Ո(qng)ṩÑܴa(yn)CՈ(qng)͕(hu)^m(x)fservletУȻͷһle(cu)Ϣ´ab˳e(cu)ϢJavaScriptZ䣺
public static String getLoginError {
StringBuffer jsBuf=new StringBuffer
. append" document.getElementByld "spanProcessing" \\n "
. append " . innerHTML="
. append " "ܴa_" \\n "
return jsBuf . tostring
}
(w)ˏՈ(qng)ȡÑܴaȻL(sh)(j)쌤ƥӛҵƥÑϢӛ(hu)һ(g)user(du)Č(sh)Ȼ䱣ڕ(hu)Ԓ(dng)m(x)Ո(qng)r(sh)ͲҪٰl(f)Ñܴaˡ˷ă(yu)c(din)ʹÑdz(zh)עNֻ茢User (sh)ĕ(hu)ԒЄhUser (du)Ķxa
//User.java
public class User {
private int xuhao=-1
private String xingming=null
public User int xuhao String xingming {
Super
this.xuhao=xuhao
this.xingming=xingming
}
public int getXuhao { return xuhao }
public String getXingming return xingming }
}
3.3 ͑˵
͑˵ɃɲֽMһDŽ(dng)B(ti)W(wng)퓵HTMLaһAjaxJavaScriptaÁՈ(qng)l(f)͵(w)Ҫ̎푑(yng)JavaScriptZеEval ɌfoַJavaScripta(zh)ַаһ(g)׃̈́(chung)@(g)׃Eval ݔ?yn)?sh)һ(g)(sh){(dio)ã͈(zh)@(g)(sh)ҪעmȻEval ʮ֏(qing)ҪIJ^ڏ(f)sԓܿܕ(hu)ܵӰW(wng)퓵HTML a
form name=" Form1 "
div id =" header "
span id =" login "
input type=" text " name =" xingming "
brܴa
input type =" mima " name=" mima "
br
span id =" spanProcessing " /span
input type=" submit " name=" btnsub " value =" " onclick=" loginreqrest "
/span
span id =" sloganText " ʾϢ/span
/div
div id =" defaultContent "
pW(wng)е(ni)ݣ/p
/div
div id =" divSettings " class=" hidden " /div
/form
aеonclick ¼Á팢(sh)(j)ύ(w)ˣͨ^{(dio)loginreqrest (sh)(dng)Ajax Ո(qng)棬ھW(wng)ʹһ(g)CSSҎ(gu)tдaęnhead (bio)һ(g)(ni)Ƕstyle(bio)Á׃ɫwСλá߅Șʽ
style type=" text/css "
html.body {margin 0px padding 0px height 100% }
#header {background-color#c0c0c0height100pxborder-bottom1px solid black font-weightbold}
#login { text- align right float right margin-top 15px margin-right 15px }
#sloganText { font-size 25px margin-left 15px line-height 100px }
/style
3.4 䛴a
Ajaxă(yu)(sh)ڣھW(wng)c(din)r(sh)JavaScriptύaֻl(f)Ñܴa(w)oύ(g)棬@ӾܼӿÑg[ٶڱУһ(g)ⲿJavaScriptļnet.js
script type=" text/javascript " src=" net.js " /script
net.js(g)EĴa֪ͨÑՈ(qng)ڱ̎Ȼռl(f)͵ϢՈ(qng)Ϣl(f)͵(w)һú(sh)LoginRequest(sh)F(xin)a£
function LoginRequest {
document. getElementByld " spanProcessing " . innerHTML=" Verifying Credentials "
var url =" portalLogin. servlet "
var strName=document.Forml.xingming.value
var strPass=document. Forml. mima. value
var strParams=" user=" +strName+ " &pass=" +strPass
var loaderl=new net.ContentLoaderurl Createscript null " POST " strParams
}
ڰl(f)Ϣ֮ǰ(hu)@ʾһlϢVÑc(din)䛡oIJ̎С@ӿԷֹÑ`Ԟʲô]l(f)؏(f)c(din)oϵy(tng)e(cu)`
W(wng)ύÑܴaֵֶ(hu)һ(g)ַȻͨ^ContentLoader(du)ύo(w)(dng)(w)ص䛳ɹϢr(sh)(hu){(dio)createscript (sh)ؓ(f)؟(z)̎ɷ(w)淵صĔ(sh)(j)
function Createscript {
strText=this . req . responseText
eval strText
}
responseTextз˰JavaScriptZַȻʹeval (sh)(zh)ַַпLoginFiltera(chn)ije(cu)ϢҲSÑ䛕r(sh)SelectServletصĴڄ(chung)a
䛳ɹՈ(qng)Ϣ͕(hu)ύoHTML(chung)һ(g)
4 Y(ji)Փ
Ajaxg(sh)ľW(wng)վÑLr(sh)w(yn)g[C(j)YԴһӵ형p؏(f)dW(wng)YԴĵȴr(sh)gʹW(wng)퓾иõҕXЧҪעʹAjaxg(sh)ǰһҪҎ(gu)Ӌ(j)ЩҪõAjaxt(hu)oW(wng)վĺ;So(h)韩һJavaScriptZԵď(qing)ܺZȱھ{(dio)ԇr(sh)һҪ]Ҏ(gu)ܝڵĈ(zh)e(cu)`Ͳ
īI(xin)
[1]Ryan Asleson Nathaniel T.Schutta. Foundation of Ajax [M]. ]늳 2006.
P(gun)~g(sh) Ajax Web g(sh)ڣףеđ(yng)c(sh)F(xin) web2 0g(sh)ĵ͑(yng) web2 0r(sh)ĵ͑(yng)
c(din)x