Getting the Physical path from virtual path by using Server Mappath object in ASP

In VBScript used in ASP environment, MapPath method returns the physical path of the file or directory from the virtual path. As we setup a site in a remote server we don't have access to the server root path of our site in case of shared hosting. As our site is stored inside a particular folder of the server and by browser or by ftp access we can't move upward to wards the root of the server so it is not possible to get the physical path of any directory or file. Some of the objects like FileSystemObject requires physical path to handle various file and directory related commands. So here we can convert a virtual address of path to physical path by using Server MapPath method.

Let us try some examples where different conditions we will apply to see the results. We kept the file mappath.asp inside my_file directory. So our total physical path looks like this.

C:\Inetpub\wwwroot\my_files\mappath.asp

Note that the object Server.MapPath does not check the existence of actual path, it only maps the given path along with the physical path. So any error in writing directory name or file name will not result in any error message. To make this point clear we are starting with one example of test.asp which is not existing inside the my_file directory. Here is the command and the output
response.write(Server.MapPath("test.asp")
The output of the above line is here

C:\Inetpub\wwwroot\my_files\test.asp

Here we have kept the code inside mappath.asp file and it has displayed the above line. Now let us try with different files and names , the output of the code is given in italic font.
response.write(Server.MapPath("my_files/mappath.asp")
C:\Inetpub\wwwroot\my_files\my_files\mappath.asp

With or without / or \ mark at the staring of the path

If the file or directory name starts with / or \ then the path is taken as full virtual and it starts from server root. Otherwise it is considered from the path of the file being executed. Here are some examples with outputs shown in Italics.
response.write(Server.MapPath("/my_files/mappath.asp")
C:\Inetpub\wwwroot\my_files\mappath.asp
response.write(Server.MapPath("\test.asp")
C:\Inetpub\wwwroot\test.asp

Note the difference the \ mark at the staring of the file name has done. It has taken the root of the server even though the current script is executed inside my_files directory.

To get the root of the present script running we have to use like this.

response.write(Server.MapPath(".")
C:\Inetpub\wwwroot\my_files

To get the server root we have to use

response.write(Server.MapPath("\")
C:\Inetpub\wwwroot

Server.MapPath is used in different applications and it is useful object in ASP platform.

Server.Htmlencode to display tags in ASP

We can't post any HTML tags to the screen ( or to browser ) as browser will interpret then as tags and try to format the string accordingly without displaying the tag itself. To display the tag to web browser we have to use server object htmlencode.

Here is an example of htmlencode
Response.Write Server.htmlencode( "To highlight a string we have to add <b> Bold Tag </b>")
The output of this is here.
To highlight a string we have to add <b> Bold Tag </b>
Try to use only Response.Write without htmlencode and see the difference.

Maximum Execution time by Server.ScriptTimeout

Script execution time of any page can be set by using Server.ScriptTimeout value. This value by default is set to 90 seconds. We can read this maximum script execution time by this.
response.write(Server.ScriptTimeout)
We can change the above value like this.
Server.ScriptTimeout=10
By keeping this value at a lower level we can create some delays by writing some for loops.
Server.ScriptTimeout=1
response.write(Server.ScriptTimeout)

Dim i
For i=1 to 100000000 
Next 
Response.Write "<br>Done ...<br>"
This way we will create some delays and once it exceeds the ScriptTimeout value then we will get this error message.
Error Type:
Active Server Pages, ASP 0113 (0x80004005)
The maximum amount of time for a script to execute was exceeded. You can change this limit by specifying a new value for the property Server.ScriptTimeout or by changing the value in the IIS administration tools.
/a/server/scripttimeout.asp

Server Transfer to run another ASP file

This is one way transfer of script execution from main file to another file without returning to main file again. ( Returning to main file again happens in Server.Execute method ). You can also read how Response.Redirect works similar to server.tranfer. Here is the code for file1.asp file.
<html><head>
<title>(Type a title for your page here)</title>
</head>
<body >

<%
Dim var1
var1=" My name is plus2net "
Response.Write " I am inside file1.asp file " 
Response.Write " <br>---<br> "
Server.Transfer("file2.asp")
Response.Write " <br>---<br> "
Response.Write " I am back in main.asp file " & var1
%>

</body>
</html>
Now here is the code for file2.asp file
<%
Response.Write " I am in file2.asp inside "  
%>
Once we run file1.asp file we will get the output like this
I am inside file1.asp file 
---
I am in file2.asp inside
Here the main difference between server.execute and server.transfer is in case of server transfer the control does not return to main file again. In case of server.execute the script control returns to main file after executing the inner ( or file2.asp) file.

Server Execute to run another asp file

While running one .asp page we can transfer the script execution to another page by using server.execute command. For our example we will use two files. One is main.asp and other one is file2.asp. Here is the code for main.asp file.

<html><head>
<title>(Type a title for your page here)</title>
</head>
<body >

<%
Dim var1
var1=" My name is plus2net "
Response.Write " I am inside main.asp file " 
Response.Write " <br>---<br> "
Server.Execute("file2.asp")
Response.Write " <br>---<br> "
Response.Write " I am back in main.asp file " & var1
%>

</body>
</html>
Now here is the code for file2.asp file
<%
Response.Write " I am in file2.asp inside "  
%>
Once we run main.asp file we will get the output like this
I am inside main.asp file 
---
I am in file2.asp inside 
---
I am back in main.asp file My name is plus2net 
You can see the control has shifted to file2.asp file but after its execution again it returned to main.asp page. This is the main difference between server.execute and server. transfer command in ASP

Be the first to post comment on this article :

plus2net.com




Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer