ASP optimization: ASP Coding Optimization Tips 8

Source: Internet
Author: User
Tags dsn urlencode

ASP (Active Server Page) is an ISAPI-based (Personal Web server) &iis (Internet Information Server) platform that is launched by Microsoft Corporation INTERNETSERVICEAPI) Principle of Dynamic Web page development technology, is increasingly mature and perfect. Here are just a few simple discussions on code optimization.

1. Declaring VBScript variables

In ASP, VBScript provides strong support for the seamless integration of VBScript functions and methods, which provides a great convenience for extending the existing functionality of ASP. Since the concept of variable types has been blurred in ASP, many programmers are wont to declare VBScript variables in the process of interaction between ASP and VBScript, thus aggravating the server's parsing burden and affecting the server's response request speed.

In this case, we can force the user to declare the variable in VBScript just like in VB, forcing the user to make a variable declaration. The implementation method is to place <% option explicit%> at the beginning of the ASP program.

2. Encode the URL address

When we use ASP to dynamically generate a URL address with a parameter and jump, it is normal to parse in IE, but there are errors in Netscrape browsing as follows:

HTTP Error 400

Request

Due to malformed syntax, the request could not being understood by the server.

The client should not repeat the request without modifications.

The workaround is to URL-encode the generated URL parameter using the UrlEncode method of the ASP built-in server object, as in the following example:

<%

Url= "Xur.asp"

Var1= "username=" & Server. UrlEncode ("Xur")

Var2= "&company=" & Server. UrlEncode ("Xurstudio")

Var3= "&phone=" & Server. UrlEncode ("021-53854336-186")

Response.Redirect URL & "?" & Var1 & var2 & VAR3

%>

3. Clear objects

When you have finished using the object, first use the Close method to free the system resources that the object occupies, and then set the object value to "nothing" to free up the object's memory. That's when I crashed my IIS on a page that created a hundred without emptying the object's recordset. The following code builds a drop-down list using the database content. The code examples are as follows:

<% mydsn= "Dsn=xur;uid=xur;pwd=xur"

Mysql= "SELECT * from authors where au_id<100"

Set Conntemp=server.createobject ("Adodb.connection")

Conntemp.open MyDSN

Set Rstemp=conntemp.execute (MySQL)

If Rstemp.eof Then

Response.Write "Database is empty"

Response.Write MySQL

Conntemp.close

Set conntemp=nothing

Response.End

End if%>

<%do until Rstemp.eof%>

<%

Rstemp.movenext

Loop

Rstemp.close

Set rstemp=nothing

Conntemp.close

Set conntemp=nothing

%>

4. Creating SQL queries using strings

Using a string to establish a query does not speed up the parsing of the server, but it also increases the parsing time of the server. However, it is still recommended to use strings instead of simple query statements to query. The advantage of this is that you can quickly identify the problem with your program, which facilitates efficient generation of your program. Examples are as follows:

<%mysql= "" SELECT * "

Mysql= MySQL & "From publishers"

Mysql= MySQL & "where state= ' NY '"

Response.Write MySQL

Set Rstemp=conntemp.execute (MySQL)

Rstemp.close

Set rstemp=nothing

%>

5. Use Case for conditional selection

When making a conditional selection, try to use case statements and avoid using the IF statement. With case statements, you can make your program flow and execute faster than an if statement. Examples are as follows:

<%

For i = 1 to 1000

n = i

Response.Write Addsuffix (N) & "
"

NEXT

%>

<%

Function Addsuffix (num)

Numpart = Right (num,1)

SELECT Case Numpart

Case "1"

IF InStr (num, "one") then

num = num & "th"

ELSE

num = num & "St"

END IF

Case "2"

IF InStr (num, "a") then

num = num & "th"

ELSE

num = num & "nd"

END IF

Case "3"

IF InStr (num, "+") Then

num = num & "th"

ELSE

num = num & "rd"

END IF

Case "4"

num = num & "th"

Case ELSE

num = num & "th"

END SELECT

Addsuffix = num

END FUNCTION

