ASP HistoryComponents - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

ASP HistoryComponents

Description:

... sending and retrieving of information from within your active server pages. ... Mostly used in debugging an Active Server Page. You can use a for.. next loop ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 51
Provided by: JFK
Category:

less

Transcript and Presenter's Notes

Title: ASP HistoryComponents


1
ASP History/Components
  • Active Server History
  • Introduced July 1996
  • Bundled with Internet Information Server (IIS)
    3,0 March of 1997
  • ASP 2.0 introduced in 1998 Shipped with IIS 4.0
  • AS 3.0 shipped as part of Windows 2000 IIS 5.0
  • IS considered to be more of a Technology than a
    language
  • It's syntax is comprised of a ASP, HTML tags and
    pure text.

2
ASP Capabilities
  • ASP can
  • Generates dynamic web pages
  • Processes the contents of HTML forms
  • Creates database driven web pages
  • Tracks user sessions can store information about
    users from the moment they arrived at web site
    tell the moment they leave.
  • Create searchable web pages
  • Detect capability different browsers
  • Send e-mail
  • Integrate custom components of your Web site
    including server side components created with
    Visual Basic ,C or Java.

3
ASP Flexibility
  • Static
  • VS
  • Dynamic
  • Websites

4
Simple Page
  • ltHTMLgt
  • ltHEADgt ltTITLEgt Hungry ASP Example lt/TITLEgt
    lt/Headgt
  • ltBODYgt
  • lt
  • For i 1 to 10
  • varvar"very,"
  • Response.Write(i""var"ltBRgt")
  • NEXT
  • gt
  • ltHRgt
  • I am ltvargt Hungry!!
  • lt/BODYgt
  • lt/HTMLgt

5
Object Overview
  • Objects Contain
  • Methods
  • Determine the things
  • Properties
  • Can be used to set the state of and object
  • Collections
  • Constitutes a set of Keys and Value pairs
    related to the object.

6
Object Example
  • A Dictionary
  • Methods
  • Look things up, used as a door stop.
  • Properties
  • Has a certain number of pages, and a particular
    author.
  • Collections
  • Each section corresponds to the text it
    contains.

7
ASP Components
  • Ad rotator component - used to display banner
    advertisements on pages a WebSite.
  • Web browsers capabilities component - to be used
    to display different HTML content according to
    the capabilities of different browsers. For
    example you can use is component to display Web
    pages with frames only to frames compliant
    browsers.
  • Content linking component- is used to live
    together several HTML pages so they can be
    navigating easily. for example you can use this
    component display the pages that online book
  • Counters component -the counters component and
    keep track of the number of visitors to your
    WebSite. You can use the counters component add a
    hit counter to a Web page.

8
ASP Components (cont)
  • Content rotator component - can be used to rotate
    through HTML content I page. For example you can
    use the component to randomly display different
    announcements on the home page of your WebSite.
  • Page counter - is exactly like counter component
    into the used to track visitors into a hit
    counter to a particular Web page.
  • Permissions checker component- can be used to
    display links to Web pages only to those users to
    their permission to see them.
  • Collaboration data objects the collaboration data
    objects enables sending and retrieving of
    information from within your active server pages.
    For example you can send an email after a new
    user registers at your WebSite
  • ActiveX data objects enabling you to retrieve
    information stored in a databases.

9
Page Scope
  • Page scope- is created on a single page and dies
    when the processing of the page ends
  • Can be created with server.Createobject() or the
    HTML ltobjectgt tag.

