1. Basic knowledge of asp
1. Asp is the abbreviation of Active Server Pages, an interpreted scripting locale;
2. Asp requires the Windows operating system to run, and PWS needs to be installed under 9x; while NT/2000/XP requires the installation of Internet Information Server (IIS for short);
3. The script tags for Asp and jsp are <%%>, while for php, you can set them to multiple types;
4. Asp's comment symbol is ';
5. Use add-ons to extend the functions of Asp.
from www.knowsky.com
example:
HelloWorld_1.asp
<%=Hello,world%>
Effect:
Hello,world
HelloWorld_2.asp
<%
for i=1 to 10
response.write Hello,world
next
%>
Effect:
Hello,world
Hello,world
Hello,world
Hello,world
Hello,world
Hello,world
Hello,world
Hello,world
Hello,world
Hello,world
Note: Asp is case-insensitive; variables can be used without definition, which is convenient to convert; syntax check is very loose.
2. Use of Asp built-in objects:
Any ASP built-in objects below can be used without having to specifically declare them in the ASP script.
1. Request:
Definition: This object can be used to access request information sent from the browser to the server. This object can be used to read the information of the entered HTML form.
set:
Cookies: The value containing browser cookies
Form: contains values in HTML form fields
QueryString: Value containing query string
ServerVariables: contains values in header and environment variables
example:
request_url.asp
<%
'Get user input and store variables
user_id=request.querystring(user_id)
user_name=request.querystring(user_name)
'Judge whether the user input is correct
if user_id= then
response.write User_id is null,please check it
response.end
end if
if user_name= then
response.write User_name is null,please check it
response.end
end if
'Print variables
response.write user_id&<br>
response.write user_name
%>
Effect:
When accessing http://10.1.43.238/course/request_url.asp?user_name=j:
User_id is null,please check it
When accessing http://10.1.43.238/course/request_url.asp?user_name=j&user_id=my_id:
my_id
j
Thinking: How are variables passed in URLs and obtained by Asp pages?
request_form.htm
<style type=text/CSS>
<!--
.input {background-color: #FFFFF; border-bottom: black 1px solid;border-left: black 1px solid; border-right: black 1px solid; border-top: black 1px solid; color: #000000;font-family : Georgia; font-size: 9pt;color: midnightblue;}
a:link {color: #1B629C; text-decoration: none}
a:hover {color: #FF6600; text-decoration: underline}
a: visited {text-decoration: none}
-->
</style>
<center>
<form name=course action=request_form.asp method=post>
User_id: <input type=text name=user_id maxlength=20 class=input><br><br>
User_name: <input type=text name=user_name maxlength=30 class=input>
</form>
<br><br>
<a href=javascript:document.course.submit();> Submit</a>
</center>
request_form.asp
<%
'Get user input and store variables
user_id=request.form(user_id)
user_name=request.form(user_name)
'Judge whether the user input is correct
if user_id= then
response.write User_id is null,please check it
response.end
end if
if user_name= then
response.write User_name is null,please check it
response.end
end if
'Print variables
response.write user_id&<br>
response.write user_name
%>
Note: What is the difference between the action pointing of form, request_form.asp and request_url.asp in source code?
2. Response:
Definition: Used to send back information to the browser, and use this object to send output from the script to the browser.
set:
Cookies: Add a cookie to your browser
method:
End: End the script processing
Redirect: Boot the browser to a new page
Write: Send a string to the browser
property:
Buffer: cache an ASP
CacheControl: The cache is controlled by the proxy server
ContentType: Specifies the content type of the response
Expires: Browser uses relative time to control cache
ExpiresAbsolute: Browser uses absolute time to control cache
example:
response_redirect.asp
<%
'Go to google and have a look
response.redirect http://www2.google.com
response.end
%>
response_cookies.asp
<%
'Set and read cookies
response.cookies(time_now)=now()
response.write request.cookies(time_now)
%>
Effect:
When accessing http://10.1.43.238/course/response_cookies.asp:
2002-9-1 16:20:40
response_buffer.asp
<%'response.buffer=true%>
<a href=a>a</a>
<%response.redirect request_form.htm%>
Effect:
①. When IIS's buffering function is turned off, an error occurred while accessing this page
a
Reply object error 'ASP 0156: 80004005'
Header wrong
/course/response_buffer.asp, line 3
The HTTP header has been written to the client browser. Any modification of HTTP headers must be before the page content is written.
②. When IIS's buffering function is turned off and the comments on the first line of the file are removed, the page redirection is successful
③. When IIS's buffering function is turned on, page redirection will be successful regardless of whether the comment on the first line of the file is removed or not,
3. Server
Definition: Different entity functions can be used on the server, such as controlling the time of script execution before time arrives. Can also be used to create other objects.
method:
CreateObject: Create an object instance
HTMLEncode: Convert strings to use special HTML characters
MapPath: Convert virtual paths into physical paths
URLEncode: convert strings into URL-encoded
ScriptTimeout: The number of seconds a script is allowed to run before termination
example:
server_htmlencode.asp
<%
'html encode
response.write server.htmlencode(atime_now)
%>
Effect:
atime_now
When viewing the source file, it appears as: atime_now
Thinking: Why isn't it such an effect as atime_now? What's wrong with the source file?
server_mappath.asp
<%
'mappath
response.write server.mappath(server_mappath.asp)
%>
Effect:
G:/asp_www/test/course/server_mapath.asp
Thinking: How to get the actual path to the root directory of the site? How to get the actual path to a directory?
server_urlencode.asp
<%
'url encode
response.write server.urlencode(a/time_now)
%>
Effect:
a%5Ctime%5Fnow
4. Application
Definition: Used to store and read application information shared by users. For example, this object can be used to transmit information between users of the website. The information is lost after the server restarts.
method:
Lock: Prevent other users from accessing Application Sets
Unlock: enables other users to access the Application Set
event:
OnEnd: triggered by terminating the network server and changing the Global.asa file
OnStart: triggered by the first application for a web page in the application
example:
application_counter.asp
<%
'A simple counter made using Application
Application.lock
Application(clicks)=Application(clicks)+1
Application.unlock
response.write You are the first &Application(clicks)& visitor of this site!
response.write <br><br>You are from &request.servervariables(remote_addr)
%>
Effect:
You are the first visitor to this site!
You are from 10.1.43.238
Thinking: What are the functions of lock and unlock in this example?
5. session
Definition: Store and read specific user conversation information, such as storing user access information to the website, and the information is lost after the server restarts.
method:
Abandon: After processing the current page, end a user session
property:
Timeout: User session duration (minutes)
event:
OnEnd: Outside Session Timeout time, the user no longer applies for the page to trigger the event
OnStart: triggered by the user's first application for the web page
example:
session_counter.asp
<%
'A simple counter made using Session
session(clicks)=session(clicks)+1
response.write You are the first visitor of this site&session(clicks)&!
response.write <br><br>You are from &request.servervariables(remote_addr)
%>
Effect:
You are the first visitor to this site!
You are from 10.1.43.238
Thinking: Since both session and application can count, what is the difference between them? How to start counting again if you want to get over 100?
3. Use Asp to operate the database:
1. The difference between connecting through ODBC or OLE?
There are now two ways to connect to the database. On the one hand, a connection can be generated using ODBC, which is compatible with any database with an ODBC drive (i.e. basically all databases on the market); on the other hand, a connection can be generated using the original OLE DB provider.
Which provider should I use? Use the original OLE DB provider whenever possible, as it provides more efficient access to the data. Microsoft is gradually replacing the ODBC standard with OLE DB and should only use ODBC without the original OLE DB provider.
⑴. Connect to SQL Server using ODBC:
①. Configure ODBC
②. Connection code:
conn_odbc.asp
<%
Set Conn = Server.CreateObject(ADODB.Connection)
'Conn.Open DSN=course_dsn;UID=course_user;PWD=course_passWord;DATABASE=course
Conn.Open course_dsn,course_user,course_password
%>
Note: When configuring MyDSN, if the default database is specified as course, the above code will be the same. Otherwise, the connection method of the second line is more flexible. You can specify to connect to a certain database (of course, the premise is that course_user has operational permissions on this database) .
⑵. Connect to SQL Server using OLE:
conn_ole.asp
<%
Set Conn = Server.CreateObject(ADODB.Connection)
Conn.Open PROVIDER=SQLOLEDB;DATA SOURCE=10.1.43.238,2433; UID=course_user;PWD=course_password;DATABASE=course
%>
2. Operation database: Connection and Recordset
Use connection and recordset to operate the database, or use connection only to operate the database.
example:
⑴. Use connection and recordset to operate the database together
use_db_1.asp
<%
Set conn=Server.CreateObject(ADODB.Connection)'Create an object connecting to the database
conn.Open course_dsn,course_user,course_password' Use this object to connect to the database
Set rs=Server.CreateObject(ADODB.RecordSet)'Create recordset object
rs.Open select * from user_info,conn,1,1' Open database using record set object
if rs.recordcount>0 then'If there is a record
response.write User_idUser_name<br>
for i=1 to rs.recordcount' loop to read all records
response.write rs(id)&&rs(user_name)&<br>
'Output record fields to the browser
rs.movenext' pointer moves one line down
if rs.eof then exit for'Exit the loop if the bottom of the record set is reached
next
end if
%>
Effect:
User_idUser_name
1ahyi
3test
⑵. Only use connection to operate the database:
use_db_2.asp
<%
Set conn=Server.CreateObject(ADODB.Connection)'Create an object connecting to the database
conn.Open course_dsn,course_user,course_password' Use this object to connect to the database
conn.execute delete from user_info
%>
Effect:
All data in the user_info table is deleted
Thinking: Are there any differences between the two methods? In what occasions are they used?
3. How to use transactions, stored procedures, and views?
⑴. Using stored procedures
①. Define stored procedures
CREATE PROCEDURE [output_1]
@sid int output
AS
set @sid=2
CREATE PROCEDURE [return_1]
(@user_name varchar(40),@password varchar(20))
AS
if exists(select id from user_info where user_name=@user_name and password=@password)
return 1
else
return 0
CREATE PROCEDURE [user_info_1]
(@user_name varchar(40),@password varchar(20))
AS
select id from user_info where user_name=@user_name and password=@password
CREATE PROCEDURE [user_info_2]
(@user_name varchar(40),@password varchar(20))
AS
SET XACT_ABORT ON
BEGIN TRANSACTION
delete from user_info where user_name=@user_name and password=@password
COMMIT TRANSACTION
SET XACT_ABORT OFF
CREATE PROCEDURE [user_info_3] AS
select * from user_info
②. Called in Asp
use_proc.asp
<!-- #include virtual=/adovbs.inc -->
<%
Set conn=Server.CreateObject(ADODB.Connection)
conn.Open course_dsn,course_user,course_password
'Call stored procedures with two input parameters and return record set using recordset
'CREATE PROCEDURE [user_info_1]
'(@user_name varchar(40),@password varchar(20))
'AS
'select id from user_info where user_name=@user_name and password=@password
response.write Normal calling method: <br>
set rs=server.createobject(adodb.recordset)
sql=user_info_1 '&request.querystring(user_name)&','&request.querystring(password)&'
rs.open sql,conn,1,1
response.write rs(id)&<br>
rs.close
'Use recordset to call stored procedures without input parameters, return recordset, you can use recordcount and other attributes
'CREATE PROCEDURE [user_info_3] AS
'select * from user_info
response.write <br>Returns the record set, you can use recordcount and other properties:
sql=exec user_info_3
rs.open sql,conn,1,1
for i=1 to rs.recordcount
response.write <br>&rs(user_name)
rs.movenext
next
rs.close
set rs=nothing
'Use command to call stored procedures with output parameters
'CREATE PROCEDURE [output_1]
'@sid int output
'AS
'set @sid=2
response.write <br><br>Calling stored procedures with output parameters: <br>
set cmd=server.createobject(adodb.command)
cmd.activeconnection=conn
cmd.commandtext = output_1
cmd.parameters.append cmd.createparameter(@sid,adinteger,adparamoutput)
cmd(@sid)=10
cmd.execute()
bbb=cmd(@sid)
response.write bbb&<br>
set cmd=nothing
'Use command to call stored procedures with two input parameters and return value
'CREATE PROCEDURE [return_1]
'(@user_name varchar(40))
'AS
'if exists(select id from user_info where user_name=@user_name)
'return 1
'else
'return 0
response.write <br>Calling a stored procedure with two input parameters and return value: <br>
set cmd=server.createobject(adodb.command)
cmd.activeconnection=conn
cmd.commandtype = adcmdstoredproc
cmd.commandtext = return_1
cmd.parameters.append cmd.createparameter(@return_value,adinteger,adparamreturnvalue)
cmd.parameters.append cmd.createparameter(@user_name,advarchar,adparaminput,40)
cmd.parameters.append cmd.createparameter(@password,advarchar,adparaminput,20)
cmd(@user_name)=tuth
cmd(@password)=yyuyu
cmd.execute()
rrr=cmd(@return_value)
response.write rrr
set cmd=nothing
conn.close
set conn=nothing
%>
Effect:
When visiting http://10.1.43.238/course/use_proc.asp?user_name=ahyi&password=tt, the following appears
Normal calling method:
12
Return to the record set, you can use recordcount and other properties:
ahyi
tet
tuth
Call stored procedures with output parameters:
2
Call a stored procedure with two input parameters and return value:
1
Note: If there are no parameters in stored procedure, the sql statement called is directly the stored procedure name, and one parameter is the stored procedure name parameter. If there are multiple parameters, the stored procedure name parameter 1, parameter 2,..., parameter n; if in Add exec to the sql statement, then you can use properties such as recordcount in the returned record set; if you want to obtain the return value or output parameters of the stored procedure, you can use the command object.
⑵. Use transaction processing
①. Asp embedded transaction support
example:
use_transaction_1.asp
<%
'Using transactions in Asp
Set conn=Server.CreateObject(ADODB.Connection)
conn.Open course_dsn,course_user,course_password
conn.begintrans'Start the transaction
sql=delete from user_info
set rs=server.createobject(adodb.recordset)
rs.open sql,conn,3,3
if conn.errors.count>0 then'There is an error
conn.rollbacktrans' rollback
set rs=nothing
conn.close
set conn=nothing
response.write transaction failed and rolled back to the state before modification!
response.end
else
conn.committrans' commit transaction
set rs=nothing
conn.close
set conn=nothing
response.write The transaction was successful!
response.end
end if
%>
②. Database-level transactions
i. Create stored procedures
CREATE PROCEDURE [user_info_2]
(@user_name varchar(40),@password varchar(20))
AS
SET XACT_ABORT ON
BEGIN TRANSACTION
delete from user_info where user_name=@user_name and password=@password
COMMIT TRANSACTION
SET XACT_ABORT OFF
ii. Called in Asp
use_transaction_2.asp
<%
Set conn=Server.CreateObject(ADODB.Connection)
conn.Open course_dsn,course_user,course_password
sql=user_info_2 '&request.querystring(user_name)&','&request.querystring(password)&'
set rs=server.createobject(adodb.recordset)
rs.open sql,conn,1,1
set rs=nothing
conn.close
set conn=nothing
%>
Discussion: The advantages and disadvantages of the two methods?
⑶. Using View
After defining the view in the database, use the view in Asp as if using a table
4. An example of a database paging
db_page.asp
<%
on error resume next
Set conn=Server.CreateObject(ADODB.Connection)'Create an object connecting to the database
conn.Open course_dsn,course_user,course_password' Use this object to connect to the database
set rs=server.createObject(adodb.recordset)
sql=select * from user_info order by id desc
rs.open sql,conn,1,1
if rs.recordcount>0 then'If there is a record
rs.pagesize=2'A maximum of 2 records are displayed per page
'Get the page to be displayed from the URL
page=cint(request(page))
'Page parameter exception handling
if page= then page=1
if page<1 then page=1
if page>= rs.pagecount then page=rs.pagecount
rs.absolutepage=page'The current page is the page specified by the page parameter
for i=1 to rs.pagesize'Cycle the records in the current page according to the size of the pagesize parameter
response.write User_id:&rs(id)&<br>
response.write User_name:&rs(user_name)&<br><br>
rs.movenext' record pointer moves down
if rs.eof then exit for'Exit the loop if the bottom of the record set is reached
next
end if
'Show page turn button
if page>1 then
response.write <a href=&request.servervariables(document_name)&?page=1>Page 1</a>
response.write <a href=&request.servervariables(document_name)&?page=&(page-1)&>Previous page</a>
end if
if page<>rs.pagecount then
response.write <a href=&request.servervariables(document_name)&?page=&(page+1)&>Next page</a>
response.write <a href=&request.servervariables(document_name)&?page=&rs.pagecount&>Last page</a>
end if
response.write page number: &page&/&rs.pagecount
'Close the object and release the memory
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
Thinking: What additional attributes are used during the paging process?