ASP Tutorial

Reviews
Shared by: swetha19
Stats
views:
469
rating:
not rated
reviews:
0
posted:
8/12/2009
language:
English
pages:
0
INTRODUCTION      ASP stands for Active Server Pages ASP is a program that runs inside IIS (Internet Information Services) An ASP file can contain text, HTML, XML, and scripts Scripts in an ASP file are executed on the server When a browser requests an ASP file, IIS passes the request to the ASP engine. The ASP engine reads the ASP file, line by line, and executes the scripts in the file. Finally, the ASP file is returned to the browser as plain HTML You can run ASP on your own PC without an external server. To do that, you must install Microsoft's Personal Web Server (PWS) or Internet Information Services (IIS) on your PC. The default scripting language is VBScript. To set JavaScript as the default scripting language for a particular page you must insert a language specification at the top of the page. Like this: <%@ language="JavaScript"%> Advantages of ASP: • Minimizes network traffic by limiting the need for the browser and server to talk to each other • Makes for quicker loading time since HTML pages are only downloaded • Allows to run programs in languages that are not supported by the browser • Can provide the client with data that does not reside on the client’s machine • Provides improved security measures since the script cannot be viewed by the browser Difference between server side scripting and client side scripting: Scripts executed only by the browser without contacting the server is called client-side script. It is browser dependent. The scripting code is visible to the user and hence not secure. Scripts executed by the web server and processed by the server is called server-side script. Order of execution for an ASP application: 1) Global.asa 2) Server-side Includes statements 3) Directives 4) Jscript scripts tagged within <% Response.write("3

") %> OUTPUT: 2 4 <%@language="JScript"%> <% Response.Write("3

"); %> 3 1 5 OUTPUT: 1 5 3 2 4 ASP PROCEDURES Call a procedure using vbscript: <% sub vbproc(num1,num2) response.write(num1*num2) end sub %> <%call vbproc(3,4)%> OR <%vbproc 3,4%> Call a procedure using javascript: <%@ language="javascript" %> <% function jsproc(num1,num2) { Response.Write(num1*num2) } %> <%jsproc(3,4)%> Call both vbscript and javascript procedures in vbscript: <% sub vbproc(num1,num2) Response.Write(num1*num2) end sub %> <%call vbproc(3,4)%> <%call jsproc(3,4)%> Note: When calling a JavaScript or a VBScript procedure from an ASP file written in JavaScript, always use parentheses after the procedure name. In vbscript, parentheses are optional. If you omit the "call" keyword, the parameter list must not be enclosed in parentheses. ASP FORMS and INPUTS Commands for information retrieval from forms: 1. Request.QueryString 2. Request.Form Difference between GET and POST methods: GET: 1. Limited data can be sent upto 255 characters. 2. Command used is Request.QueryString 3. Data is visible to the users. POST: 1. Umlimited data can be sent. 2. Command used is Request.Form 3. Data is not visible to the users. ASP COOKIES A cookie is a temporary file used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. Why is a cookie used? A cookie (persistent) enables a website to remember you on subsequent visits, speeding up or enhancing your experience of services or functions offered. For example, a website may offer its contents in different languages. On your first visit, you may choose to have the content delivered in French and the site may record that preference in a persistent cookie set on your browser. When you revisit that site it will use the cookie to ensure that the content is delivered in French. Creating a Cookie: <%Response.Cookies("firstname")="Alex"%> Properties: 1. Domain: This is the domain that the cookie originated from. It is set by default to the domain in which it was created but it can be altered. Example: Response.Cookies("name").Domain = "www.cookiemonster.com" 2. Expires: There are 2 ways. a) You can use the current date and add or subtract days Response.Cookies("name").Expires = Date + 365 b) You can set it to a specific date Response.Cookies("name").Expires = #May 10,2002# Note: To expire the session enabled with cookies, Give current date. Example: Response.Cookies("name").Expires = Date OR Date-1 3. Path: This specifies in more detail the exact path on the domain that can use the cookie. Response.Cookies("name").Path = "/this/is/the/path" 4. Secure: If set, the cookie will only be set if the browser is using secure sockets or https:// to connect. Response.Cookies("name").Secure = True Dictionary Cookie OR Cookies with Keys: It is a cookie that can hold multiple values. Example: Response.Cookies("name")("first") = "John" Response.Cookies("name")("last") = "Smith" How to know if the cookie has keys? Ans: With the Haskeys property. Example: <% dim x,y for each x in Request.Cookies response.write("