10
Page Scope Example
  • ltHTMLgt
  • ltHEADgtltTITLEgt Objectst - ASP Examplelt/TITLEgtlt/HEAD
    gt
  • ltBODYgt
  • lt
  • set MydictServer.CreateObject("Scripting.Dictiona
    ry")
  • MyDict.Add "CA", "California"
  • MYDict.Add "OH", "OHIO"
  • MyDict.Add "FL", "Florida"
  • gt
  • My dictionary has ltMydict.countgt entries.
  • ltBRgt
  • The first entry is ltMydict.Item("CA")gt
  • ltBRgt
  • lt/BODYgt
  • lt/HTMLgt

11
Session Scope
  • Components are created for each visitor to your
    web site
  • Components do not leave memory until the visitor
    leaves your website

12
Buffering
  • Can be set as a default for the website IIS
    Enable Buffering
  • Will not send page until the script completely
    processes
  • Response.BufferTrue must appear before any HTML
    or Script output otherwise an error will be
    issued.

13
Caching
  • Browsers will sometimes present a page from
    Cache instead of retrieving it from the
    website.
  • The browser can be set to always go to the
    website for pages
  • ASP provides Expires, ExpiresAbsolute for
    websites and CacheControl (for proxy servers)
  • Response.Expires 0 sets the page life to 0
    minutes and will always request fresh pages
  • Response.ExpiresAbsolute Jan 1, 2010
    000000 set the page to expire on midnight of
    12/31/2009
  • Response.CacheControl Public means anyone
    can use the page, Private does not cache the
    pages

14
ASP Status Codes
  • 1xx - Informational - Expermental
  • 2xx - Success
  • 3xx - Redirection
  • 4xx - Client Error
  • 5xx - Server Error

15
Navigating your User
  • On occasion, you will need to redirect your user
    to another page. (Access rights, Registration,
    Form completion etc)
  • Response.Redirect path/page.asp
  • Must be used before any text is outputted to the
    browser
  • Buffering may need to be utilized.

16
Request Object
  • Contains all of the information about the HTTP
    request performed to retrieve an Active Server
    Page
  • The request object contains three collections
  • Querystring used to pass information form the
    browser to the server. It appears after the ?
  • FormCollection - similar to the query string
    used to store variables posted within a HTML form
  • Server Variables when a browser requesta a web
    page from a server, the request contains several
    headers

17
Request Object Query String
  • Are typically not entered directly into the
    address bar of a browser .
  • Firstpage.ASP
  • lthtmlgt
  • ltheadgtlttitlegtColor Choicelt/titlegtlt/headgt
  • What is your favorite color?
  • ltPgt
  • lta href"nextpage.asp?favcolorblue"gtbluelt/agt
  • ltpgt
  • lta href"nextpage.asp?favcolorred"gtredlt/agt
  • lt/bodygt
  • lt/htmlgt

18
Query String Example
  • NextPage.ASP
  • lthtmlgt
  • ltheadgtlttitlegtYour Favorite Colorlt/titlegtlt/headgt
  • ltbodygt
  • lt
  • favColor Request.QueryString( "favColor" )
  • gt
  • Your favorite color is ltfavColorgt!
  • lt/bodygt
  • lt/htmlgt

19
URL Encoded Query String
  • Used when the value of a query string contains a
    non alphanumeric character such as a space,
    quotation mark, comma period, etc .
  • lta hrefmenupage.asp?favfoodchocolate
    cake"gtChocolate Cakelt/agt
  • Would need to be implemented as follows
  • lt
  • favfood Server.URLEncode (Chocolate Cake)
  • gt
  • lta hrefmenupage.asp?favfoodltfavfoodgt
    "gtChocolate Cakelt/agt

20
Multiple Query String Variables
  • You can pass multiple variables in the query
    string by utilizing the
  • ...
  • lta hreforder.asp?entrée steakdrinkpepsi"gtStea
    k and Pepsilt/agt
  • ...
  • order.asp
  • lt
  • Response.Write Request.QueryString(entrée)
  • Response.Write Request.QueryString(drink)
  • gt
  • You can pass multiple values to a variables in
    the query string by utilizing the
  • ...
  • lta hrefFlag.asp?fcolorredwhiteblue"gtUSAlt/agt
  • lt
  • Response.Write Request.QueryString(fcolor)
  • gt
  • Would produce red, white, blue

21
Dumping QueryString Collection
  • Mostly used in debugging an Active Server Page.
  • You can use a for.. next loop lt
  • for i 1 to Request.QueryString.Count
  • Response.Write Request.QueryString(i) ltbrgt)
  • next
  • gt
  • You can also use a for.. each loop
  • lt
  • for each thing in Request.QueryString
  • Response.Write thing
    Request.QueryString(thing) ltbrgt)
  • next
  • gt

