4. Development and use of asp components:
1. What are the components?
Advantages:
n is easy to call and saves code
High safety
nSupport transaction processing, multi-component union
nFast running speed
nUse no need to modify the page to upgrade and modify components, so it has good scalability
l Disadvantages:
Development and debugging difficulties
2. How to develop using VB?
⑴. Open VB>>NewPRoject>>ActiveX DLL
⑵. Modify the project name to course
⑶. Modify the name of the class module to be conn_db
⑷. Project>> References, references to COM+ Service Type Library and Microsoft Active Server Pages Object Library.
⑸. Modify the class code as follows:
from www.knowsky.com
'Create a database connection and output the database field
Dim Response As Response
Dim Request As Request
Dim Server As Server
DimapplicationAs Application
DimsessionAs Session
Private Sub Class_Initialize()
Dim objContext As ObjectContext
Set objContext = GetObjectContext()
Set Response = objContext(Response)
Set Request = objContext(Request)
Set Server = objContext(Server)
Set Application = objContext(Application)
Set Session = objContext(Session)
End Sub
Sub conn_db()
Set conn = CreateObject(adodb.connection)
conn.open course_dsn, course_user, course_passWord
Set rs = CreateObject(adodb.recordset)
rs.open select * from user_info, conn, 1, 1
If rs.recordcount > 0 Then
For i = 1 To rs.recordcount
Response.write <br> & rs(user_name) & <br>
If rs.EOF Then Exit For
rs.movenext
Next
End If
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Sub
⑹. Add a new class cutstr
⑺. Modify the class code as follows:
'Intercepting the string
Function cutstr(str, length)
If Len(str) > length Then
cutstr = Left(str, length) & ...
Else
cutstr = str
End If
End Function
⑻. File>>Save
⑼. File>>make course.dll
3. Registered components: MTS and regsvr32.exe
There are two ways to register components: MTS and using regsvr32.exe. MTS is recommended because it has the following advantages:
n Dynamic uninstall balance to improve the upgradeability of components and component-based applications.
n contains the ability to publish and submit event and queue components, making it easier to combine with multiple components.
To make the component have the MTS characteristics, a little change must be made to the component. When developing under NT and 98, the Microsoft Transaction Server Type Library must be referenced in the project, and when developing under Windows 2000, the COM+ Service Type Library must be referenced.
⑴. Regsvr32 registration:
regsvr32.exe is an executable file under system32. It reads component information into the registry for Asp to call.
Use the command line to enter the directory where the component dll file is located, and execute regsvr32 dll_file_name.
Run regedit, and the course.conn_db and course.cutstr items will be found under HKEY_CLASSES_ROOT, indicating that the component registration is successful.
⑵. Register with MTS:
①. Start >>Programs >>Administrative Tools >>Component Services
②. Expand the directory to the following state:
③. Follow the wizard, next step until the following dialog box, click Create an empty application:
④. In the following dialog box, name the application course, and the other defaults until it is completed
⑤. Expand the course application, right-click, and create a new component
⑥. Follow the prompts and continue. When the following dialog box appears, select Import the registered component.
⑦. Select the components we developed, next step until they are finished
⑧. At this time, you can find that there are two additional components under the course application:
4. Calling components in Asp
asp_use_com.asp
<%
'asp calls com component
set cutstr_obj=server.createobject(course.cutstr)
response.write cutstr_obj.cutstr(abcdefghijk,3)&<br>
set cutstr_obj=nothing
set conn_obj=server.createobject(course.conn_db)
conn_obj.conn_db()
set conn_obj=nothing
%>
Effect:
abc...
ahyi
tuth
It means the call is successful.
5. Uninstall components
⑴. Use the regsvr32 to uninstall the component using the -u switch:
Note: First enter the directory where the component dll is located, and then use regsvr32 –u dll_file_name to uninstall; just restart IIS after uninstalling.
⑵. Using MTS registered components, first delete the corresponding application in the component service, and then perform step 1 to completely uninstall the components.
6. Dll component storage location and permission settings
⑴. We just need to copy the compiled Dll file, and other files do not need to be processed.
⑵. Put Dll outside the Web site, such as in the system32 directory, to prevent downloading
⑶. The file rights of Dll are set to System reading, and Internet users traverse folders/run files
⑷. Dll removes all permissions in IIS, such as reading, voluntary access to scripts, etc.
After the above processing, the security of the Dll file can be ensured.
7. other
How to use Asp's object in a component to conveniently port Asp code to COM component?
5. IIS optimal configuration
1. Web site tab: ip, port, virtual host, connection, log
2. ISAPI filter: add php and jsp support
3. Home directory configuration tab: I IS permission settings (combined with file rights), execution permission, application protection, mapping, buffering, parent path, error information
4. Other tabs: Custom errors, Http headers, directory security, documentation
5. The benefits and disadvantages of file compression
VI. Others
1. Send email (jmail; Ms smtp)
Send emails using Microsoft Smtp
⑴. Install Microsoft SMTP Service
⑵. Setting up Microsoft SMTP Service
⑶. Code section:
mail_smtp.asp
<%
Sub sendmail(fromwho,towho,subject,body)
dim mymail
set mymail = server.createobject(cdonts.newmail)
mymail.from = fromwho
mymail.to = towho
mymail.subject = subject
mymail.body = body
mymail.send
set mymail = nothing
end sub
%>
This subroutine accepts 4 parameters corresponding to the following items.
lThe email address of the sender
lThe email address of the email recipient
lEmail Subject
lEmail content
How to use:
<%
fromWho=…
toWho=…
Subject=…
Body=…
IF toWho <> THEN
sendMail from Who, toWho, Subject, Body
END IF
%>
Send Email using Jmail
If you are interested, you can discuss it with me. I also have the Jmail software.
2. Unzip Zip files (Wscript.Shell and Winzip command line; java components)
⑴. Install Winzip 8.1 or above
⑵. Install Winzip command line
⑶. Set the file rights of the working directory to be read, written and modified by Internet users
⑷. Code section:
unzip_a_zipfile.asp
<%
'Start the program with the shell object
'zip_path is the path to the specific zip file, such as c:/test.zip
'path is the path to store the unzipped file
'ond is a command line parameter
set wshshell = server.createobject(wscript.shell)
issue = wshshell.run (wzunzip -ond &zip_path& &path,1,true)
'Delete the zip file
set myfileobject=server.createobject(scripting.filesystemobject)
myfileobject.deletefile zip_path
'Judge whether it is successful to continue the operation
if issue = 0 then
'success
...
else
'fail
...
end if
%>
3. Operate xml file
The exchange time is limited, so let's discuss it in detail if you have time
4. File upload
⑴. Install the file upload component Asp fileup (supports multiple file uploads, file type and size judgment, file name change after uploading, etc.)
⑵. Restart IIS to make the upload component effective
⑶. Set the file rights of the upload directory so that Internet users can read, write and modify
⑷. Code section
upload_file.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 enctype=multipart/form-data method=post action=upload_file.asp name=Upload>
<input type=hidden name=CopyrightInfo value=http://www.chinaasp.com>
Please select a file: <input type=file name=file1 class=input><br><br>
Please select a file: <input type=file name=file2 class=input><br><br>
</form>
<br><br>
<a href=Javascript:document.Upload.submit();> Submit</a>
</center>
upload_file.asp
<%
on error resume next
'Define the function that obtains the file suffix
function getfileextname(filename)
pos=instrrev(filename,.)
if pos>0 then
getfileextname=mid(filename,pos+1)
else
getfileextname=
end if
end function
'Define the function to get the file correct name
function getfilename(filename)
lens=len(filename)-len(getfileextname(filename))-1
getfilename=left(filename,lens)
end function
'Create an object for file upload component
set fileup=server.createobject(chinaasp.upload)
'Read the file uploaded by the user loop and save it on the server
for each f in fileup.files
'When the user does not select a file or the file size exceeds 10m, return to the page where the file is selected to upload it
if f.filename= or f.filesize>10485500 then response.redirect upload_file.htm
'Get the saved path
path=server.mappath(upload_file.asp)
path=left(path,len(path)-15)
'Save the file
f.saveas path&getfilename(f.filename)&.&getfileextname(f.filename)
next
response.redirect upload_file.htm
%>
5. Drive/directory/file operations
The exchange time is limited, so let's discuss it in detail if you have time
6. Asp writing and debugging experience: how to select cookies and sessions, the number of cookies traps, page expiration and buffering settings, how to ensure portability, how to deal with 500 errors in the internal server...
1. Cookies and Session Choices:
⑴. Common Features
⑵. The difference:
①. How to work
②. Expiration conditions
③. Impact on server performance
2. Cookies Number Trap:
IIS can save no more than 20 general cookies, and then define new cookies. The value of the previous cookies is lost. This is obviously very limited to large applications. How to solve this problem?
The answer is to use 2D cookies.
example:
Test the limit of the number of 1D cookies:
test_cookies_1.asp
<%
for i=1 to 50
response.cookies(cookies_&i)=i
next
%>
test_cookies_2.asp
<%
for i=1 to 50
response.write request.cookies(cookies_&i)&<br>
next
%>
Effect:
First visit test_cookies_1.asp, then test_cookies_2.asp, what did you find?
test_cookies_3.asp
<%
for i=1 to 50
response.cookies(cookies_&i)=i
next
for i=1 to 50
response.write request.cookies(cookies_&i)&<br>
next
%>
Effect:
No cookies are lost! ! ! !
Test the limit of the number of 2D cookies:
test_cookies_4.asp
<%
for i=1 to 301
response.cookies(tuht)(cookies_&i)=i
next
%>
test_cookies_5.asp
<%
for i=1 to 301
response.write request.cookies(tuht)(cookies_&i)&<br>
next
%>
Effect:
Use this method to use 201*20=4020 cookies! ! ! !
3. Page expiration and buffering settings
<%
'Expiration and buffering
response.buffer=true
response.cachecontrol=no-chache
response.expiresabsolute=now()-1
response.expires=0
%>
You can also make settings in html:
<meta content=no-cache http-equiv=Pragma>
<meta HTTP-EQUIV=Expires CONTENT=0>
4. Ensure of transplantability
⑴. Include files
<!--#include file=top.asp -->
⑵. Use server.mappath to find file paths, avoid using absolute paths directly in the page
⑶. Try to use components to encapsulate business logic
5. Error debugging internal server 500
⑴. Set IIS to display specific error messages
⑵. Step by step debugging, from top to bottom
⑶. Print the values of some important variables and check if they are expected
⑷. Judging errors based on experience
7. Operate Word Documents
⑴. Install Office 2000, which is required for Word 2000
⑵. Set Internet security in IE: All ActiveX controls and plug-ins are enabled
⑶. Set the file rights of the working directory to read/modify/write the Internet and System
⑷. Write template course.dot
⑸. Specific code:
opr_doc_inc.asp
<%
Response.write Dim Var_Num & chr(13)
Response.write Var_Num = 2 & chr(13)
Response.write Dim varstrings(2) & chr(13)
Response.write varstrings(0)= & chr(34) & drafter: & chr(34) & chr(13)
Response.write varstrings(1)= & chr(34) & Date: & chr(34) & chr(13)
Response.write Dim varValues(2) & chr(13)
Response.write varValues(0)= & chr(34) & drafter: Tu Haitao & chr(34) & chr(13)
Response.write varValues(1)= & chr(34) & Date: &date()& chr(34) & chr(13)
%>
Sub instead(word)
Set myRange = word.ActiveDocument.Content
for i=0 to Var_Num - 1
call myRange.Find.Execute(varStrings(i),false,false,false,false,false,false,false,false,false,false,varValues(i),2)
Next
End Sub
opr_doc.asp
<%
'Get the saved path
path=server.mappath(opr_doc.asp)
path=left(path,len(path)-11)
filenames=path&test.doc
w1=word.activedocument.saveAs&chr(32)&chr(34)&filenames&chr(34)
w2=wApp.Documents.open&chr(32)&chr(34)&filenames&chr(34)
%>
<script language=vbscript>
On Error Resume Next
'Create a Word document with a specified file name
Dim word
set word = CreateObject(Word.Application)
if Err.number > 0 Then
An error occurred in Alert, please confirm whether the file exists
else
word.visible = False
word.documents.open <%response.write path%>course.dot
<%Response.write w1%>
word.documents.close
set word=nothing
end if
<!--#include file=opr_doc_inc.asp-->
Dim wApp
Set wApp = CreateObject(Word.Application)
If Err.number > 0 Then
An error occurred in Alert. Please confirm whether the file was created correctly.
else
wApp.visible = True
<%Response.write w2%>
call instead(wApp)
set wApp=nothing
end if
</script>
Effect: Check whether the doc file has been generated? What is the difference between this newly created doc file and template file? Has the drafter and the date changed? Save it and see the contents of the newly generated doc file.
Attached:
1. All the above codes were tested and passed under Windows 2000 Server SP2+IIS 5.0+MS SQL Server 2000+Office 2000
2. Configure database: database name course, user course_user, password course_password, ODBC driver is course_dsn, port is 2433, scripts describing the table structure are in the shared directory.
3. Please download the software Asp fileup, Jmail, Winzip 8.1, and Winzip command line by yourself.
4. Database script file:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[output_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[output_1]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[return_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[return_1]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[user_info_1]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info_2]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[user_info_2]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info_3]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[user_info_3]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[user_info]
GO
CREATE TABLE [dbo].[user_info] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[user_name] [varchar] (40) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[password] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[user_info] WITH NOCHECK ADD
CONSTRAINT [PK_user_info] PRIMARY KEYCLUSTERED
(
[user_name]
)ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [output_1]
@sid int output
AS
set @sid=2
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
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
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
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
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
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
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [user_info_3] AS
select * from user_info
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO