[Introduction][The
Browser's Result of a Computed ASP page][Personal
Web Server][Sending Data to the user]
How does it work?
The ASP environment enables you to create a dynamic website that is interactive,
without making you worry about the capabilities of your clients browsers,
which is a fact when you do client -side scripting like Javascript or Vbscript.
- When a browser requests an HTML file, the server returns the file
- When a browser requests an ASP file, the server calls ASP
- The server reads the ASP file and executes the scripts in the file
- Finally, the ASP file is returned to the browser as a plain HTML
file
What can ASP do for you?
- Dynamically edit, change or add any content of a Web page
- Respond to user queries or data submitted from HTML forms
- Access any data or databases and return the results to a browser
- The advantages of using ASP instead of CGI and Perl, are those of
simplicity and speed
- Provides security since your ASP code can not be viewed from the browser
- Since ASP files are returned as plain HTML, they can be viewed in
any browser
- Clever ASP programming can minimize the network traffic
What you need to Run ASP
ASP is available in multiple platforms. With microsoft
platforms (Windows 95, 98) you can use Microsoft Personal WebServer. For
Windows 2000, and NT you should Use Microsoft Internet Information Server.
What is ASP all about?
ASP stands for Active Server Pages. ASP is
a server side technology; this means it works on any web browser, because
all the work is done at the web server end.
Essentially ASP pages are
just normal HTML with scripts embedded in them. You can write your scripts
in VBScript, JavaScript or any language, which is Active Script compatible.
You indicate the start of a script with <%
and the end of a script with a %>. For example;
<% for a = 1 to 5 %>
<font size= <% = a %> > Hello World
</font> <br>
<% next %>
Will be executed by the server before the page is sent to the browser
Of course the real advantage of ASP is the
ability to use ODBC databases with your web pages.
How to format the text with HTML tags.
<html>
<body>
<%
response.write("<h2>Hello World!<br>This sentence uses
HTML tags to format the text!</h2>")
%>
</body>
</html>
Built in ASP Objects
The following built-in objects are used for request and response processing
and for managing a Web-application
- Application - to store application-wide information
- Session - to maintain information on a per user basis
- Request - information passed to the server from the browser
- Response - produce html and other information
- Server - Server functionallity for use with ASP
- Object Context - Allows to commit or abort translations
Scripting Languages in ASP
When we want to write our code in a scripting language, we have to
inform ASP about it. We can do this in 3 ways:
a) Web Site level: this will change the default scripting language
for all pages in the Web Site
b) File Level- to define the scripting language for a specific
page.
c) Functional level-defining the scripting language for a specific
function in the page.
The Web Server finds the ASP file, and processes all the ASP code between
<% ... %>.
Code between <% ... %> never arrives at the client, the viewer
never sees the code.
By default the scripting language tha ASP uses is VBScript. If you wish
to use another language, such as Jscript, you must let the server know
on what level you will be using another language other than VBScript.
This can be done by:
<% @ Language=ScriptingLanguage %>
Replace the ScriptingLanguage above with the name of the scripting language
you will be using on a specific page. I.e.,
<% @ Language=Javascript %>
You can also change the Function-Level and System-Level scripting languages
also, but please note that Perl is much more suited for use at the System
Level.
JavaScript
To use JavaScript as the default scripting language,
insert a language specification at the top of the page:
<%@ language="javascript" %>
<html>
<body>
<%
Response.Write("Hello World!")
%>
</body>
</html>
|
Note that - unlike VBScript - JavaScript is case sensitive.
You will have to write your ASP code with uppercase letters and lowercase
letters when the language requires it.
Other Scripting Languages
ASP comes with VBScript and JavaScript. If you want to
script in another language, like PERL, REXX, or Python, you have to install
scripting engines for them.
Because the scripts are executed on the server, the browser
that requests the ASP file does not need to support scripting.
It is important to know the behaviour of an asp page
when multiple scripting languages are used in server side scripting. In
an ASP page having Jscript and VBscript, code in Jscript block is executed
first then followed by code in asp tags and vbscript block respectively.
The following example makes the above concept clear:
For example
<script language = "vbscript" runat ="server">
Response.write("some HTML commands<br>")
</script>
<% Response.write "other HTML commands<br>" %>
<script language = "jscript" runat = "server">
Response.write("HTML Commands<br>")
</script>
In the above example first the jscript block is executed followed by
code in asp tag and vbscript block.When creating an ASP using VBscript
we should take care that the page is easy to maintain. For example the
browser must not cache the file or dsiplay a reloaded version after the
expiration date.
he following example shows how to display to the client the server time,
as well as the number of hours till the year 2003. You can also see date
and time functions.
<!-- Script from Sams Teach yourself ASP in 24 hours -->
<% @LANGUAGE = VBScript %>
<% ' Server Time
Option Explicit
Response.Expires = 0 ' in minutes
Dim dtmTime, dtmLater, dtmDiff
dtmTime = Time
dtmLater = DateAdd("h",1,dtmTime)
dtmDiff = DateDiff("h",Now,#1/1/2003#)
%>
<HTML>
<BODY>
Hello User.<BR>
The server's time is: <%= dtmTime %>.<BR>
In one hour the server's time will be: <%= dtmLater %>.<BR>
<%
If dtmDiff > 0 Then
Response.Write "Still " & dtmDiff & " hours "
Response.Write "to go till year 2000.<BR>" & VbCrLf
ElseIf dtmDiff < 0 Then
Response.Write "Already " & Abs(dtmDiff) & "
hours passed since "
Response.Write "the beginning of the year 2000.<BR>"
& VbCrLf
Else
Response.Write "START.<BR>" & VbCrLf
End If
%>
</BODY>
</HTML>
Option Explicit- when using this in our code we need to declare
every single variable in our code. This is good to do because if for
example, we mispell a variable, we would get a runtime error with the
message (variable undefined) when calling the script from the browser.
This statement actually tells the VBScript scripting engine that explicit
declaration will be done for all variables uding the Dim, Private, Public
or Redim statements. ( A runtime error occurs when the server-side
scripting contains errors. Make sure that you use Option Explicit
in every server side scripting before all other statements. After
you check and correct your script for errors you can remove Option
Explicit to improve performance, because run-time variable checks
should not be enforced.
As we can see in the above example, we use the response.expires.
This will tell the browser that the content of the page expires
after a certain amount of time. This is very usefull for pages that
get updated dynamically to overcome the capability of the browser to
load cached pages first for perfomrance reasons.
Response.Expires = 0 ' in minutes
also we can use
Response.ExpiresAbsolute = #January 1, 2003 00:00:00#
This assignement should be performed before any content is sent to
the browser through HTML code or some response.write, otherwise we would
get an error message (Response object error)
Creating Reusable Code Blocks
Even though ASP scripts are scripts, there are several approaches that
can be taken to modularize the source code and encapsulate complex tasks.
These include using server-side includes, taking advantage of VBScript
classes.
<%
Function SayHello(nTimes)
For i = 1 To nTimes
strTemp = strTemp & "Hello World!<br>"
Next
SayHello = strTemp
End Function
strHello = SayHello(10)
Response.Write strHello
%>
we can use this afterwards by saying in our script
<!--#include file="<directory>/files.asp"-->
We have 2 types of includes: Virtual and file includes.
By using the virtual include directive, the file included is absolute
to the root of the Website.
ie
<! - - # include virtual=''/inc/sample.inc'' - - >
This includes a file relative to the root under the subdirectory inc
We can't access an include file directly.
By using a file include, we are using a file relative to the directory
wher the include file resides.
<!- - # include file="inc/sample.asp"- ->
This means that this include file is in the directory inc which is
under the directory of the asp file calling this include
|