ASP (Active Server Page) is a dynamic web page development technology based on the PWS (Personal Web Server) & IIS (Internet Information Server) platform launched by Microsoft, which is now becoming more mature and perfect. Here we have only some simple discussions on code optimization.
8 Asp coding optimization techniques:
1. Declare VBScript variables
In ASP, vbscript is provided with strong support and can seamlessly integrate vbscript functions and methods, which provides great convenience for extending the existing functions of ASP. Since the concept of variable types has been blurred in ASP, many programmers are accustomed to not declaring vbscript variables during the interaction between ASP and vbscript, which increases the parsing burden of the server and thus affects the server's response request speed .
In view of this, we can force the user to perform variable declarations in vbscript just like we force the user to perform variable declarations in VB. The implementation method is to place <% option explicit%> at the beginning of the ASP program line.
2. Encode the URL address
When we use asp to dynamically generate a URL address with parameters and jump, it is normal to parse in IE, but when browsing NetScrape, there are errors as follows:
- HTTPError400
- 400BadRequest
- Duetomalformedsyntax, therequest could not beunderstory by theserver.
- Theclient should notrepeattherequestwithoutmodifications.
The solution is to use the URLencode method of ASP built-in server object to URL encoding the generated URL parameters. The example is as follows:
- <%
- URL=xur.asp
- var1=username=&server.URLencode(xur)
- var2=&company=&server.URLencode(xurstudio)
- var3=&phone=&server.URLencode(021-53854336-186)
- response.redirectURL&?&var1&var2&var3
- %>
3. Clear the object
After using the object, first use the Close method to free up the system resources occupied by the object; then set the object value to nothing to free up the object to occupy memory. Back then, I crashed my IIS by creating more than 100 recordsets on a page with no clear objects. The following code uses the database content to create a drop-down list. The code example is as follows:
- <%myDSN=DSN=xur;uid=xur;pwd=xur
- mySQL=select*fromauthorswhereAU_ID<100
- setconntemp=server.createobject(adodb.connection)
- conntemp.openmyDSN
- setrstemp=conntemp.execute(mySQL)
- ifrstemp.eofthen
- response.write database is empty
- response.writemySQL
- conntemp.close
- setconntemp=nothing
- response.end
- endif%>
- <%dountilrstemp.eof%>
- <%
- rstemp.movenext
- loop
- rstemp.close
- Setrstemp=nothing
- conntemp.close
- setconntemp=nothing
- %>
4. Create SQL query using strings
Using strings to establish queries does not speed up the server's parsing speed. On the contrary, it will also increase the server's parsing time. But it is still recommended to use strings instead of simple query statements to query. The advantage of this is that it can quickly discover the problem of the program, thereby facilitating and efficiently generating the program. Examples are as follows:
- <%mySQL=select*
- mySQL=mySQL&fromempublishers
- mySQL=mySQL&wherestate='NY'
- response.writemySQL
- setrstemp=conntemp.execute(mySQL)
- rstemp.close
- Setrstemp=nothing
- %>
5. Use case to select the condition
When making conditional selection, try to use case statements and avoid using if statements. Using case statements can make the program flowable and execute faster than if statements. Examples are as follows:
- <%
- FORi=1TO1000
- n=i
- Response.WriteAddSuffix(n)&<br>
- NEXT
- %>
- <%
- FunctionAddSuffix(num)
- numpart=RIGHT(num,1)
- SELECTCASEnumpart
- CASE1
- IFInStr(num,11)THEN
- num=num&th
- ELSE
- num=num&st
- ENDIF
- CASE2
- IFInStr(num,12)THEN
- num=num&th
- ELSE
- num=num&nd
- ENDIF
- CASE3
- IFInStr(num,13)THEN
- num=num&th
- ELSE
- num=num&rd
- ENDIF
- CASE4
- num=num&th
- CASEELSE
- num=num&th
- ENDSELECT
- AddSuffix=num
- ENDFUNCTION
- %>
6. Use constants defined in adovbs.inc file to open the record set
When opening a record set, you can define the cursor type and lock type that the record set opens. There are some constants defined in the adovbs.inc file to define these types. The adovbs.inc file is saved in the /inetpub/iissamples/IISamples directory. Below are a few commonly used cursor types and lock types.
Cursor type: adOpenFowardOnly cursor can only move forward; adOpenKeyset cursor can move forward or backward. If a user adds a record, new records will not appear in the record set; adOpenDynamic cursor is dynamic and random; adOpenStatic record set does not modify records caused by other users. reflect.
Lock type: adLockReadOney cannot modify records in the record set; adLockPessimistic locks it when editing a record; adLockOptimstic locks the record set Update method only when adLockBatchOpeimstic records can only be updated in batches.
- <!--#INCLUDEVIRTUAL=/ADOVBS.INC-->
- <%
- connectme=DSN=xur;uid=xur;pwd=xur
- sqltemp=select*frommpublisherswherename='xur'
- setrstemp=Server.CreateObject(adodb.Recordset)
- rstemp.opensqltemp, connectme, adOpenStatic, adLockOptimstic
- response.writerstemp.recordcount&recordsin<br>&sqltemp
- rstemp.close
- Setrstemp=nothing
- %>
7. Avoid object definition in the global.asa file
Since the contents in the global.asa file can be referenced by all files in the site, undoubtedly, defining objects in the global.asa file can save a lot of duplication. For example, in the application_onstart function in global.asa, the following definition is made:
- <%SUBapplication_onstart
- setapplication(theCONN)=server.createobject(adodb.connection)
- ENDSUB%>;
This allows you to make similar references in any code on the site:
- <%
- mySQL=select*frommpublisherswherestate='xur'
- setrstemp=application(theconn).execute(mySQL)
- %>
Similarly, the record set object can be created in the session_onstart function
- <%SUBsession_onstart
- setsession(rstemp)=server.createobject(adodb.recordset)
- ENDSUB%>
Then, the following quotes are made in the site:
- <%
- mySQL=select*frommpublisherswherestate='xur'
- setsession(rstemp)=conntemp.execute(mySQL)
- %>
However, doing this also has a great negative impact. Since both Application and session variables only release the occupied resources when closing the website, the session parameters will waste a lot of unnecessary memory, and at this time the application variable becomes a bottleneck in server performance. .
Solution: Create an asp page to define objects, and introduce this asp page on the page where these objects need to be called. Assuming that the asp page name of the defined object is defined.asp, the page can be introduced by adding the following statement to the corresponding asp page.
When introducing pages, it is best not to include <%@LANGUAGE=VBSCRIPT%> statement in the asp file to be introduced. Because in the asp file, there can only be one script parsing language defined by @.
8. Safety protection
Asp provides a good code protection mechanism, and all asp code is executed on the server side and only returns the result of the client code execution. Even so, in the old version of IIS, you can view the source code of asp after the file name::$DATA, which already falls within the scope of Web Server security and is not within the scope of this article. The following are two simple safety precautions.
Although it is recommended to introduce files with inc asp as the extension, it is still recommended to use asp as the extension to introduce files here. When these codes run on a Web Server with poor security mechanism, you can browse the contents of the imported file by simply entering the address bar (inc is the extension). This is because on Web Server, If the dynamic connection library of a certain type (such as inc) is not defined, the file is displayed in source code.
Do not put the database file inside the website structure, so that when a malicious person obtains the database path, he can easily obtain the database and then change the database content arbitrarily. A better approach is to establish a DSN (Date Source Name) for the database, and access the DSN directly when accessing the database.
Each of the above eight Asp coding optimization techniques is very important and requires everyone to experience it carefully and truly understand it to become their own thing.