Response Object
In previous lessons we have used response object to send output to web browser.
Response.write "<html><body>Hello World!</body></html>"
In this lesson we will cover more methods and usages of response object.
Redirection
Sometimes you want to redirect users arriving at a page to another URL. This is easily possible with ASP. We will use "redirect" method of response object for this purpose.
Response.redirect "http://www.yahoo.com"
For example lets assume you want to send different people to different pages according their login information. In this case you can use redirection.
In our first example we will assume that we have two groups of people who will login to our system, students and
teachers.
For ease of programming we will assume user names starting with the letter "s" are students and usernames starting with the letter "t" are teachers.
Again we will consider the word "student" as default password of students and "teacher" as default password for
teachers.
Example 7-1:
login.html:
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="login.asp" method="post">
User Name: <input type="text" name="username"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" value="Enter">
</form>
</body>
</html>
login.asp:
<%
username=Request.Form("username")
pass=Request.Form("pass")
if left(username,1)="s" and pass="student" then
response.redirect "students.html"
end if
if left(username,1)="t" and pass="teacher" then
response.redirect "teachers.html"
end if
response.redirect "nologin.html"
%>
students.html:
<html>
<head>
<title>Students Page</title>
</head>
<body>
<br><br><br>
<center><h2>You have logged in Students section of website
!</h2></center>
</body>
</html>
teachers.html:
<html>
<head>
<title>Students Page</title>
</head>
<body>
<br><br><br>
<center><h2>You have logged in Teachers section of website
!</h2></center>
</body>
</html>
nologin.html:
<html>
<head>
<title>Login incorrect</title>
</head>
<body>
<br><br><br>
<center>
Your login information is not correct!<br>
<br>
Wither click <a href="login.html">here</a> to return to
login page,<br>
Or click <a href="register.html"> here</a> to go to
registration page.
</center>
</body>
</html>
register.html:
<html>
<head>
<title>Login incorrect</title>
</head>
<body>
<br><br><br>
<center>
Please send your requests for registration<br>
<br>
to our administration email box.
</center>
</body>
</html>
Before we end this section, let me warn you that pages that redirect users to another page must do this redirection before sending anything to user's browser. This means page cannot contain html codes etc. before redirection code.
Proxy servers and caching
Many of Internet service providers use cache and proxy servers to increase their performance and security. When
someone browse main page of yahoo website, it can be retrieved from your website, Temporary cache of browser or ISP proxy server or cache.
This will improve performance, as it is not needed to use expensive Internet bandwidth for retrieving the same page for hundreds of times everyday. After retrieving a static page for the first time proxy server and browser cache will deliver cached copy instead of downloading a fresh copy. Internet bandwidth is expensive in most parts of the world yet.
However sometimes we need to avoid caching a page. For example if you have a dynamic page such as a web email box you will want to avoid caching pages. Otherwise new users will see pages retrieved by other users instead of fresh copies, which belong to their own web mail account.
In fact users will see a constant page instead of dynamic pages created at server. To avoid caching a page in browser, you must modify header of asp page.
You can determine the expiration time for each page in seconds.
Example 7-2: expire.asp
<%response.expires=0%>
<html>
<head><title>Expiration Test</title></head>
<body>
This page is expired immediately.
</body>
</html>
Discussed method works on a web browser only. This will not control how they are cached in proxy servers.
If you want to avoid caching a page in proxy servers you must use "cachecontrol" property of "response" object.
Example 7-3 public.asp
<%response.cachecontrol="PUBLIC"%>
This page will not be cached in proxy server
<%count=Application("counter")
count=count+1
application("counter")=count
%>
If you put the code on a remote server over the Internet and browse it behind a proxy server you will see that it will refresh with new counter value each time you visit the page.
Request Object
In previous lessons we used form and querystring methods of retrieving form data sent by browser. In this lesson we will introduce other methods and properties of request method.
Server Variables
When we use a browser to request a page from server, a lot of information is sent to server along with page filename.
As an example, we can mention browser and operating system types. This information is stored in "Server Variables".
In addition to browser information some other variables related to server itself are stored in server variables.
Application physical path and running script name are among the server variables, which are accessible by asp program.
We can read the value of each server variable using "ServerVariable" method of the request object.
HTTP_REFERER
Here we have written an example to extract the value of HTTP_REFERER server variable and copy it to "referrer"
variable.
When someone clicks on a link on the Internet to visit your page, this variable holds address of the page, which has sent the user to our page. If you want to track websites that send you traffic you can see the value in this
variable.
REMOTE_ADDR
If you want to know IP address of the client computer (which browser is running on it) then you can use this server variable.
Addr=request.ServerVariables("REMOTE_ADDR")
Example :
track.asp:
<html>
<head><title>Referer and IP address</title></head>
<body>
You are coming here by clicking on the page:
<%=request.ServerVariables("HTTP_REFERER")%><br>
Your current IP address is
<%=request.ServerVariables("REMOTE_ADDR")
</body>
</html>
In this lesson we tried to cover remaining important methods and properties of response object. We also studied a new and important method of request object.
Exercises
1- Create a html login page with the filename "login.html" login.asp is another file which will process data submitted by login.html
login.asp will store username in a session variable and will contain a main menu.
Menu must include a logout link which points to a logout.asp file. This file will clear the username session variable and will contain a link to login page. Prepare html and asp files for described application.
2- We have 3 affiliate web sites which have put a link on their web sites to send visitors to our site. They have put the links on these pages.
http://site1.com/links.html
http://site2.com/directory.html
http://site3.com/sites.html
Create three counter variables, One for each web site. Create an asp file to show results of our affiliate program. (number of visitors coming from other sites)
Return to Index Page
|