ABOUT ACTIVE SERVER PAGE Microsoft active server pages (asp) is a server-side scripting environment that you can use to create and run dynamic, interactive web server applications. With asp, you can combine with html page, script commands, and activex components to create interactive web pages or powerful web based applications, asp applications are easy to develop and modify WHAT IS NEW IN ASP active server pages has been enhanced with features of mixing code languages. that make it easier to use while developing applications. ADDING SCRIPT COMMANDS script is a series of commands or instructions. unlike html tags, which simply format text or read in a graphic, video, or sound file, a script command instructs the web server to performs an action. !cript commands can store a user"s name in a variable, display the user"s name in a page returned to the browser, or store the user"s name in a database. !cript commands are differentiated from text by delimiters. delimiter is a character or se#uence of characters that marks the beginning or end of a unit. in the case of html, these delimiters are the less ($) and greater (%) symbol, which enclose html tags. sp uses the delimiters $& and &% to enclose script commands. you can include within the delimiters any command that is valid for the scripting language you are using. the following example shows a simple html page that contains a script command. CSC-AVINASHI AND CSC-GANAPATHY ' ASP NOTES MIXING HTML AND SCRIPTING TAG Asp has been in!"#$%e &ih '!ee (ea$!e "( )i*in' +an'$a'e s,se)- ,"$ %an in%+$#e h)+. /bs%!ip "! 0a/aS%!ip a's a+s" &ihin asp- E*a)p+e ("! )i*in' +an'$a'e s,se) has sh"&n be+"&- 1h)+2 1b"#, b'%"+"!3!e#2 14 i( i)e23567899899AM: i)e 1567899899PM 5hen 42 '""# )"!nin' 14 e+se 42 He++" 1;h)+2 RE<UEST OB0ECT Usin' he asp !e=$es "b>e% ,"$ %an %!eae si)p+e. ,e p"&e!($+ s%!ips ("! %"++e%in' an# p!"%essin' #aa 'ahe!e# &ih h)+ ("!)s- In his "pi% ,"$ &i++ n" "n+, +ea!n h"& " %!eae basi% ("!) in("!)ai"n. b"h "n ,"$! &eb se!/e! an# a he $se!?s b!"&se!- CSC-AVINASHI AND CSC-GANAPATHY ( ASP NOTES POST METHOD P"s is he )eh"# " s$b)i he #aas (!") he pa'e " pa'e-i( ,"$ &an " $se his )eh"# ,"$ )$s a## he s$b)i b$"n in ,"$! ("!)- GET METHOD Ge is he )eh"# " s$b)i he #aas (!") he pa'e " pa'e-his )eh"# a+&a,s sh"&s he #aa in ,"$! a##!ess ba!- EXAMPLE PROGRAM @OR RE<UEST OB0ECT 1h)+2 1("!) na)e35()6: )eh"#3p"s a%i"n35$se-asp:2 Ene! ,"$! na)e 81inp$ na)e35na):21b!21b!2 Ene! ,"$! A'e 81inp$ na)e35a':21b!2 1inp$ ,pe35s$b)i: /a+$e35han# "/e!:2 $)html% USE-ASP 14 #i) na.a' na3!e=$es-("!)A5na):B a'3!e=$es-("!)A5a':B !esp"nse-&!ie 5na)e is 8: C na C 51b!21b!25 !esp"nse-&!ie 5A'e Is 8: C a' 42 RE<UEST WITH PARAMETER CSC-AVINASHI AND CSC-GANAPATHY * ASP NOTES EXAMPLE $+,M-% $ +./01234/.!565.134/7%34/$)%$8.% $ +./01234/.!565.1,W37%,W3$)%$8.% $)+,M-% ONE-ASP $& 9:M 5. 5.1trim(./;</!,.;</.=!,.:4>(25.7)) &% $& :0 5.1234/7 ,+/4 &% $,8-/ W:9,+1?@& -:>41A/4,/. 83.9/.1'@% $,.% $,9%5.M/,/. 34/$),9% $),.% $),8-/% $&/49 :0&% $& :0 5.12,W37 ,+/4 &% $,8-/ W:9,+1?@& -:>41A/4,/. 83.9/.1'@% $,.% $,9%5.M/,/. ,W3$),9% $),.% $),8-/% $&/49 :0&% CSC-AVINASHI AND CSC-GANAPATHY B ASP NOTES
Usin' /bs%!ip &ih in asp $html% $form name12fm'7 method1post% <ser 4ame C$input type12text7 name12t'7%$br% 5asswords C$input type12text7 name12t(7% $:45<, ,=5/128<,,347 4M/128,4'7 D-</12!<8M:,7% $)form% $script language12vbscript7% sub btn'Eonclick dim a,b a1trim(document.fm'.t'.value) b1trim(document.fm'.t(.value) if a122 or b122 then msgbox(2/nter ll Dalues7) else fm'.action12test.asp7 fm'.submit() end if end sub $)script% $)html% Re=$es-"a+b,es p!"pe!, ,he totalbytes property specifies the total number of bytes the client sent in the body of the re#uest. ,his property is read-only. CSC-AVINASHI AND CSC-GANAPATHY ? ASP NOTES EXAMPLE One-asp $html% $form name12fm'7 method12post7 action12ms.asp7% /nter =our 4ame C$input type12text7 name12t'7% /nter 5assword C$input type12password7 name12t(7% $input type12submit7 value12send7% $)html% )s-asp $& dim nam, pwd nam1re#uest.form(2t'7) pwd1re#uest.form(2t(7) response.write nam F (2$br%2) response.write pwd F (2$br%2) response.write (2,otal 8ytes 3ccupyC 2) response.write (re#uest.totalbytes) &% Resp"nse-%+ea! ,he clear method erases any buffered html output. when response. clear is called, !5 empties the buffer output immediately(clears only the existing buffer). EXAMPLE $& response.buffer1true CSC-AVINASHI AND CSC-GANAPATHY G ASP NOTES &% $& dim nam, pwd nam1trim(re#uest.form(2t'7)) pwd1trim(re#uest.form(2t(7)) response.write nam F (2$br%2) response.write pwd F (2$br%2) response.clear response.write (2,otal 8ytes 3ccupyC 2) response.write (re#uest.totalbytes) &% Resp"nse-en# ,he end method causes the web server to stop processing the script and return the current result. ,he remaining content of the file are not processed. EXAMPLE $& dim nam, pwd nam1trim(re#uest.form(2t'7)) pwd1trim(re#uest.form(2t(7)) response.write nam F (2$br%2) response.write pwd F (2$br%2) response.end response.write (2,otal 8ytes 3ccupyC 2) response.write (re#uest.totalbytes) &% CSC-AVINASHI AND CSC-GANAPATHY H ASP NOTES Sessi"n Ob>e%Ai)e"$ p!"pe!ie a/ai+ab+eB !ession is a different type of variable it stores the datas it self and also pass the values to different pages. ,o give a variable session scop, store it in the session obIect by assigning a value to a named entry in the obIect. 0or example, the following commands store two new variables in the session obIect. $& session(2first name7)12Ieff7 session (2last name7)12smith7 &% to retrieve information from the session obIect,access the named entry by using the output directive ($1) or response.write. the following example uses the output directive to display the current value of session (20irst name7)J welcome $&1session(2first name7) &% SESSION TIMEOUT PROPERTIE $& session(2id7)12<!/.7 session(2id'7)1session(2id'7)K' response.write (2<!/. 4M/ C7) response.write !ession(2id7) response.write !ession(2id'7) session.timeout1' &% CSC-AVINASHI AND CSC-GANAPATHY L ASP NOTES SESSIONID PROPERTY ,he sessionid property is a read-only property that returns the session identification number for each user.each session has a uni#ue identifier,generated by the application when the session is created.the session identification number is actually a cookie that is stored on the user"s machine .it will expire only when the session timeout. 3ne rather large problem with the session obIect is that it simply won"t work if the user has cookies turned off.the session won"t get started when the user browses the site and they won"t get access to the session obIect ,o retrieve the current user"s session:d,useC $& session.sessionid &% EXAMPLE PROGRAM @OR SESSIONID login.asp $html% $form name12fm'7 method12post7 action12ssid.asp7% <ser 4ame $input type12text7 name12t'7%$br% $input type12submit7 value12-ogin7% $)html% !sid.asp $& dim unam unam1re#uest.form(2t'7) response.write (2<ser 4ame C7 F unam) response.write(2!ession:d C7 F session.sessionid) CSC-AVINASHI AND CSC-GANAPATHY M ASP NOTES &% APPLICATION OB0ECT pplication is the variable it holds the given value untill you shut down the system. E*a)p+e ("! app+i%ai"n 14 application(2numv7) num1application(2numv7) if num122 then application(2numv7)1' else application(2numv7)1numK' response.write(2number of times visit7) response.write(num) end if &% COODIES OB0ECT Aookies is the variable it holds the given value till closing the application. E*a)p+e P!"'!a) @"! C""Eies $& response.cookies(2numv7) num1re#uest.cookies(2numv7) if num122 then response.cookies(2numv7)1' CSC-AVINASHI AND CSC-GANAPATHY '@ ASP NOTES else response.cookies(2numv7)1numK' response.cookies(2numv7)1numK' response.write(24umber of times visit in the transaction7) response.write(num) end if &% GLOBAL-ASA !ince we can now store obIects and variables in an application Nlevel scope,we need to have a file to store these declarations in. this file is global.asa. each application can have only one global.asa, and it"s places in the virtual directory"s root.in global.asa,you can include event handler scripts and declare obIects that will have session or application scope.you can also store application-level variables and obIects used by the application. ,he file has no display component,as it is not displayed to users. :f you are using vbscript, global.asa can contain only four event handling subroutinesC '. applicationEonstart(runs the first time a user runs any page in your application). (. applicationEonend(run once when the web server shutdowns). *. sessionEonstart(runs once when first page of your application is runs for the first time). B. sessionEonend(runs when session obIect timeout or #uits your application). /xample CSC-AVINASHI AND CSC-GANAPATHY '' ASP NOTES >lobal.asp $script language12vbscript7 runat12server7% sub applicationEonstart application(2unam7)12 2 end sub sub applicationEonend application(2unam7)12 2 end sub sub sessionEonstart session(2unam7)12 2 end sub sub sessionEonend session(2unam7)12 2 end sub REDIRECT OB0ECT Insea# "( sen#in' %"nen " a $se!. ,"$ %an !e#i!e% he b!"&se! " an"he! URL &ih !e#i!e% )eh"#- @"! e*a)p+e.i( ,"$ &an " )aEe s$!e $se!s ha/e ene!e# ,"$! app+i%ai"n (!") a h")e pa'e s" ha he, !e%ei/e a %$s")e! ID.,"$ %he%E " see i( he, ha/e a %$s")e! I#Fi he, #" n".,"$ %an !e#i!e% he) " he h")e pa'e- 14 i( sessi"nA5C$s")e!I#:B39 hen !esp"nse-!e#i!e% 5h")epa'e-asp: en# i( 42 CSC-AVINASHI AND CSC-GANAPATHY '( ASP NOTES DATABASE CONNECTIVITE The ("++"&in' &" "b>e%s nee#e# ("! %!eain' #aabase %"nne%i"n 6- se!/e!-%!eae"b>e%A5a#"#b-%"nne%i"n:B 7- se!/e!-%!eae"b>e%A5a#"#b-!e%"!#se:B CONNECTION OB0ECT This "b>e% is $se# " 'e he spe%i(ie# #aabase na)e (!") pah- RECORDSET OB0ECT This "b>e% is $se# " 'e he spe%i(ie# ab+e (!") #aabase- CONNECTION STRING This "b>e% is $se# " 'i/e he ine!a%i"n be&een #aabase app+i%ai"nAba%E en#B an# AspA(!"n en#B- EXAMPLE PROGRAM @OR RETRIEVE THE DATAS 1h)+2 1("!) na)e35()6:2 1se+e% na)e35ss6:21b!2 14 #i) a#"%"n #i) a#"!e% se a#"%"n3se!/e!-%!eae"b>e%A5a#"#b-%"nne%i"n:B se a#"!e%3se!/e!-%!eae"b>e%A5a#"#b-!e%"!#se:B a#"%"n-%"nne%i"ns!in'35P!"/i#e!3Mi%!"s"(-0e-OLEDB-G-H6FDa a S"$!%e3C8IInep$bI&&&!""Ia/967I(i!s-)#b: CSC-AVINASHI AND CSC-GANAPATHY '* ASP NOTES a#"%"n-"pen a#"!e%-"pen 5se+e% J(!") se%"n#:.a#"%"n.G.G.6 a#"!e%-)"/e(i!s &hi+e n" a#"!e%-e"( !esp"nse-&!ieA51"pi"n25B !esp"nse-&!ie a#"!e%A5na)e:B !esp"nse-&!ieA51;"pi"n25B a#"!e%-)"/ene* &en# !esp"nse-&!ieA51;se+e%25B a#"!e%-%+"se a#"%"n-%+"se 42 1;h)+2 EXAMPLE PROGRAM @OR VIEW THE RECORDS IN A TABLE @M6-ASP 1HTML2 1("!) na)e35()6: )eh"#35p"s: a%i"n35sh"&-asp:2 Se+e% An, Na)e 81se+e% na)e35ss6:21b!21b!2 14 #i) a#"%"n #i) a#"!e% se a#"%"n3se!/e!-%!eae"b>e%A5a#"#b-%"nne%i"n:B se a#"!e%3se!/e!-%!eae"b>e%A5a#"#b-!e%"!#se:B a#"%"n-%"nne%i"ns!in'35P!"/i#e!3Mi%!"s"(-0e-OLEDB-G-H6FDa a S"$!%e3C8IInep$bI&&&!""Ia/967I(i!s-)#b: a#"%"n-"pen a#"!e%-"pen 5se+e% J (!") se%"n#:.a#"%"n CSC-AVINASHI AND CSC-GANAPATHY 'B ASP NOTES a#"!e%-)"/e(i!s &hi+e n" a#"!e%-e"( !esp"nse-&!ie A51"pi"n25B !esp"nse-&!ie a#"!e%A5na)e:B !esp"nse-&!ie A51;"pi"n25B a#"!e%-)"/ene* &en# !esp"nse-&!ie A51;se+e%21b!21b!25B 42 1inp$ ,pe35s$b)i: /a+$e35Sh"&:2 1;("!)2 1;h)+2 SHOW-ASP 1h)+2 1ab+e b"!#e!36 a+i'n3%ene!2 14 #i) na) #i) a#"!e%.a#"%"n na)3!i)A!e=$es-("!)A5ss6:BB se a#"%"n3se!/e!-%!eae"b>e%A5a#"#b-%"nne%i"n:B se a#"!e%3se!/e!-%!eae"b>e%A5a#"#b-!e%"!#se:B a#"%"n-%"nne%i"ns!in'3A5P!"/i#e!3Mi%!"s"(-0e-OLEDB-G-H6FD aaS"$!%e3C8IInep$bI&&&!""Ia/967I(i!s-)#bB a#"%"n-"pen a#"!e%-"pen 5se+e% J (!") se%"n#:.a#"%"n &hi+e n" a#"!e%-e"( i( a#"!e%A5na)e:B3na) hen !esp"nse-&!ie A51!21#2AGE 81;#21#2SALARY1;#21;!21!21#25B !esp"nse-&!ie a#"!e%A5a'e:B CSC-AVINASHI AND CSC-GANAPATHY '? ASP NOTES !esp"nse-&!ie A51;#21#25B !esp"nse-&!ie a#"!e%A5sa+a!,:B en# i( a#"!e%-)"/ene* &en# 42 1;ab+e2 1;h)+2 EXAMPLE PROGRAM @OR DIRECT <UERY PASSING METHOD adorec.open 2select O from second where name1 P2 F nam F 2"7,adocon,*,*,' EXAMPLE PROGRAM @OR APPENDING @M7-ASP $html% $form name12fm(7 method12post7 action12show'.asp7% /nter 4ame C$input type12text7 name12t'7%$br% /nter ge C$input type12text7 name12t(7%$br% /nter !alaryC$input type12text7 name12t*7%$br% $input type12submit7 value12!+3W7% $)html% SHOW6-ASP $& dim adorec,adocon,nam,ag,sal nam1re#uest.form(2t'a7) ag1re#uest.form(2t(7) sal1re#uest.form(2t*7) set adocon1server.createobIect(2adodb.connection7) CSC-AVINASHI AND CSC-GANAPATHY 'G ASP NOTES set adorec1server.createobIect(2adodb.recordset7) adocon.connectionstring125rovider1Microsoft.Qet.3-/98.*.?'J9ata !ource1ACR:netpubRwwwrootRav@'(Rfirst.mdb7 adocon.open adorec.open 2select O from second7,adocon,*,*,' adorec.movelast adorec.addnew adorec(2name7)1nam adorec(2age7)1ag adorec(2salary7)1sal adorec.update adorec.close adocon.close &% EXAMPLE PROGRAM @OR DELETE AN EXISTING RECORD $& 1re#uest.form(2s'7) dorec.open 2delete from first where id12 F F 2 2,adocon,*,*,' &% EXAMPLE PROGRAM @OR UPDATE AN EXISTING RECORD $& sal1re#uest.form(2newsal7) id1re#uest.form(2id7) dorec.open 2update second set salary1salaryK7 F sal F7 where id12 F id F 2 2,adocon &% CSC-AVINASHI AND CSC-GANAPATHY 'H