Você está na página 1de 17

ASP NOTES

ASP (ACTIVE SERVER PAGE)


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 combaine 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 A 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. Script commands can store a users name in a variable, display the users name in a page returned to the browser, or store the users name in a database. Script commands are differentiated from text by delimiters. A delimiter is a character or sequence 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. Asp 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 introduce with greet feature of mixing language system. you can include html, vbscript or javascript tags also within asp.example for mixing language system has shown below. <html> <body bgcolor=red> <% if time>=12:00:00AM time <12:00:00PM then %> good morning <% else %> Hello </html>

REQUEST OBJECT Using the asp request object you can create simple,yet powerful scripts for collecting and processing data gathered with html forms. In this topic you will not only learn how to create basic form information,both on your web server and at the users browser. POST METHOD

CSC-AVINASHI AND CSC-GANAPATHY

ASP NOTES

Post is the method to submit the datas from the page to page.if you want to use this method you must add the submit button in your form. GET METHOD Get is the method to submit the datas from the page to page.this method always shows the data in your address bar. EXAMPLE PROGRAM FOR REQUEST OBJECT
<html> <form name=fm1 method=post action=use.asp> Enter your name :<input name=nam><br><br> Enter your Age :<input name=ag><br> <input type=submit value=hand over>

</html>

USE.ASP <% dim na,ag na=request.form(nam) ag=request.form(ag) response.write name is : & na & <br><br> response.write Age Is : & ag %> REQUEST WITH PARAMETER

CSC-AVINASHI AND CSC-GANAPATHY

ASP NOTES

EXAMPLE <HTML> <A HREF=ONE.ASP?PARA=ONE>ONE</A><BR> <A HREF=ONE.ASP?PARA=TWO>TWO</A><BR> </HTML> ONE.ASP <% DIM PR PR=trim(REQUEST.QUERYSTRING(PARA)) %> <% IF PR=ONE THEN %> <TABLE WIDTH=50% ALIGN=CENTER BORDER=10> <TR> <TD>PARAMETER ONE</TD> </TR> </TABLE> <%END IF%> <% IF PR=TWO THEN %> <TABLE WIDTH=50% ALIGN=CENTER BORDER=10> <TR> <TD>PARAMETER TWO</TD> </TR> </TABLE> <%END IF%>
CSC-AVINASHI AND CSC-GANAPATHY
4

ASP NOTES

Using vbscript with in asp <html> <form name=fm1 method=post> User Name :<input type=text name=t1><br> Passwords :<input type=text name=t2> <INPUT TYPE=BUTTON NAME=BTN1 VALUE=SUBMIT> </form> <script language=vbscript> sub btn1_onclick dim a,b a=trim(document.fm1.t1.value) b=trim(document.fm1.t2.value) if a= or b= then msgbox(Enter All Values) else fm1.action=test.asp fm1.submit() end if end sub </script> </html> Request.totalbytes property The totalbytes property specifies the total number of bytes the client sent in the body of the request. This property is read-only.

CSC-AVINASHI AND CSC-GANAPATHY

ASP NOTES

EXAMPLE One.asp <html> <form name=fm1 method=post action=ms.asp> Enter Your Name :<input type=text name=t1> Enter Password :<input type=password name=t2> <input type=submit value=send> </html> ms.asp <% dim nam, pwd nam=request.form(t1) pwd=request.form(t2) response.write nam & (<br>) response.write pwd & (<br>) response.write (Total Bytes Occupy: ) response.write (request.totalbytes) %> Response.clear The clear method erases any buffered html output. when response. clear is called, ASP empties the buffer output immediately(clears only the existing buffer). EXAMPLE <% response.buffer=true
CSC-AVINASHI AND CSC-GANAPATHY
6

ASP NOTES

%> <% dim nam, pwd nam=trim(request.form(t1)) pwd=trim(request.form(t2)) response.write nam & (<br>) response.write pwd & (<br>) response.clear response.write (Total Bytes Occupy: ) response.write (request.totalbytes) %> Response.end The end method causes the web server to stop processing the script and return the current result. The remaining content of the file are not processed. EXAMPLE <% dim nam, pwd nam=trim(request.form(t1)) pwd=trim(request.form(t2)) response.write nam & (<br>) response.write pwd & (<br>) response.end response.write (Total Bytes Occupy: ) response.write (request.totalbytes) %>

