Programming | Advanced 4.3.5 get path information for the server object
When you work with files that are stored on a Web site, you need to get the actual physical path to the file, rather than using a virtual path or URL, although you can use them to properly locate files in other pages. In the next chapter, there is an example that uses FileSystemObject to read and write files in the Inetpub\Wwwroot folder of a Web site. When you create your own custom component or use a commercialized component to access a file system, you often need to provide it with a physical path to the file.
MapPath method for the server object
You can extract HTTP header variables from the Request.ServerVariables collection that contain the physical path of the current file (in the Document_name and path_translated headers). The server object provides a method MapPath that can be used to extract the appropriate physical path for any file that we can provide a valid virtual path to. You can see the use of the MapPath method in a sample Web page that you've already used, and you can experiment with that method.
As shown in Figure 4-24, in the Miscellaneous methods section at the bottom of the page, there is a button that executes the Server.MapPath method and provides the value in the text box near the button. This value has been set to "/iishelp/default.htm" in the source code of the page, and this file should be automatically installed on the computer. You can also enter a URL for another Web page.
Figure 4-24 the screen using Server.MapPath
Click the button to reload the page, execute the method, and display the results at the top, showing the remainder of the original page in the lower part, as shown in Figure 4-25:
Figure 4-25 shows the results of the Server.MapPath
(1) The function of the sample Web page code
The code that handles this process is very similar to the code that has been used in similar sample files before.
In the ASP script area at the top of the page, check the name of the button you clicked. In this case, the button's name will be Cmdmappath, simply passing the value in the matching text box Txtmappath to the Server.MapPath method and displaying the resulting result:
If Len (Request.Form ("Cmdmappath")) Then
strvalue = Request.Form ("Txtmappath")
Response.Write "<b>results:</b><br>server.mappath (" & QUOT & strvalue _
& QUOT & ") returned <B>" & QUOT & Server.MapPath (strvalue) _
& QUOT & "</B><HR>"
End If
(2) MapPath and virtual application directory
Note that the results obtained by the MapPath method for the/iishelp/default.htm file are outside the Web server directory and in the Help directory of the main Winnt directory. This clearly proves that the MapPath method is very useful.
For files in the default Web site directory, the path portion of the URL is usually the same as the physical path. For example, a file is stored on a Web server:
C:\InetPub\WWWRoot\yourfiles\thisfile.asp
If the Web root is already installed in the default directory at installation time, the URL is as follows:
Http://yoursite.com/yourfiles/thisfile.asp
However, the IIS Help file is installed in a virtual directory outside the default Web site directory, so there is no direct association between the URL used to access it and the physical path. The real physical path can be obtained only by using the Server.MapPath method.
4.3..6 format data using the server object
When you discuss the code for a Web page that demonstrates an SSI directive, you happen to encounter an old problem with HTML. How do I display HTML code in an HTML page? If used "as it is", that is, using all the HTML characters in the appropriate position, it will be interpreted and executed by the browser as HTML. This occurs when the following content is displayed in the browser:
This is the syntax of a <TABLE> element:
Text <table> will not be displayed, because the browser takes it as a start tag for a datasheet and executes it. To avoid this situation, all characters that are illegal or invalid in HTML must be converted to an equivalent HTML character entity (character entity). Most common character Furu are shown in table 4-5:
Table 4-5 relationships between characters and equivalent HTML entities
Characters
Equivalent HTML entity
Characters
Equivalent HTML entity
Figure 4-26 Screen using the HTMLEncode method
When you reload the page, the results are displayed at the top of the page. The HTMLEncode method converts angle brackets to "<" and ">" and converts the double quotes into ";" as shown in Figure 4-27:
Figure 4-27 Using HTMLEncode Method 1
(1) The function of the sample Web page code
There are a few interesting places to get the results.
First, the <B> and </B> tags have been discarded in parentheses behind the method name, and a bold text section has been added accordingly. ,<b> and </B> are submitted as HTML when the original values are displayed in the Web page, so the <B> and </B> tags disappear and the related content is shown in bold text.
This situation can be avoided very easily. In fact, this is the reason for designing HTMLEncode methods. The original sample code is as follows:
...
Response.Write "Server.HTMLEncode (" & QUOT & strvalue & QUOT & ") returned"
...
All you can do now is apply the HTMLEncode method to the value you are outputting:
...
strresult = Server.HTMLEncode (strvalue)
Response.Write "Server.HTMLEncode (" & QUOT & strresult & QUOT & ") returned"
A very useful result is now exported, as shown in Figure 4-28:
Figure 4-28 Results of using the HTMLEncode Method 2
You have now solved the problem of displaying HTML without submitting HTML. But what happens if you want to display the results of the HTMLEncode method in HTML without submitting and processing the results? In order to solve this problem, we should start from htmlencod
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.