%>

6. Open the recordset using constants defined in the Adovbs.inc file

When you open a recordset, you can define the type of cursor and the type of lock that the recordset opens. Some constants are defined in the Adovbs.inc file to define these types. The Adovbs.inc file is saved under the \inetpub\iissamples\iisamples directory. Several common cursor types and locking types are listed below.

Cursor Type: ADOPENFOWARDONLY cursor can only forward; the adOpenKeyset cursor can be forward or backward, as if a user added a record, the new record does not appear in the Recordset; adOpenDynamic cursor is dynamically arbitrary The adOpenStatic recordset does not reflect the record modifications caused by other users.

Lock type: Adlockreadoney cannot modify records in a Recordset, adlockpessimistic locks a record when it is edited, adlockoptimstic locks the record when the recordset Update method is called ; Adlockbatchopeimstic records can only be updated in batches.

The!--#INCLUDE virtual= "/adovbs. INC "-->

<%

Connectme= "Dsn=xur;uid=xur;pwd=xur"

Sqltemp= "SELECT * From Publishers where name= ' Xur '"

Set Rstemp=server.createobject ("ADODB. Recordset ")

Rstemp.open sqltemp, Connectme, adopenstatic,adlockoptimstic

Response.Write Rstemp.recordcount & "Records in
" & Sqltemp

Rstemp.close

Set rstemp=nothing

%>

7. Avoid object definition in the use of global.asa files

Since the contents of the Global.asa file can be referenced for all the files within the site, there is no doubt that the object definition in the Global.asa file can save a lot of duplication of effort. For example, the Application_OnStart function in Global.asa is defined as follows:

<%sub Application_OnStart

Set Application ("Theconn") =server.createobject ("Adodb.connection")

END SUB%>;

This makes it possible to make similar references in any code in the site:

<%

Mysql= "SELECT * From Publishers where state= ' Xur '

Set Rstemp=application ("Theconn"). Execute (MySQL)

%>

Similarly, you can create a Recordset object in the Session_OnStart function

<%sub Session_OnStart

Set session ("Rstemp") =server.createobject ("Adodb.recordset")

END SUB%>

The following references are then made in the site also polygon:

<%

Mysql= "SELECT * From Publishers where state= ' Xur '

Set session ("Rstemp") =conntemp.execute (MySQL)

%>

But this also has a great negative impact, because application and session variables are only when the site is closed to release the resources used, so the session parameter will waste a lot of unnecessary memory, and at this time the application variable becomes the bottleneck of server performance.

Workaround: Create an ASP page that defines the object and, on the page where the objects need to be called, introduce this ASP page. Assuming that the ASP page name for the definition object is define.asp, the page can be introduced as long as the following statement is added to the corresponding ASP page.

!--#INCLUDE virtual= "/define.asp"-->

In the introduction of the page, it is best to not include <% @LANGUAGE = "VBSCRIPT"%> statement in the ASP file to be introduced. Because in an ASP file, there can only be one sentence defined by the @ Script resolution language.

8. Safety protection

ASP provides a good code protection mechanism, all ASP code is executed on the server side and only return to the client code execution results. Even so, in the old version of IIS can also be in the back of the file name:: $DATA to view the source code of the ASP, which is already in the Web server security category is not covered in this article. Two simple safety precautions are presented below.

Although it is recommended to introduce the file with Inc as the extension in ASP, it is still recommended to use ASP as the extension of the citation file. When the code runs on a poorly secured Web server, simply enter the address of the introduction file on the Address Bar (inc is the extension) to browse the contents of the ingest file, because on the Web server, if there is no dynamic connection library defined to parse a type (such as Inc), The file is displayed in Source mode.

Shanghai treatment of impotence hospital procedures to remind readers, do not put the database files inside the structure of the site, so that when the malicious person to obtain the database path, it can easily obtain the database, and then arbitrarily change the database content. It is a good practice to establish a DSN (Date Source Name) for the database, and to access the DSN directly during database access.

ASP optimization: ASP Coding Optimization Tips 8

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.