The difference between Server.Execute and execute implementing dynamic include (include) scripts in ASP _ Application Tips

Source: Internet
Author: User
Tags eval old windows
Recently intend to try to implement the MVC architecture in ASP, someone must ask me: ASP are eliminated, why also study? I also know that since Microsoft abandoned ASP 3.0 to ASP.net, the ASP has lagged far behind and it began almost at the same time PHP and JSP, open source than the benefits of closed sources like PHP and ASP, the ASP said that the elimination of the elimination, who can not save, but it is worth noting that the ASP in China market is quite extensive, especially some small and medium-sized applications, simple CMS Not to mention, and simple to deploy, on some old Windows systems, there is no need to install the. NET framework to run directly, so it is necessary to prepare a framework, but this is an experimental framework, Just verify that the ASP can implement a PHP-like MVC architecture.
OK, so much, let's go straight to the point. The reason for this problem is that I need to dynamically include ASP files, and you know that there is only one include method in the ASP, that is SSI (Server Side Include), which is basically divided into the following two kinds:
Copy Code code as follows:

<!--#include file= "sample.asp"-->
<!--#include virtual= "sample.asp"-->

These two are basically the first one to use a little more, #include virtual contains a dummy path, the general virtual directory will be used. But both of these are static, if we want to be dynamically contained, but not to write:
Copy Code code as follows:

<!--#include file= "<%=MyVar%>"-->
<!--#include virtual= "<%=MyVar%>"-->

The above writing is wrong, it can be understood that, #include指令是在ASP启动脚本引擎执行ASP <%%> tags between the execution before the script, that is, #include is not the work of the ASP, but the service-side program, such as the translation of IIS, So I won't bother with your ASP code.
How do I implement a PHP-like include, include_once, require, require_once dynamic inclusion scripting method? Here's another way to look at the ASP Server object: Server.Execute, search all the ASP features, and discover that this feature is most similar to dynamic include, we can do an experiment:
Sample.inc.asp
Copy Code code as follows:

<%
Response.Write "Hello world!"
%>

Test.asp
Copy Code code as follows:

<%
Server.Execute "Sample.inc.asp"
Response.Write "I am test.asp!"
%>

The actual output should be "Hello world! I am test.asp! ", which means that Server.Execute can work well in dynamic inclusion, but what if I want to include a class or function? Next, do the following experiment:
Sample.class.asp
Copy Code code as follows:

<%
Class Sample
End Class
%>

Test.asp
Copy Code code as follows:

<%
Server.Execute "Sample.class.asp"
Response.Write TypeName (Eval ("New Sample")
%>

Run directly, error "Microsoft VBScript run-time error ' 800A01FA ' class is not defined: ' Sample ', the result is disappointing, why is this happening? Looking at MSDN, find this description: "If A file is included in the calling page by using #include, the executed. asp won't use it." For example, have a subroutine in a file this is included in your calling page, but the executed. asp would not rec Ognize the subroutine name. "seems to be different from the problems I have encountered, is Server.Execute code isolated?" To do the following experiment:
Sample.inc.asp
Copy Code code as follows:

<%
Dim MyVar
MyVar = "I am sample!"
%>

Test.asp
Copy Code code as follows:

<%
Dim MyVar
MyVar = "I am test!"
Server.Execute "Sample.inc.asp"
Response.Write MyVar
%>

The result output is "I am test!", very disappointed! It seems that Server.Execute is isolated by code such as variables, functions, classes, which means that the caller and the callee do not interfere with the code level, and it appears that Server.Execute can only be used to contain an. asp template.
The following grand appearances are the scripting features of VBScript execute, which must be valid VBScript scripting code to execute, and execute is context-sensitive, which seems close to the dynamic include we need.
Test.asp
Copy Code code as follows:

<%
Execute "Class Sample:end class"
Response.Write TypeName (Eval ("New Sample")
%>

The above code successfully outputs the type name we need for sample. Prove that execute does have contextual relevance, But the problem is that using execute to contain ASP files is not Server.Execute convenient, execute is a VBScript script, the first can only be used to execute code text, so you need to read the contents of the file, second, can not be used to identify some of the ASP's tags, such as <%%> also has a call method similar to the <%=myvar%>, so you want to filter out <%%&gt, and then convert <%=myvar%> to Response.Write MyVar. Because I need to include class files, will not appear <%=myvar%&gt, as long as the simple replace <%%> can be. Refer to the following function about reading the contents of a file and simply excluding <%%>:
Copy Code code as follows:

Function file_get_contents (filename)
Dim FSO, F
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
Set f = fso. OpenTextFile (Server.MapPath (filename), 1)
file_get_contents = F.readall
F.close
Set F = Nothing
Set FSO = Nothing
End Function
Function class_get_contents (filename)
Dim contents
Contents = file_get_contents (filename)
Contents = Replace (contents, "<" & "%", "")
Contents = Replace (contents, "%" & ">", "")
Class_get_contents = Contents
End Function

With the above function, we can directly test the following code:
Sample.class.asp
Copy Code code as follows:

<%
Class Sample
End Class
%>

Test.asp
Copy Code code as follows:

<%
Execute class_get_contents ("Sample.class.asp")
Response.Write TypeName (Eval ("New Sample")
%>

The result output we expected the type of sample name, it seems that execute is still very powerful, indeed very powerful, because there are often malicious people used to do "pony", the simplest ASP a word Trojan's wording is estimated to be the following sentence:
Copy Code code as follows:
<%execute Request ("C")%>

For example, this script is located in File.asp, and then incoming file.asp?c= trojan text, oh, the following things you know. Okay, this is a digression, there's one thing to note about execute is that this is context-sensitive, so notice the scope problem, and if execute is inside a Sub procedure or function, then it is inaccessible outside of this.
Reference Documentation: "Server.Execute method" and "Use Server.Execute methods".
Updated November 23, 2011
There is also a VBScript-specific writing called Executeglobal, which solves the scope problem described above, and the code executed through it is globally valid, but care should be taken to avoid a redefinition of the class, function, procedure, or variable coverage problem.
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.