飘灵儿 发表于 2015-1-16 22:27:42

ASP教程之一个投票体系的源程序(coveryourasp.co...

强大的可扩展性。ASP具有强大的扩展性,可以实现与多种网络、硬件设备的连接:通过专用的通讯线路远程接入企业;通过远程拨号服务器为远程拨号客户提供服务;通过WAP为移动电话互联网客户服务。SurveyDescr.asp
<!--#includefile="include/Startup.asp"-->
<!--#includefile="utils/Survey.asp"-->

<%
//============================================
//NOTE:allsourcecodedownloadedfromCoverYourASPwaswrittenby
//JamesShaw(unlessstatedotherwise),andiscopyright(c)2000by
//JamesShaw.Youmayusethissourcecodeonyourwebsites,but
//pleasedontpublishordistributeinanyway.
//
//IwouldappreciateanHTMLcommentinanycodeyouuse,i.e.
//<!--portions(c)james@CoverYourASP.com-->
//(seeFooter(),documentedinSSI.aspfordetailsonhowtodothis)
//
//<shamelessplug>
//PleasecontactmetodiscussanyASPcontractworkyoumayhave.
//</shamelessplug>
//============================================

//outputrelevantmetatags
Init("Surveyyourreaders");

//outputcommontopofpage
Header(Surveyyourreaders);

//outputpagecontent
Content();

//outputcommonbottomofpage
Footer();

//============================================
//thecontentofthispage-everypagehasafunctionContentthat
//iscalledabove.
//============================================
functionContent()
{
Out(<tdcolspan=2width="80%"valign="top">);

//ifthesurveyhasntbeensubmittedyet...
if(!Request.Form.Count)
{
//...displaysomeblah,blah
Out(Finally,surveyscometoCoverYourASP!Ivebeenwantingtoaskyouguysandgals
questionsforalongtime,andnowIcan.Itsuptoyouifyouwanttoanswerofcourse!);

Out(<p>Ofcourse,therealbenefittoyouisthatifyoutellmewhatyoulikeIllprobably
provideit.Ifyousendinyour<ahref="Donate.asp">donations</a>theprobabilityincreasesrather
dramatically!);

Out(<p>Taketheexamplesurveybelowifyouhavethetimeandinclination.Iplantopostmore
inaspecialsurveycategory,andstartofferingincentivestotakethem.);

Out(<p>Afterwards,lookatthecode.Ithinkyoullbesurprisedhowsimpleitistocreate
surveyswiththiscode.Thispagehasonefunctioncallinit,withjustoneparameter-thenameofthe
survey!Allquestions,answersandresultsarestoredinthedatabase.);
}

//showthesurvey,orprocessitsinput
ProcessSurvey(Whoareyouandwhatdoyouthink?);

if(!Request.Form.Count)
Out(<p><ahref="ShowSource.asp?page=SurveyDescr"></a></center>);

Out(</td>);
Out(<tdwidth="20%"valign="top">);

//showrotatingbanners
ShowBanners(4);

Out(</td>);
}
%>


utils/Survey.asp
<%
//============================================
//NOTE:allsourcecodedownloadedfromCoverYourASPwaswrittenby
//JamesShaw(unlessstatedotherwise),andiscopyright(c)2000by
//JamesShaw.Youmayusethissourcecodeonyourwebsites,but
//pleasedontpublishordistributeinanyway.
//
//IwouldappreciateanHTMLcommentinanycodeyouuse,i.e.
//<!--portions(c)james@CoverYourASP.com-->
//(seeFooter(),documentedinSSI.aspfordetailsonhowtodothis)
//
//<shamelessplug>
//PleasecontactmetodiscussanyASPcontractworkyoumayhave.
//</shamelessplug>
//============================================

//============================================
//displayorprocessthenamedsurvey
//============================================
functionProcessSurvey(sName)
{
//hasthesurveyformbeensubmitted?
if(Request.Form.Count)
{
//connecttothedatabase
DBInitConnection();

varsSurvey=""+Request.Form("Survey");

//onlyupdatethesurveywhennocookie
if(""==Request.Cookies(sSurvey))
{
//getthedatafromtheformandupdatethedatabase
//useanenumeratortogetnameandvalue
vare=newEnumerator(Request.Form);

while(!e.atEnd())
{
varoItem=e.item();

//incrementthecurrentnumberoftimesthisanswerhasbeenchosen
oConnection.Execute(UPDATESurveyAnswersSETHits=Hits+1WHEREQuestion="+oItem+"AND
Answer="+Request.Form(oItem)+";);

e.moveNext();
}

//notethatsettingcookiehereassumeswearebuffering
//theReponse.Writes-cookiesmustbesetbeforeany
//HTMLissenttotheclient
Response.Cookies(sSurvey)="1";

//Imnotsettingtheexpiresonthecookie,soitllgo
//awaywhenthebrowserisclosed.Ijustwantedtostop
//thesurveyincrementingifthepagerefreshed.
}

//nowdisplayalltheanswerstothesurvey
Out(<p>Thanksfortakingpartinour"+sSurvey+"survey!Theanswersthateveryonehas
givensofarareshownbelow:);

//thelastquestionwedisplayed
varsLast="";

//getalltheselectedanswers,sortedbyquestionandhits
DBGetRecords(SELECTSurveyAnswers.Question,Answer,HitsFROMSurveyAnswersINNERJOIN
SurveyQuestionsONSurveyQuestions.Question=SurveyAnswers.QuestionWHERESurvey="+sSurvey+"AND
Hits>0ORDERBYSurveyAnswers.Question,HitsDESC;);

varfScale;

while(!oRecordSet.EOF)
{
//displayquestionwhenitchanges
varsIntQuestion=""+oRecordSet(0);

//sliceoffcharsusedforsorting
varsQuestion=sIntQuestion.slice(2);

//getanswer
varsIntAnswer=""+oRecordSet(1);

//sliceoffcharsusedforsorting
varsAnswer=ExpandMacros(sIntAnswer.slice(2));

varnReaders=oRecordSet(2)-0;

if(sQuestion!=sLast)
{
Out(<h5>+sQuestion+</h5>);
sLast=sQuestion;

Out(<fontcolor="red">"+sAnswer+"wasthetopanswer(+nReaders+readers)
</font><br>);

fScale=300.0/nReaders;
}
else
{
Out("+sAnswer+"waschosenby+nReaders+readers<br>);
}

Out(<imgsrc="http://edu.cnzz.cn/NewsInfo/images/Dot.gif"height="8"width="+(nReaders*fScale)+"><br>);

oRecordSet.MoveNext();
}

//releasetheconnectionASAP
DBReleaseConnection();
}
else
{
//someinitialinstructions
Out(<p>Therearen        anyimportantinstructionswhenansweringthesequestions-exceptyou
don        havetoanswerany.Allareoptional-ifyoudon        likeaquestion,ornoneoftheanswersare
relevant,justmoveontothenextone!);

//connecttothedatabase
DBInitConnection();

//getthequestionsfromthedatabase
DBGetRecords(SELECTQuestionFROMSurveyQuestionsWHERESurvey="+sName+"ORDERBY
Question;);

if(oRecordSet.EOF)
{
Out(Noquestionswerefoundforsurvey"+sName+"<p>);
return;
}

//storethequestionsinanarray
varsIntQuestions=newArray;
varnQuestions=0;

while(!oRecordSet.EOF)
{
sIntQuestions=""+oRecordSet(0);
oRecordSet.MoveNext();
}

Out(<formaction="+Request.ServerVariables(SCRIPT_NAME)+"method="post">);

//somehiddenfieldstopassdatathroughtoresultspage
Out(<inputtype="hidden"name="Survey"value="+sName+">);

//nowloopthroughthequestions
for(varnQuestion=0;nQuestion<nQuestions;nQuestion++)
{
varsIntQuestion=sIntQuestions;

//sliceoffcharsusedforsorting
varsQuestion=sIntQuestion.slice(2);

//gettheanswersfromthedatabase
DBGetRecords(SELECTAnswer,AnswerTypeFROMSurveyAnswersWHEREQuestion="+sIntQuestion+"
ORDERBYAnswer;);

Out(<h5>+sQuestion+</h5>);

while(!oRecordSet.EOF)
{
//gettheanswer
varsIntAnswer=""+oRecordSet(0);

//sliceoffcharsusedforsorting
varsAnswer=ExpandMacros(sIntAnswer.slice(2));

varsAnswerType=""+oRecordSet(1);

switch(sAnswerType)
{
caseradio:
Out(<inputtype="radio"name="+sIntQuestion+"value="+sIntAnswer+">+
sAnswer);
break;

default:
break;
}

Out(<br>);

//getnextanswer
oRecordSet.MoveNext();
}
}

Out(<p><inputtype="submit"value="Submitanswers">);
Out(</form>);

//releasetheconnectionASAP
DBReleaseConnection();
}
}

//============================================
//addlinkstotextwhere*?macrosarefound,e.g.*dexpandsto
//<ahref="Donate.asp">Sendadonation!</a>
//NOTE:currentlyassumesexpansionsarealwaysatendofline
//============================================
functionExpandMacros(sText)
{
varsMacros=newArray(
**,
*d
);

varsExpansions=newArray(
<ahref="Contact.asp"target="CYAEXternal">Youneedtosendmefeedback!</a>,
<ahref="Donate.asp"target="CYAEXternal">sendadonation!</a>
);

for(vari=0;i<sMacros.length;i++)
{
varnPos=sText.indexOf(sMacros);

if(-1!=nPos)
{
sText=sText.slice(0,nPos)+sExpansions;
break;
}
}

returnsText;
}
%>


utils/Database.asp
<%
//============================================
//NOTE:allsourcecodedownloadedfromCoverYourASPwaswrittenby
//JamesShaw(unlessstatedotherwise),andiscopyright(c)2000by
//JamesShaw.Youmayusethissourcecodeonyourwebsites,but
//pleasedontpublishordistributeinanyway.
//
//IwouldappreciateanHTMLcommentinanycodeyouuse,i.e.
//<!--portions(c)james@CoverYourASP.com-->
//(seeFooter(),documentedinSSI.aspfordetailsonhowtodothis)
//
//<shamelessplug>
//PleasecontactmetodiscussanyASPcontractworkyoumayhave.
//</shamelessplug>
//============================================

//globals
varoConnection;
varoRecordSet;

//enums

//Connection.StateandRecordset.Stateproperty
varadStateClosed=0;//theobjectisclosed.
varadStateOpen=1;//theobjectisopen.
varadStateConnecting=2;//theobjectisconnecting.
varadStateExecuting=4;//theobjectisexecutingacommand.
varadStateFetching=8;//therowsoftheobjectarebeingfetched.

//Recordset.Cursorproperty
varadOpenUnspecified=-1;//doesnotspecifythetypeofcursor.
varadOpenForwardOnly=0;//(default)aforward-onlycursor,i.e.yougetonlyonepassthruthedata!
varadOpenKeyset=1;//cangoinanydirection,andasabonusyoullseechangesotherusers
make.EXPENSIVE!
varadOpenDynamic=2;//asKeyset,butalsoyoucanseeadditions/deletionsotherusersmake.
EXPENSIVE!
varadOpenStatic=3;//cangoinanydirection,butread-only.

//Recordset.LockTypeproperty
varadLockUnspecified=-1;//doesnotspecifyatypeoflock.
varadLockReadOnly=1;//(default)guess!
varadLockPessimistic=2;//guaranteedtowork
varadLockOptimistic=3;//recordslockedonlywhenyoucallUpdate.fingerscrossed
varadLockBatchOptimistic=4;//requiredforbatchupdatemode

//============================================
//exampleusage:
//DBInitConnection();
//
//DBGetRecords("SELECT*FROMSomewhere");
//
//...useoRecordSet
//
//DBReleaseRecords();//optionalstep
//
//DBGetRecords("SELECT*FROMSomewhereElse");
//
//...useoRecordSet
//
//DBReleaseRecords();//optionalstep
//
//DBReleaseConnection();
//============================================

//============================================
//initializesdatabasevariablesforfirstuseonpage-leaveittothe
//lastpossiblesecondbeforecallingthisfunction
//============================================
functionDBInitConnection()
{
//dontopenitagainifalreadyopened!
if(oConnection!=undefined)
return;

//youcanopenRecordsetobjectswithoutaConnectionobject,but
//itsfarlessefficientifyouareopeningmultipleRecordsets.
//
//ifyoudontcreateaConnectionobjectADOcreatesanewonefor
//eachnewRecordset.Open,evenifyouusethesameconnectionstring.
oConnection=Server.CreateObject(ADODB.Connection);

//openthedatabase-useMapPathtomakerelativepathintophysicalpath
//NOTE:keepyourdatabasepathasecret-nastypeopleareeverywhere!
//2.changethe4.0to3.51whenusingAccess97
oConnection.Open(Provider=Microsoft.Jet.+sDBDriver+;DataSource=+Server.MapPath(
sDBPath));

//createaRecordset
oRecordSet=Server.CreateObject(ADODB.Recordset);
}

//============================================
//tidiesupafterDBInitConnection
//============================================
functionDBReleaseConnection()
{
//dontreleasetheconnectionifnotconnected!
if(oConnection==undefined)
return;

//closeanddeletetheRecordsetobject
DBReleaseRecords();

oRecordSet=undefined;

//DontcallCloseiftheRecordsetfailedtoOpenproperly,i.e.its
//StateisstilladStateClosed(0)
if(oConnection.State!=adStateClosed)
oConnection.Close();

oConnection=undefined;
}

//============================================
//executesthepassedinSQLstatementandreturnsaread-only
//forward-onlyoRecordSetobject
//============================================
functionDBGetRecords(sSQL)
{
//iftheRecordsetisalreadyopen,closeit
DBReleaseRecords();

//wecoulduseoRecordSet=oConnection.Execute(sSQL)here
//butthenwewillalwaysgetbackaread-only,forward-onlycursor.
//(admittedlythisisthemostusedtype,butstill)

//useoRecordSet.Openandwehavefarmorecontrol.Fordetails
//readthedefinitionsoftheenumsatthetopofthisfile.

//rememberthatthiscanfailifpassedgarbage,andhencethe
//Recordsetwillremainclosed,State==adStateClosed
oRecordSet.Open(sSQL,oConnection,adOpenForwardOnly,adLockReadOnly);
}

//============================================
//tidiesupafterDBGetRecords
//============================================
functionDBReleaseRecords()
{
//whenyouhavefinishedwithanopenRecordsetobject,callthe
//Closemethodtoreleaseitsresources.YoucancallOpenagain.

//DontcallCloseiftheRecordsetfailedtoOpenproperly,i.e.its
//StateisstilladStateClosed
if(oRecordSet!=undefined&&oRecordSet.State!=adStateClosed)
oRecordSet.Close();
}
%>

对于中小型web应用来说,php有很强的竞争力,linux+apache+mysql+php(lamp)的组合几乎可以胜任绝大多数网站的解决方案,对于大型应用来讲,对于系统架构要求更高,需要有成熟的框架支持,jsp的struts是个不错的框架,国内介绍它的资料也非常多,应用逐渐广泛起来。asp就不用说了,

蒙在股里 发表于 2015-1-19 13:25:36

哪些内置对象是可以跳过的,或者哪些属性和方法是用不到的?

若天明 发表于 2015-1-25 18:06:28

如何学好ASP,以前也有人问过,把回答给你转过来看看能否对你有帮助:

不帅 发表于 2015-2-3 12:34:51

没有坚实的理论做基础,那么我们连踏入社会第一步的资本都没有,特别对于计算机专业的学生学好专业知识是置关重要的。在这里我侧重讲一下如何学习ASP,从平时的学习过程中。

只想知道 发表于 2015-2-9 00:35:22

代码逻辑混乱,难于管理:由于ASP是脚本语言混合html编程,所以你很难看清代码的逻辑关系,并且随着程序的复杂性增加,使得代码的管理十分困难,甚至超出一个程序员所能达到的管理能力,从而造成出错或这样那样的问题。

第二个灵魂 发表于 2015-2-26 15:44:37

下载一个源代码,然后再下载一个VBScript帮助,在源代码中遇到不认识的函数或是其他什么程序,都可以查帮助进行解决,这样学习效率很高。

admin 发表于 2015-3-8 15:50:15

交流是必要的,不管是生活还是学习我们都要试着去交流,通过交流我们可以学到很多我们自己本身所没有的知识,可以分享别人的经验甚至经历。

爱飞 发表于 2015-3-16 03:49:01

作为IE上广为流传的动态网页开发技术,ASP以它简单易学博得了广大WEB程序爱好这的青睐,而且它对运行环境和开发品台的不挑剔,以及有大量有效的参考手册,极大的推广了它的发展。

冷月葬花魂 发表于 2015-3-22 19:55:57

以上是语言本身的弱点,在功能方面ASP同样存在问题,第一是功能太弱,一些底层操作只能通过组件来完成,在这点上是远远比不上PHP/JSP,其次就是缺乏完善的纠错/调试功能,这点上ASP/PHP/JSP差不多。
页: [1]
查看完整版本: ASP教程之一个投票体系的源程序(coveryourasp.co...