22
Query Strings Pros/Cons
  • Can be useful for passing small bits of
    information.
  • Always displayed in clear text do not use for
    passing hidden data or large chunks of data.
  • Different browsers handle a different maximum
    limits of query string lengths

23
Request Object
  • Contains all of the information about the HTTP
    request performed to retrieve an Active Server
    Page
  • The request object contains three collections
  • Querystring used to pass information form the
    browser to the server. It appears after the ?
  • FormCollection - similar to the query string
    used to store variables posted within a HTML form
  • Server Variables When a browser requests a web
    page from a server it includes several headers
    which can be queried using this collection.

24
Forms Collection
  • The forms collection is very similar to the
    querystring collection.
  • FormCollection - similar to the query string
    used to store variables posted within a HTML form

25
Form Example
  • Color.ASP
  • lt
  • Dim color, state
  • color "Blue"
  • state "Ohio"
  • Function checked(firstVal, secondVal)
  • If firstVal secondVal then
  • checked " CHECKED"
  • End if
  • End Function
  • Function selected(firstVal, secondVal)
  • If firstVal secondVal then
  • checked " SELECTED"
  • End if
  • End Function
  • gt
  • lthtmlgt
  • ltheadgtlttitlegtColor Choice using
    Buttonslt/titlegtlt/headgt
  • lt/bodygt

26
Form Example
  • Colorp.ASP
  • lt
  • for each thing in request.form
  • Response.Write thing " "Request.form(thing)
    "ltbrgt"
  • next
  • DIM state,color
  • stateRequest.Form("state")
  • colorRequest.Form("color")
  • gt
  • lthtmlgt
  • ltheadgtlttitlegtColors ..lt/titlegtlt/headgt
  • ltbodygt
  • Do all of the people who live in ltstategt have
    ltcolorgt as their favorite color??
  • lt/bodygt
  • lt/htmlgt

27
Form Example
  • Comment.ASP
  • lthtmlgt
  • ltheadgtlttitlegtYour Comments Matterlt/titlegtlt/headgt
  • ltbodygt
  • ltform method"post" action"confirm.asp"gt
  • ltpgtPlease enter your name (L,F ) lt/bgt
  • ltinput name"username" type"text" size30gt
  • ltpgt Please enter your comments
  • ltbrgt lttextarea name"comments" cols40 rows5gt
    lt/textareagt
  • ltinput type"submit" value"Send comments"gt
  • lt/formgt
  • lt/bodygt
  • lt/ht

28
Form Example
  • Confirm.asp
  • lt
  • DIM username,comments
  • usernameRequest.Form(username)
  • commentsRequest.Form(comments)
  • gt
  • lthtmlgt
  • ltheadgtlttitlegtThanks ..lt/titlegtlt/headgt
  • ltbodygt
  • Your name is ltusernamegt
  • Your comments are ltcommentsgt
  • lt/bodygt
  • lt/htmlgt

29
Form Example
  • Confirm.asp
  • lt
  • DIM username,comments
  • usernameRequest.Form(username)
  • commentsRequest.Form(comments)
  • gt
  • lthtmlgt
  • ltheadgtlttitlegtThanks ..lt/titlegtlt/headgt
  • ltbodygt
  • Your name is ltusernamegt
  • Your comments are ltcommentsgt
  • lt/bodygt
  • lt/htmlgt

30
Form Example
  • Confirm.asp
  • lt
  • DIM username,comments
  • usernameRequest.Form(username)
  • commentsRequest.Form(comments)
  • gt
  • lthtmlgt
  • ltheadgtlttitlegtThanks ..lt/titlegtlt/headgt
  • ltbodygt
  • Your name is ltusernamegt
  • Your comments are ltcommentsgt
  • lt/bodygt
  • lt/htmlgt

31
Request Object
  • Contains all of the information about the HTTP
    request performed to retrieve an Active Server
    Page
  • The request object contains three collections
  • Querystring used to pass information form the
    browser to the server. It appears after the ?
  • FormCollection - similar to the query string
    used to store variables posted within a HTML form
  • Server Variables When a browser requests a web
    page from a server it includes several headers
    which can be queried using this collection.

32
Include Files
  • Are used like copy books to create function or
    code libraries
  • Syntax
  • lt!-- INCLUDE VIRTUAL /path/file.inc--gt
  • File does not nee to have the inc extension -
    common practice.