") if Request.Cookies(x).HasKeys then for each y in Request.Cookies(x) response.write(x & ":" & y & "=" & Request.Cookies(x)(y)) response.write("
") next else Response.Write(x & "=" & Request.Cookies(x) & "
") end if response.write "

" next %> Note: There are limits on how much data you can put on a clients browser. Most browsers allow you to place 20 cookies per domain at a maximum of 4k each. Types of cookies: 1. Persistent: Persistent cookies are stored on your computer hard disk. They stay on your hard disk and can be accessed by web servers until they are deleted or have expired. Persistent cookies are not affected by your browser setting that deletes temporary files when you close your browser. 2. Non-Persistent: Non-persistent cookies are saved only while your web browser is running. They can be used by a web server only until you close your browser. They are not saved on your disk. ASP OBJECTS Response Object: Response object is used to display items on a web page. Request Object: The ASP Request object is used to get information from the user. Application Object: A group of ASP files that work together to perform some purpose is called an application. The Application object in ASP is used to tie these files together. Session Object: The Session object is used to store information about, or change settings for a user session. Server Object: The ASP Server object is used to access properties and methods on the server. ASPError Object: The ASPError object is used to display detailed information of any error that occurs in scripts in an ASP page. Object Context: The ObjectContext object can be used to commit or abort a transaction. Collections, Properties and Methods: OBJECT COLLECTIONS PROPERTIES 1. Cookies 1. Buffer Response 2. ContentType 3. Expires 4. ExpiresAbsolute 5. Charset 6. CacheControl 7.IsClientConnected 8. Pics 9. Status 1. Cookies 1. TotalBytes Request 2. Form 3. QueryString 4.ServerVariables 5.ClientCertificate Application 1. Contents 2. StaticObjects METHODS 1. Write 2. Redirect 3. End 4. Clear 5. Flush 6. BinaryWrite 7. AddHeader 8.AppendToLog 1. BinaryRead EVENTS Session 1. Contents 2. StaticObjects Server 1. CodePage 2. LCID 3. SessionID 4. Timeout 1. ScriptTimeOut 1. Lock 2. UnLock 3.Contents.Remove 4.Contents.RemoveAll() 1. Abandon 2. Contents.Remove 3.Contents.RemoveAll() 1. CreateObject 2. Execute 3. GetLastError() 4. Transfer 5. HTMLEncode 6. URLEncode 7. MapPath 1. Application_OnStart 2. Application_OnEnd 1. Session_OnStart 2. Session_OnEnd Error 1. ASPCode 2. ASPDescription 3. Category 4. Column 5. Line 6. Source 7. Description 8. File 9. Number Object Context 1. SetAbort 2. SetComplete 1.OnTransactionAbort 2.OnTransaction CommitEvent RESPONSE: Collections Collection Cookies Description Sets a cookie value. If the cookie does not exist, it will be created, and take the value that is specified Properties Property Buffer CacheControl Charset ContentType Expires ExpiresAbsolute IsClientConnected Pics Status Methods Method AddHeader AppendToLog BinaryWrite Clear End Flush Redirect Write REQUEST: Description Adds a new HTTP header and a value to the HTTP response Adds a string to the end of the server log entry Writes data directly to the output without any character conversion Clears any buffered HTML output Stops processing a script, and returns the current result Sends buffered HTML output immediately Redirects the user to a different URL Writes a specified string to the output Description Specifies whether to buffer the page output or not Sets whether a proxy server can cache the output generated by ASP or not Appends the name of a character-set to the content-type header in the Response object Sets the HTTP content type for the Response object Sets how long (in minutes) a page will be cached on a browser before it expires Sets a date and time when a page cached on a browser will expire Indicates if the client has disconnected from the server Appends a value to the PICS label response header Specifies the value of the status line returned by the server Collections Collection ClientCertificate Cookies Form QueryString ServerVariables Properties Property TotalBytes Description Returns the total number of bytes the client sent in the body of the request Description Contains all the field values stored in the client certificate Contains all the cookie values sent in a HTTP request Contains all the form (input) values from a form that uses the post method Contains all the variable values in a HTTP query string Contains all the server variable values Methods Method BinaryRead Description Retrieves the data sent to the server from the client as part of a post request and stores it in a safe array APPLICATION: Collections Collection Contents StaticObjects Description Contains all the items appended to the application through a script command Contains all the objects appended to the application with the HTML tag Methods Method Contents.Remove Contents.RemoveAll() Lock Unlock Description Deletes an item from the Contents collection Deletes all items from the Contents collection Prevents other users from modifying the variables in the Application object Enables other users to modify the variables in the Application object (after it has been locked using the Lock method) Events Event Application_OnEnd Application_OnStart Description Occurs when all user sessions are over, and the application ends Occurs before the first new session is created (when the Application object is first referenced) SESSION: Collections Collection Contents StaticObjects Description Contains all the items appended to the session through a script command Contains all the objects appended to the session with the HTML tag Properties Property CodePage LCID Description Specifies the character set that will be used when displaying dynamic content Sets or returns an integer that specifies a location or region. Contents like date, time, and currency will be displayed according to that location or region Returns a unique id for each user. The unique id is generated by the server Sets or returns the timeout period (in minutes) for the Session object in this application SessionID Timeout Methods Method Abandon Contents.Remove Contents.RemoveAll() Events Event Session_OnEnd Session_OnStart SERVER: Description Occurs when a session ends Occurs when a session starts Description Destroys a user session Deletes an item from the Contents collection Deletes all items from the Contents collection Properties Property ScriptTimeout Description Sets or returns the maximum number of seconds a script can run before it is terminated Methods Method CreateObject Execute GetLastError() HTMLEncode MapPath Transfer URLEncode ERROR: Properties Property ASPCode ASPDescription Category Column Description File Line Number Source Description Returns an error code generated by IIS Returns a detailed description of the error (if the error is ASPrelated) Returns the source of the error (was the error generated by ASP? By a scripting language? By an object?) Returns the column position within the file that generated the error Returns a short description of the error Returns the name of the ASP file that generated the error Returns the line number where the error was detected Returns the standard COM error code for the error Returns the actual source code of the line where the error occurred Description Creates an instance of an object Executes an ASP file from inside another ASP file Returns an ASPError object that describes the error condition that occurred Applies HTML encoding to a specified string Maps a specified path to a physical path Sends (transfers) all the information created in one ASP file to a second ASP file Applies URL encoding rules to a specified string ASP SESSION The Session object is used to store information about a user session. Session variables are used to store information about ONE single user, and are available to all pages in one application. Typically information stored in session variables are name, id, and preferences. Session is implemented by creating a unique cookie for each user. Session Starts when a new user requests an ASP file, and the Global.asa file includes a Session_OnStart procedure. A session ends if a user has not requested or refreshed a page in the application for a specified period. By default, this is 20 minutes. To set the timeout interval, Timeout property is used. <%Session.Timeout=5%> To end the session immediately, Abandon method is used. <%Session.Abandon%> Store session variables: <%Session("username")="Donald Duck"%> Retrieve session variables: <%Response.Write(Session("username"))%> Remove session variables: <% If Session.Contents("age")<18 then Session.Contents.Remove("sale") End If %> The Contents collection contains all session variables. It is possible to remove a session variable with the Remove method. To remove all variables in a session, use the RemoveAll method: <%Session.Contents.RemoveAll()%> To know the number of items in the content collection: use Count property <%Session.Contents.Count%> To see the values of all objects stored in the session object: <% dim i For Each i in Session.StaticObjects Response.Write(i & "
") Next %> What is the difference between session and cookie? If you set the variable to "cookies", then your users will not have to log in each time they enter your community. The cookie will stay in place within the user’s browser until it is deleted by the user. But Sessions are popularly used, as the there is a chance of your cookies getting blocked if the user browser security setting is set high. If you set the variable to "sessions", then user activity will be tracked using browser sessions, and your users will have to log in each time they re-open their browser. The Key difference would be cookies are stored in your hard disk whereas a session aren't stored in your hard disk. Sessions are basically like tokens, which are generated at authentication. A session is available as long as the browser is opened. ASP APPLICATION A group of ASP files that work together to perform some purpose is called an application. Application variables are available to all pages in one application. Application variables are used to store information about ALL users in a specific application. To create application variables: You can create Application variables in "Global.asa" like this: Lock and Unlock: <% Application.Lock 'do some application object operations Application.Unlock %> ASP #INCLUDE The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages. Syntax: or Virtual: to indicate a path beginning with a virtual directory File: to indicate a relative path. A relative path begins with the directory that contains the including file. Note: Included files are processed and inserted before the scripts are executed. GLOBAL.ASA FILE The Global.asa file is an optional file (stored in the root directory of the application) that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application. The Global.asa file can contain only the following:      Application events Session events declarations TypeLibrary declarations the #include directive Events in Global.asa Application_OnStart: Occurs when 1. The FIRST user calls the first page from an ASP application. 2. after the Web server is restarted or after the Global.asa file is edited Session_OnStart: Occurs 1. EVERY time a NEW user requests his or her first page in the ASP application. Session_OnEnd: Occurs 1. EVERY time a user ends a session. 2. after a page has not been requested by the user for a specified time Application_OnEnd: Occurs 1. after the LAST user has ended the session. 2. when a Web server stops Note: we cannot use the ASP script delimiters (<% and %>) to insert scripts in the Global.asa file A Global.asa file could look something like this: Object Declaration: Syntax: .... Parameters: 1. scope 2. id 3. ProgID 4. ClassID Example: Difference between Object tag and Server.CreateObject: object tag creates the object and initializes the memory only when the first method/property of the object was accessed. Server.CreateObject will immediately create the object and initialize the memory. ASP COMPONENTS 1. 2. 3. 4. AdRotator Content Rotator Content Linking Browser Capabilities 1. ADROTATOR: The ASP AdRotator component creates an AdRotator object that displays a different image each time a user enters or refreshes a page. Syntax: <% set adrotator=server.createobject("MSWC.AdRotator") adrotator.GetAdvertisement("textfile.txt") %> //Here textfile.txt contains information about the images Example: ASP page looks like below: /* code if images are hyperlinks <% url=Request.QueryString("url") If url<>"" then Response.Redirect(url) %> */ <% set adrotator=Server.CreateObject("MSWC.AdRotator") response.write(adrotator.GetAdvertisement("textfile.txt")) %> Txt file looks like below: REDIRECT banners.asp //is used when images should be hyperlinks WIDTH 468 //width, height and border are for the size of the image that is displayed HEIGHT 60 BORDER 0 * w3schools.gif //image http://www.w3schools.com/ //hyperlink address Visit W3Schools Alt text 80 //percent of hits microsoft.gif http://www.microsoft.com/ Visit Microsoft 20 Methods: 1. GetAdvertisement Properties: 1. Border 2. Clickable 3. TargetFrame 2. CONTENT ROTATOR: The ASP Content Rotator component creates a ContentRotator object that displays a different HTML content string each time a user enters or refreshes a page. Syntax: <% Set cr=Server.CreateObject( "MSWC.ContentRotator" ) %> Example: //ASP File looks like this: <% set cr=server.createobject("MSWC.ContentRotator") response.write(cr.ChooseContent("text/textads.txt")) %> //Txt file looks like this %% #1 This is a great day!! %% #2