CSC-AVINASHI AND CSC-GANAPATHY

ASP NOTES

Session Object(timeout propertie available) Session is a different type of variable it stores the datas it self and also pass the values to different pages. To give a variable session scop, store it in the session object by assigning a value to a named entry in the object. For example, the following commands store two new variables in the session object. <% session(first name)=jeff session (last name)=smith %> to retrieve information from the session object,access the named entry by using the output directive (<=) or response.write. the following example uses the output directive to display the current value of session (First name); welcome <%=session(first name) %> SESSION TIMEOUT PROPERTIE <% session(id)=USER session(id1)=session(id1)+1 response.write (USER NAME :) response.write Session(id) response.write Session(id1) session.timeout=1 %>

CSC-AVINASHI AND CSC-GANAPATHY

ASP NOTES

SESSIONID PROPERTY The sessionid property is a read-only property that returns the session identification number for each user.each session has a unique identifier,generated by the application when the session is created.the session identification number is actually a cookie that is stored on the users machine .it will expire only when the session timeout. One rather large problem with the session object is that it simply wont work if the user has cookies turned off.the session wont get started when the user browses the site and they wont get access to the session object To retrieve the current users sessionId,use: <% session.sessionid %> EXAMPLE PROGRAM FOR SESSIONID

login.asp
<html> <form name=fm1 method=post action=ssid.asp> User Name <input type=text name=t1><br> <input type=submit value=Login> </html>

Ssid.asp
<% dim unam unam=request.form(t1) response.write (User Name : & unam) response.write(SessionId : & session.sessionid)
CSC-AVINASHI AND CSC-GANAPATHY
9

ASP NOTES

%> APPLICATION OBJECT Application is the variable it holds the given value untill you shut down the system. Example for application <% application(numv) num=application(numv) if num= then application(numv)=1 else application(numv)=num+1 response.write(number of times visit) response.write(num) end if %> COOKIES OBJECT Cookies is the variable it holds the given value till closing the application. Example Program For Cookies <% response.cookies(numv) num=request.cookies(numv) if num= then response.cookies(numv)=1
CSC-AVINASHI AND CSC-GANAPATHY
10

ASP NOTES

else response.cookies(numv)=num+1 response.cookies(numv)=num+1 response.write(Number of times visit in the transaction) response.write(num) end if %> GLOBAL.ASA Since we can now store objects and variables in an application level 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 its places in the virtual directorys root.in global.asa,you can include event handler scripts and declare objects that will have session or application scope.you can also store application-level variables and objects used by the application. The file has no display component,as it is not displayed to users. If you are using vbscript, global.asa can contain only four event handling subroutines: 1. application_onstart(runs the first time a user runs any page in your application). 2. application_onend(run once when the web server shutdowns). 3. session_onstart(runs once when first page of your application is runs for the first time). 4. session_onend(runs when session object timeout or quits your application). Example
CSC-AVINASHI AND CSC-GANAPATHY
11

ASP NOTES

Global.asp <script language=vbscript runat=server> sub application_onstart application(unam)= end sub sub application_onend application(unam)= end sub sub session_onstart session(unam)= end sub sub session_onend session(unam)= end sub REDIRECT OBJECT Instead of sending content to a user, you can redirect the browser to another URL with redirect method. For example,if you want to make sure users have entered your application from a home page so that they receive a customer ID,you check to see if they have a customer Id;it they do not,you can redirect them to the home page. <% if session(CustomerId)=0 then response.redirect homepage.asp end if %>

CSC-AVINASHI AND CSC-GANAPATHY


12

ASP NOTES

