Server Object - Final Example

<% @ Language="JavaScript" %>

<HTML>
<
HEAD><TITLE>Server Object - Final Example</TITLE></HEAD>

<BODY>

<%

var PathInfo = Request.ServerVariables("PATH_INFO") // gives us the virtual path of this file

/*
Server.MapPath(PathInfo) translates a virtual path to a physical path.
/javascript/week10/asp/escher.asp is actually s:\classes\javascript\week10\asp\escher.asp

In order to read or write to a file we need to know the physical path.
*/

// here we write out the physical path of the file
Response.write(Server.MapPath(PathInfo) + "<BR>" + "<BR>")

var fsObj = new ActiveXObject("Scripting.FileSystemObject");
var txtFile = fsObj.OpenTextFile(Server.MapPath(Request.ServerVariables("PATH_INFO")));

/*
Read through the file line by line. The while loop tells the program to continue until the end of the file is reached.

ReadLine() reads from the file line by line while Server.HTMLEncode(txtFile.ReadLine()) automatically encodes all characters (ö, ñ, etc...) for us
*/

while (!txtFile.AtEndOfStream) { // while not at the end of the file read the next line
     Response.write(Server.HTMLEncode(txtFile.ReadLine()) + "<BR>");
}

%>

</BODY>

</HTML>