Smile

%% #3 %% #4 Here's a link. Note: #Num indicates the relative weight of the HTML content string Methods: 1. ChooseContent 2. GetAllContent 3. CONTENT LINKING: The ASP Content Linking component is used to create a quick and easy navigation system! The Content Linking component returns a Nextlink object that is used to hold a list of Web pages to be navigated. Syntax: <% Set nl=Server.CreateObject( "MSWC.NextLink" ) %> Example: //txt file looks like this: asp_intro.asp ASP Intro asp_syntax.asp ASP Syntax asp_variables.asp ASP Variables asp_procedures.asp ASP Procedures //On each of the pages listed above, put one line of code:
Related docs
asp tutorial
Views: 1846  |  Downloads: 171
asp tutorial free
Views: 1069  |  Downloads: 109
ASP Tutorial - Online Ad Management
Views: 22  |  Downloads: 2
ASP Syntax
Views: 6  |  Downloads: 1
ASP Tutorial HTML Form to Database Connectivity
Views: 361  |  Downloads: 93
Tutorial
Views: 123  |  Downloads: 20
Asp Return Data
Views: 83  |  Downloads: 1
asp html forms
Views: 337  |  Downloads: 74
dreamweaver asp templates
Views: 93  |  Downloads: 18
ASP script
Views: 202  |  Downloads: 0
premium docs
Other docs by swetha19
AJAX Basic Tutorial
Views: 226  |  Downloads: 20