DATABASE CONNECTIVITE The following two objects needed for creating database connection 1. server.createobject(adodb.connection) 2. server.createobject(adodb.recordset) CONNECTION OBJECT This object is used to get the specified database name from path. RECORDSET OBJECT This object is used to get the specified table from database. CONNECTION STRING This object is used to give the interaction between database application(back end) and Asp(front end). EXAMPLE PROGRAM FOR RETRIEVE THE DATAS <html> <form name=fm1> <select name=ss1><br> <% dim adocon dim adorec set adocon=server.createobject(adodb.connection) set adorec=server.createobject(adodb.recordset) adocon.connectionstring=Provider=Microsoft.Jet.OLEDB.3.51;Da ta Source=C:\Inetpub\wwwroot\av012\first.mdb
CSC-AVINASHI AND CSC-GANAPATHY
13

ASP NOTES

adocon.open adorec.open select *from second,adocon,3,3,1 adorec.movefirst while not adorec.eof response.write(<option>) response.write adorec(name) response.write(</option>) adorec.movenext wend response.write(</select>) adorec.close adocon.close %> </html> EXAMPLE PROGRAM FOR VIEW THE RECORDS IN A TABLE FM1.ASP <HTML> <form name=fm1 method=post action=show.asp> Select Any Name :<select name=ss1><br><br> <% dim adocon dim adorec set adocon=server.createobject(adodb.connection) set adorec=server.createobject(adodb.recordset) adocon.connectionstring=Provider=Microsoft.Jet.OLEDB.3.51;Da ta Source=C:\Inetpub\wwwroot\av012\first.mdb adocon.open adorec.open select * from second,adocon
CSC-AVINASHI AND CSC-GANAPATHY
14

ASP NOTES

adorec.movefirst while not adorec.eof response.write (<option>) response.write adorec(name) response.write (</option>) adorec.movenext wend response.write (</select><br><br>) %> <input type=submit value=Show> </form> </html> SHOW.ASP <html> <table border=1 align=center> <% dim nam dim adorec,adocon nam=trim(request.form(ss1)) set adocon=server.createobject(adodb.connection) set adorec=server.createobject(adodb.recordset) adocon.connectionstring=(Provider=Microsoft.Jet.OLEDB.3.51;D ataSource=C:\Inetpub\wwwroot\av012\first.mdb) adocon.open adorec.open select * from second,adocon while not adorec.eof if adorec(name)=nam then response.write (<tr><td>AGE :</td><td>SALARY</td></tr><tr><td>) response.write adorec(age)
CSC-AVINASHI AND CSC-GANAPATHY
15

ASP NOTES

response.write (</td><td>) response.write adorec(salary) end if adorec.movenext wend %> </table> </html> EXAMPLE PROGRAM FOR DIRECT QUERY PASSING METHOD
adorec.open select * from second where name= & nam & ,adocon,3,3,1

EXAMPLE PROGRAM FOR APPENDING FM2.ASP <html> <form name=fm2 method=post action=show1.asp> Enter Name :<input type=text name=t1><br> Enter Age :<input type=text name=t2><br> Enter Salary:<input type=text name=t3><br> <input type=submit value=SHOW> </html> SHOW1.ASP <% dim adorec,adocon,nam,ag,sal nam=request.form(t1a) ag=request.form(t2) sal=request.form(t3) set adocon=server.createobject(adodb.connection)
CSC-AVINASHI AND CSC-GANAPATHY
16

ASP NOTES

set adorec=server.createobject(adodb.recordset) adocon.connectionstring=Provider=Microsoft.Jet.OLEDB.3.51;Data Source=C:\Inetpub\wwwroot\av012\first.mdb adocon.open adorec.open select * from second,adocon,3,3,1 adorec.movelast adorec.addnew adorec(name)=nam adorec(age)=ag adorec(salary)=sal adorec.update adorec.close adocon.close %> EXAMPLE PROGRAM FOR DELETE AN EXISTING RECORD <% A=request.form(s1) Adorec.open delete from first where id= & A & ,adocon,3,3,1 %> EXAMPLE PROGRAM FOR UPDATE AN EXISTING RECORD <% sal=request.form(newsal) id=request.form(id) Adorec.open update second set salary=salary+ & sal & where id= & id & ,adocon %>

CSC-AVINASHI AND CSC-GANAPATHY


17

Você também pode gostar