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>"
(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:
If the Web root is already installed in the default directory at installation time, the URL is as follows:
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
Equivalent HTML entity
Equivalent HTML entity
All entities begin with the & and end with a semicolon, as part of a standard method of demonstrating an entity in some languages, which is based on the SGML (standardized general identifier language) rule, such as the HTML language.
1. HTML entity equivalent string for numbers
Note the last example, the registered Trademark ® is a numeric value prefixed with the "#" character, rather than a text abbreviation for the corresponding meaning (e.g. copy corresponds to the copyright symbol ©). ANSI code that has an ANSI code value greater than 126 that is represented as a decimal character in HTML, in & #为前缀, with a semicolon suffix.
In fact, it is worth noticing that using numeric entity equivalent strings takes precedence over some of the less supported text entity strings. An example is a trademark character (™), where the entity equivalent string of the character is "™" but not all browsers (such as navigator) recognize the string, in which case the entity string is displayed in the Web page. In contrast, using ™ works well in all browsers.
2. HTMLEncode method for the server object
Converting HTML to text is required for effective display, otherwise HTML is treated and executed by the browser as HTML, which means that invalid characters must be encoded so that they become equivalent HTML entity strings. To manage this transformation, the server object provides a HTMLEncode method. You can practice using this method in the ASP Server Object Web page provided in this book.
Simply enter some text into the HTMLEncode corresponding text box and click the button. The example provides some real HTML as the default text, as shown in Figure 4-26:
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