33
Server Variables
  • The ServerVariables collection retrieves the
    values of environment variables.
  • Request.ServerVariables (variable)
  • variable
  • Specifies the name of the server environment
    variable to retrieve. It can be one of the
    following values.
  • Variable Description
  • AUTH_TYPE The authentication method that the
    server uses to validate users when they attempt
    to access a protected script.
  • CONTENT_LENGTH The length of the content as
    given by the client.
  • CONTENT_TYPE The data type of the content. Used
    with queries that have attached information,
    such as the HTTP queries POST and PUT.
  • GATEWAY_INTERFACE The revision of the CGI
    specification used by the server. Format
    CGI/revision
  • HTTP_ltHeaderNamegt The value stored in the header
    HeaderName. Any header other than those listed
    in this table must be prefixed by HTTP_ in
    order for the ServerVariables collection to
    retrieve its value.
  • Note The server interprets any underscore (_)
    characters in HeaderName as dashes in the actual
    header. For example if you specify
    HTTP_MY_HEADER, the server searches for a header
    sent as MY-HEADER.

34
Server Variables (Continued)
  • LOGON_USER The Windows NT account that the user
    is logged into.
  • PATH_INFO Extra path information as given by the
    client. You can access scripts by using their
    virtual path and the PATH_INFO server variable.
    If this information comes from a URL it is
    decoded by the server before it is passed to the
    CGI script.
  • PATH_TRANSLATED A translated version of PATH_INFO
    that takes the path and performs any necessary
    virtual-to-physical mapping.
  • QUERY_STRING Query information stored in the
    string following the question mark (?) in the
    HTTP request.
  • REMOTE_ADDR The IP address of the remote host
    making the request.
  • REMOTE_HOST The name of the host making the
    request. If the server does not have this
    information, it will set REMOTE_ADDR and leave
    this empty.
  • REQUEST_METHOD The method used to make the
    request. For HTTP, this is GET, HEAD, POST, and
    so on.
  • SCRIPT_MAP Gives the base portion of the URL.
  • SCRIPT_NAME A virtual path to the script being
    executed. This is used for self-referencing URLs.
  • SERVER_NAME The server's host name, DNS alias,
    or IP address as it would appear in
    self- referencing URLs.
  • SERVER_PORT The port number to which the request
    was sent.

35
Server Variables (Continued)
  • SERVER_PORT_SECURE A string that contains either
    0 or 1. If the request is being handled on the
    secure port, then this will be 1. Otherwise, it
    will be 0.
  • SERVER_PROTOCOL The name and revision of the
    request information protocol. Format
    protocol/revision
  • SERVER_SOFTWARE The name and version of the
    server software answering the request (and
    running the gateway). Format name/version
  • URL Gives the base portion of the URL.
  • Query_String Gives the portion of the URL after
    the ?

36
Server Variables Example
  • Servervar.asp
  • ltHTMLgt
  • ltHEADgtltTITLEgt server variableslt/TITLEgtlt/HEADgt
  • ltBODYgt
  • the time is ltTIMEgt
  • lt
  • for each name in request.servervariables
  • response.write ("ltpgtltBgt"name"lt/bgt")
  • response.write (request.servervariables(name))
  • Next
  • gt
  • lt/BODYgt
  • lt/HTMLgt

37
File Passing - Upload
  • File Uploads allows the web visitor the
    capability of passing a file to the website.
  • ltHTMLgt
  • ltHEADgtltTITLEgt File Upload lt/TITLEgtlt/HEADgt
  • ltBODYgt
  • ltFORM ENCTYPE"multipart/form-data"
  • ACTION"upresult.asp" METHODPOSTgt
  • Please choose a picture to upload
  • ltBRgtltINPUT NAME"picture" TYPEFILE
    ACCEPT"image/"gt
  • ltBRgtltINPUT TYPESUBMIT VALUE"Submit Me!"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt

38
File Passing - Retrieve
  • To process the uploaded file
  • Upresults.asp
  • lt
  • FormSize Request.TotalBytes
  • FormData Request.BinaryRead( FormSize )
  • bnCRLF chrB( 13 ) chrB( 10 )
  • Divider LEFTB( FormData, INSTRB( FormData,
    bnCRLF ) - 1 )
  • DataStart INSTRB( FormData, bnCRLF bnCRLF )
    4
  • DataEnd INSTRB( DataStart 1, FormData,
    divider ) - DataStart
  • Response.ContentType "image/gif"
  • Response.BinaryWrite MIDB( FormData, DataStart,
    DataEnd )
  • gt

39
ODBC
  • Open Database Connectivity (ODBC) is a standard
    or open application programming interface
    (application program interface) for accessing a
    database. By using ODBC statements in a program,
    you can access files in a number of different
    databases, including Access, Oracle, Sybase SQL
    Server, DB2. ODBC allows programs to use SQL
    requests that will access databases without
    having to know the proprietary interfaces to the
    databases. ODBC handles the SQL request and
    converts it into a request the individual
    database system understands.

40
Making An Oracle Connection
  • DBinq.asp
  • lt
  • set conn server.createobject("ADODB.Connection
    ")
  • conn.open "Oracleadd","system","manager"
  • set rs server.createobject("ADODB.Recordset")
  • SQLSTR "SELECT from system.address order
    by lastname"
  • rs.open SQLSTR, conn, 3, 3
  • while not rs.eof
  • ln rs.fields("lastname")
  • response.write ln "ltbrgt "
  • rs.movenext
  • wend
  • conn.close
  • set rs nothing
  • set conn nothing
  • gt
  • lt/bodygt
  • lt/htmlgt

41
Making An Oracle Connection
  • For a Connection object
  • connection.Open ConnectionString, UserID,
    Password
  • For a Recordset object
  • recordset.Open Source, ActiveConnection,
    CursorType, LockType
  • The Open method syntax has these parts.
  • Part Description
  • Connection An object variable representing an
    existing Connection object.
  • recordset An object variable representing an
    existing Recordset object.
  • ConnectionString Optional. A String containing
    connection information. See the ConnectionString
    property for details on valid settings.
  • UserID Optional. A String containing a user name
    to use when establishing the connection.
  • Password Optional. A String containing a password
    to use when establishing the connection.
  • Source Optional. A Variant that evaluates to a
    valid Command object variable name, an SQL
    statement, a table name, or a stored procedure
    call.
  • ActiveConnection Optional. A Variant that
    evaluates to a valid Connection object variable
    name or a String containing a definition for a
    connection.

42
Making An Oracle Connection
  • CursorType Optional. A CursorTypeEnum value that
    determines the type of cursor that the provider
    should use when opening the Recordset. Can be one
    of the following constants
  • adOpenForwardOnly, 0 (Default)
  • adOpenKeyset, 1
  • adOpenDynamic, 2
  • adOpenStatic, 3
  • LockType Optional. A LockTypeEnum value that
    determines what type of locking (concurrency) the
    provider should use when opening the Recordset.
    Can be one of the following constants
  • adLockReadOnly, 1
  • adLockPessimistic, 2
  • adLockOptimistic, 3
  • adLockBatchOptimistic, 4
  • See the LockType property for definitions of
    these settings.

43
Sessions
  • A session is something that starts the moment a
    user request something from your website and ends
    soon after they leave
  • Each visitor of your website is given a unique
    session.
  • Sessions can be used to maintain preferences
    background color, frames, text only version etc.
  • Sessions can also track purchase requests
    (Virtual Shopping Carts) and checkout all at
    once.
  • Sessions can track the habits of your visitors as
    they roam from page to page. Useful for
    advertisements and overall web design
    improvements.

44
Sessions Variables
  • Session variables work much like application
    variables.
  • Main difference separate copies session variables
    are automatically created for each visitor.
  • Sessions use cookies
  • A unique identifier is created for each visitor
    and persists until they leave
  • A time threshold Is set to expire the session
  • ltSession.Timeout20gt

45
The State of Sessions
  • HTTP by nature is a simple set comprised of a
    request and response.
  • Each visit to a website is a new request
    regardless if it is the same visitor from the
    same browser.
  • This nature of HTTP creates a serious restriction
    in your ability to identify a user over multiple
    web pages.

46
Sessions -Cookies
  • The cookie is created in the user's browser
  • The purpose is to create a unique identifier(an
    activeX component object), to manage the
    session, when the session ends the cookie
    expires.
  • Response.Cookies(username) Jim Smith will
    create a cookie to persist information
  • There are four Microsoft ActiveX component
    threading models that can be used when creating a
    component for use with Active Server Pages
  • Single-Threaded (not recommended)
  • Apartment-Threaded (recommended)
  • Free-Threaded (not recommended)
  • Objects Marked as Both (highly recommended)

47
Single-Threaded Components
  • This threading model is not recommended for
    components that will be called by Active Server
    Pages.
  • Single-threaded objects run in exactly one
    thread, the one that called the CoInitialize
    method. Calls to these objects from other threads
    are marshaled to this thread. Only one thread at
    a time can enter a set of single-threaded
    objects, and it must be the same thread each
    time.
  • The Web server creates a parked thread that calls
    CoInitialize, and then marshals object calls to
    itself.
  • One disadvantage of single-threaded objects is
    that they cannot be stored in the Application
    object. The only way to create a single-threaded
    object with application scope is to declare it
    with an ltOBJECTgt tag. However, when this object
    is used, the Web server will lock the application
    down to a single thread, the one that created the
    object. This will reduce server performance
    significantly, and should only be considered for
    intranet applications.
  • In addition, if a single-threaded object is
    created with session scope or is stored in the
    Session object, the Web server will lock the
    session down to the single thread that created
    the object.

48
Apartment-Threaded
  • This threading model is recommended for
    components called by ASP.
  • Each apartment-model object may only be entered
    by one thread, the thread that called it.
    However, an object server can support multiple
    objects each being entered simultaneously by
    different threads. Common data held by the object
    server must be protected against thread
    collisions.
  • Calls to the object from the apartment thread are
    not marshaled. Requests that are in the same
    session as an apartment model object are handled
    by the thread that created the apartment model
    object.
  • Disadvantage of apartment-threaded objects is
    that they cannot be stored in the Application
    object. The only way to create an
    apartment-threaded object with application scope
    is to declare it with an ltOBJECTgt tag. However,
    when this object is used, the Web server will
    lock the application down to a single thread, the
    one that created the object. This will reduce
    server performance significantly, and should only
    be considered for intranet applications.
  • In addition, if an apartment-threaded object is
    created with session scope or is stored in the
    Session object, the Web server will lock the
    session down to the single thread that created
    the object.

49
Free-Threaded
  • This threading model is not recommended for
    components that will be called by ASP
  • Free-threaded objects place no restrictions on
    which threads or how many threads can enter the
    object. The object cannot contain thread-specific
    data and must protect its data from simultaneous
    access by multiple threads.
  • The Web server creates free-threaded objects in a
    different thread than the one that called
    CoCreateInstance. All calls to the object are
    marshaled to a free-threading thread.
  • One disadvantage of free-threaded objects is that
    they cannot be stored in the Application object.
    The only way to create a free-threaded object
    with application scope is to declare it with an
    ltOBJECTgt tag. However, when this object is used,
    the Web server will lock the application down to
    a single thread, the one that created the object.
    This will reduce server performance
    significantly, and should only be considered for
    intranet applications.
  • In addition, if a free-threaded object is created
    with session scope or is stored in the Session
    object, the Web server will lock the session down
    to the single thread that created the object.

50
Both Threaded
  • This threading model is highly recommended for
    components that will be called by Active Server
    Pages.
  • ActiveX objects marked as both can be used in
    either apartment-threaded or free-threaded modes.
    They can be entered by multiple threads, protect
    their data from thread collisions, and do not
    contain thread-specific data.
  • The Web server creates an object marked as both
    in a single thread, the one that serviced the
    request. The call to CoCreateInstance is not
    marshaled to another thread. However, if the
    object has session scope or is placed in the
    Session object, the Web server will not lock the
    session to that thread. Calls to the object from
    other requests in the same session are executed
    in the request thread.
  • Because objects marked as both guarantee to
    protect their data against multiple thread
    access, they can be used for objects that have
    application scope.
Write a Comment
User Comments (0)
About PowerShow.com