[g(sh)ڣףеđ(yng)cF(xin)]web2.0g(sh)ĵ͑(yng)
l(f)rg:2020-02-16 Դ: ӛȫ c
ժҪҪ̽ӑAjaxg(sh)Webеđ(yng)ԭ^y(tng)web(yng)õă(yu)ݵȆ}efAjaxÑ䛹ܵČF(xin) P(gun)I~AjaxJavaScript
ЈD̖TP311 īIRaA
1
AjaxһN͵ľW(wng)퓽g(sh)Sڿ͑˺ͷ(w)֮gֻݔϢÿcҪdĵȴĶÑõ푑(yng)rķ
ڂy(tng)ľW(wng)վg[ؓ؟(z)(w)ύÑՈ̎(w)Ո푑(yng)@ʹÑύһcc(sh)(j)ҪdW(wng)W(wng)퓵lˢ²HʹÑȴ푑(yng)ĕrg׃LҸ(do)Ñ`
AjaxģgӁQ@һ}Ajax棨Ajax Engine̎͑˺ͷ(w)֮gͨšAjaxʹJavaScript(sh)F(xin)ֻЮ(dng)Ҫ̎͑c(w)gͨŵĕr{(dio)cy(tng)ģ͵ͬAjaxģ͵̎^ǮģҪȵ푑(yng)_^m(x)(zh)кm(x)Ĵa
2 ^y(tng)web(yng)õă(yu)
2.1 Ajaxg(sh)Dʾ
2.2 Ajaxļg(sh)(yu)
1ͨӍîģʽʹW(wng)퓈(zh)еЧʴ͑(w)˰l(f)˲ԃ̈(zh)вԃZһlaoͣȴ(w)˵ķY(ji)
2(yu)˞g[ͷ(w)gĂݔpٲҪĔ(sh)(j)dsup[1]/supy(tng)web(yng)õĻ^ǣÑύΕrWeb(w)l(f)һՈ(w)˽ղ̎ՈȻһµľW(wng)@ӵ^M˺ܶrgǰɂW(wng)퓴aͬġÿν^̶Ҫ(w)˰l(f)ՈW(wng)퓵푑(yng)ٶȾهڷ(w)푑(yng)ٶȣ@͌(do)ÑľW(wng)퓽푑(yng)ȱسĽ푑(yng)Ҫö
3Ajaxڿ͑\Г(dn)һԭɷ(w)̎ĹĶpٷ(w)˵Ĕ(sh)(j)ؓdMһW(wng)j(lu)ЧAjax(yng)óH(w)˰l(f)Ͳ@ȡĔ(sh)(j)Ķʹ(w)͞g[gQĔ(sh)(j)pÑܿ푑(yng)đ(yng)^̡һںܶ̎ڿ͑ɣҲs˷(w)̎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 (w)˴a
ÑTW(wng)վ^ǣ͑˴a(w)˰l(f)һՈՈЂfÑϢ(w)˫@ȡ@һՈҪ_ϢǷ___ʼ(chung)Ϣ_ͽo͑l(f)سeϢ
JavaZԞservletbc(w)˵Ľ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
}
(w)ˏՈȡÑܴaȻL(sh)(j)쌤ƥӛҵƥÑϢӛ䛣һuserČȻ䱣ڕԒ(dng)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)͵(w)Ҫ̎푑(yng)JavaScriptZеEval ɌfoַJavaScripta(zh)ַаһ׃̈́(chung)@׃Eval ݔ?yn)?sh)һ(sh){(dio)͈(zh)@(sh)ҪעǣmȻEval ʮ֏ҪIJ^ڏ(f)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)ύ(w)ͨ^{(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)Ñܴa(w)oύ@ӾܼӿÑg[ٶڱУһⲿJavaScriptļnet.js
script type=" text/javascript " src=" net.js " /script
net.jsEĴa֪ͨÑՈڱ̎Ȼռl(f)͵ϢՈϢl(f)͵(w)һú(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)؏(f)coϵy(tng)e`
W(wng)ύÑܴaֵֶһַȻͨ^ContentLoaderύo(w)(dng)(w)ص䛳ɹϢr{(dio)createscript (sh)ؓ؟(z)̎ɷ(w)淵صĔ(sh)(j)
function Createscript {
strText=this . req . responseText
eval strText
}
responseTextз˰JavaScriptZַȻʹeval (sh)(zh)ַַпLoginFiltera(chn)ijeϢҲSÑ䛕rSelectServletصĴڄ(chung)a
䛳ɹՈϢ͕ύo棬HTML(chung)һ
4 Y(ji)Փ
Ajaxg(sh)ľW(wng)վÑLrwg[CYԴһӵ형Уp؏(f)dW(wng)YԴĵȴrgʹW(wng)퓾иõҕXЧҪעʹAjaxg(sh)ǰһҪҎ(gu)ӋЩҪõAjaxtoW(wng)վĺ;So韩һJavaScriptZԵďܺZȱھ{(dio)ԇrһҪ]Ҏ(gu)ܝڵĈ(zh)e`Ͳ֡
īI
[1]Ryan Asleson Nathaniel T.Schutta. Foundation of Ajax [M]. ]늳 2006.
P(gun)~g(sh) Ajax Web g(sh)ڣף⡡еđ(yng)cF(xin) web2 0g(sh)ĵ͑(yng) web2 0rĵ͑(yng)
cx