Getting the Physical path from virtual path by using Server Mappath object in ASPIn 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
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.
C:\Inetpub\wwwroot\my_files\my_files\mappath.aspWith or without “/” or “\” mark at the staring of the pathIf 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.
C:\Inetpub\wwwroot\my_files\mappath.asp
C:\Inetpub\wwwroot\test.aspNote 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.
C:\Inetpub\wwwroot\my_filesTo get the server root we have to use
C:\Inetpub\wwwrootServer.MapPath is used in different applications and it is useful object in ASP platform. Server.Htmlencode to display tags in ASPWe 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
The output of this is here.
Try to use only Response.Write without htmlencode and see the difference.
Maximum Execution time by Server.ScriptTimeoutScript 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.
We can change the above value like this.
By keeping this value at a lower level we can create some delays by writing some for loops.
This way we will create some delays and once it exceeds the ScriptTimeout value then we will get this error message.
Server Transfer to run another ASP fileThis 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.
Now here is the code for file2.asp file
Once we run file1.asp file we will get the output like this
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 fileWhile 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.
Now here is the code for file2.asp file
Once we run main.asp file we will get the output like this
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
This article is written by plus2net.com team.
Be the first to post comment on this article : |