Unit Iv WT
Unit Iv WT
Unit Iv WT
ASP – Working of ASP – Objects – File System Objects – ADO – Access a Database from ASP
– Server side Active-X Components – HTTP GET and POST requests – session tracking –
cookies.
Exercise: Programs using DOM and SAX parsers.
--------------------------------------------------------------------------------------------------------------------
ASP
ASP stands for Active Server Pages
ASP is a Microsoft Technology
ASP is a program that runs inside a web server
An ASP file has the file extension ".asp"
An ASP file is just the same as an HTML file
An ASP file can contain server scripts in addition to HTML
Server scripts in an ASP file are executed on the server
ASP and ASP.NET are server side technologies.
Both technologies enable computer code to be executed by an Internet server.
When a browser requests an ASP or ASP.NET file, the ASP engine reads the file,
executes any code in the file, and returns the result to the browser.
<!DOCTYPE html>
<html>
<body>
<%
response.write("My first ASP script!")
%>
</body>
</html>
ASP can
Edit, change, add content, or customize any web page
Respond to user queries or data submitted from HTML forms
Access databases or other server data and return results to a browser
Provide web security since ASP code cannot be viewed in a browser
Offer simplicity and speed
Working of ASP
When a browser requests a normal HTML file, the server just returns the file.
When a browser requests an ASP file, the server passes the request to the ASP engine
which reads the ASP file and executes the server scripts in the file.
Finally the ASP file is returned to the browser as plain HTML.
The default scripting language in ASP is VBScript.
A scripting language is a lightweight programming language.
VBScript is a light version of Microsoft's Visual Basic.
ASP files can be ordinary HTML files. In addition, ASP files can also contain server
scripts.
Scripts surrounded by <% and %> are executed on the server.
The Response.Write() method is used by ASP to write output to HTML.
The following example writes "Hello World" into HTML:
Example
<!DOCTYPE html>
<html>
<body>
<%
Response.Write("Hello World!")
%>
</body>
</html>
To set JavaScript as the scripting language for a web page you must insert a language
specification at the top of the page:
Example
<%@ language="javascript"%>
<!DOCTYPE html>
<html>
<body>
<%
Response.Write("Hello World!")
%>
</body>
</html>
Before a database can be accessed from a web page, a database connection has to be established
If you have a database called "northwind.mdb" located in a web directory like "c:/webdata/", you
can connect to the database with the following ASP code:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
%>
The ADO Connection object is used to create an open connection to a data source. Through this
connection, you can access and manipulate a database.
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Customers", conn
%>
We can also get access to the data in the "Customers" table using SQL:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", conn
%>
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", conn
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT * FROM Customers", conn
do until rs.EOF
for each x in rs.Fields
Response.Write(x.name)
Response.Write(" = ")
Response.Write(x.value & "<br>")
next
Response.Write("<br>")
rs.MoveNext
loop
rs.close
conn.close
%>
</body>
</html>
name1=value1&name2=value2
Some notes on POST requests:
POST requests are never cached
POST requests do not remain in the browser history
POST requests cannot be bookmarked
POST requests have no restrictions on data length
Security GET is less secure compared to POST POST is a little safer than GET
because data sent is part of the URL because the parameters are not
stored in browser history or in web
Never use GET when sending server logs
passwords or other sensitive
information!
Visibility Data is visible to everyone in the URL Data is not displayed in the URL
Session Tracking
A session starts when:
A new user requests an ASP file, and the Global.asa file includes a Session_OnStart
procedure
A value is stored in a Session variable
A user requests an ASP file, and the Global.asa file uses the <object> tag to instantiate an
object with session scope
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.
If you want to set a timeout interval that is shorter or longer than the default, use
the Timeout property.
The example below sets a timeout interval of 5 minutes:
<%
Session.Timeout=5
%>
Cookies
A cookie is often 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. With ASP, you can both create and retrieve cookie values.