[gڣףеđcF(xin)]web2.0gĵ͑
l(f)rg:2020-02-16 Դ: ӛȫ c
ժҪҪ̽ӑAjaxgWebеđԭ^y(tng)webõă(yu)ݵȆ}efAjaxÑ䛹ܵČF(xin) PI~AjaxJavaScript
ЈD̖TP311 īIRaA
1
AjaxһN͵ľW(wng)퓽gSڿ͑˺ͷ֮gֻݔϢÿcҪdĵȴĶÑõ푑rķ
ڂy(tng)ľW(wng)վg[ؓ؟ύÑՈ̎Ո푑@ʹÑύһcc(sh)(j)ҪdW(wng)W(wng)퓵lˢ²HʹÑȴ푑ĕrg׃LҸÑ`
AjaxģgӁQ@һ}Ajax棨Ajax Engine̎͑˺ͷ֮gͨšAjaxʹJavaScript(sh)F(xin)ֻЮҪ̎͑cgͨŵĕr{(dio)cy(tng)ģ͵ͬAjaxģ͵̎^ǮҪȵ푑_^m(x)(zh)кm(x)Ĵa
2 ^y(tng)webõă(yu)
2.1 AjaxgDʾ
2.2 Ajaxļg(yu)
1ͨӍîģʽʹW(wng)퓈(zh)еЧʴ磬͑˰l(f)˲ԃ̈(zh)вԃZһlaoͣȴ˵ķY
2(yu)˞g[ͷgĂݔpٲҪĔ(sh)(j)dsup[1]/supy(tng)webõĻ^ÑύΕrWebl(f)һՈ˽ղ̎ՈȻһµľW(wng)@ӵ^M˺ܶrgǰɂW(wng)퓴aͬÿν^̶Ҫ˰l(f)ՈW(wng)퓵푑ٶȾهڷ푑ٶ@͌ÑľW(wng)퓽푑ȱسĽ푑Ҫö
3Ajaxڿ͑\УГһԭɷ̎ĹĶpٷ˵Ĕ(sh)(j)ؓdMһW(wng)jЧAjaxóH˰l(f)Ͳ@ȡĔ(sh)(j)Ķʹ͞g[gQĔ(sh)(j)p٣Ñܿ푑đ^һںܶ̎ڿ͑Ҳs˷̎rg
3 AjaxF(xin)Ñ䛹
3.1 ÑϢ
惦ÑϢĔ(sh)(j)usersа3ԣքe̖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 ˴a
ÑTW(wng)վ^ǣ͑˴a˰l(f)һՈՈЂfÑϢ˫@ȡ@һՈҪ_ϢǷ___ʼ(chung)Ϣ_ͽo͑l(f)سeϢ
JavaZԞservletbc˵Ľaһ^VzUserǷ(jng)ڕԒǣͽt(j)ՈṩÑܴaCՈ͕^m(x)fservletȻͷһleϢ´ab˳eϢJavaScriptZ䣺
public static String getLoginError {
StringBuffer jsBuf=new StringBuffer
. append" document.getElementByld "spanProcessing" \\n "
. append " . innerHTML="
. append " "ܴa_" \\n "
return jsBuf . tostring
}
ˏՈȡÑܴaȻL(sh)(j)쌤ƥӛҵƥÑϢӛ䛣һuserČȻ䱣ڕԒСm(x)ՈrͲҪٰl(f)Ñܴa˷ă(yu)cǣʹÑdz(zh)עNֻ茢User ĕԒЄhUser Ķ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ŽӑB(ti)W(wng)퓵HTMLaһAjaxJavaScriptaÁՈl(f)͵Ҫ̎푑JavaScriptZеEval ɌfoַJavaScripta(zh)Сַаһ׃̈́(chung)@׃Eval ݔ?yn)?sh)һ(sh){(dio)͈(zh)@(sh)ҪעmȻEval ʮ֏ҪIJ^ڏsԓܿܕܵӰ푡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)ύͨ^{(dio)loginreqrest (sh)Ajax ՈھW(wng)ʹһCSSҎ(gu)tдaęnhead ˺һ(ni)Ƕstyle˺Á׃ɫ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)ڣھW(wng)crJavaScriptύaֻl(f)Ñܴaoύ@ӾܼӿÑg[ٶڱһⲿJavaScriptļnet.js
script type=" text/javascript " src=" net.js " /script
net.jsEĴa֪ͨÑՈڱ̎Ȼռl(f)͵ϢՈϢl(f)͵һú(sh)LoginRequestF(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)Ϣ֮ǰ@ʾһlϢVÑc䛡oIJ̎@ӿԷֹÑ`Ԟʲô]l(f)؏͵coϵy(tng)e`
W(wng)ύÑܴaֵֶһַȻͨ^ContentLoaderύoص䛳ɹϢr{(dio)createscript (sh)ؓ؟̎ɷ淵صĔ(sh)(j)
function Createscript {
strText=this . req . responseText
eval strText
}
responseTextз˰JavaScriptZַȻʹeval (sh)(zh)ַַпLoginFiltera(chn)ijeϢҲSÑ䛕rSelectServletصĴڄ(chung)a
䛳ɹՈϢ͕ύoHTML(chung)һڡ
4 YՓ
AjaxgľW(wng)վÑLrwg[CYԴһӵ형p؏dW(wng)YԴĵȴrgʹW(wng)퓾иõҕXЧҪעʹAjaxgǰһҪҎ(gu)ӋЩҪõAjaxtoW(wng)վĺ;So韩һJavaScriptZԵďܺZȱݣھ{(dio)ԇrһҪ]Ҏ(gu)ܝڵĈ(zh)e`Ͳ
īI
[1]Ryan Asleson Nathaniel T.Schutta. Foundation of Ajax [M]. ]늳 2006.
P~g Ajax Web gڣףеđcF(xin) web2 0gĵ͑ web2 0rĵ͑
cx