ASP 经典网站制作实例教程:构建一个简易个人博客
本教程旨在通过一个实际项目,让你掌握 ASP (VBScript) 开发网站的核心技能。

第一部分:准备工作与环境搭建
在开始之前,你需要准备好开发环境。
- Web 服务器:最常用的是 IIS (Internet Information Services),它是 Windows 系统自带的,你可以通过“控制面板” -> “程序” -> “启用或关闭 Windows 功能” 来安装 IIS。
- ASP 解释器:IIS 自带,无需额外安装。
- 数据库:我们将使用 Microsoft Access,它简单易用,非常适合小型项目,你需要安装 Access 或者 Access Database Engine(如果只需要读写 .mdb 文件)。
- 代码编辑器:任何文本编辑器都可以,如 记事本、Notepad++、Visual Studio Code 或 Dreamweaver,推荐使用 VS Code,它对代码有很好的高亮和提示功能。
- 浏览器:用于测试你的网站。
创建网站根目录:
在 IIS 中,你需要创建一个“网站”或“虚拟目录”,并指定一个物理文件夹作为你的网站根目录,我们创建一个名为 MyBlog 的文件夹,并将其设置为网站的根目录,之后,我们所有的 .asp 文件和数据库文件都将放在这个文件夹里。
第二部分:数据库设计与创建
博客的核心是数据,我们需要一个数据库来存储文章信息。
- 打开 Microsoft Access。
- 创建一个新的空白数据库,命名为
blog.mdb。 - 在
blog.mdb中,创建一个新表,命名为Articles。 - 为
Articles表设计以下字段:
| 字段名称 | 数据类型 | 说明 |
|---|---|---|
ID |
自动编号 | 主键,每篇文章的唯一ID |
Content |
备注 | (可以很长) |
Author |
文本(50) | 作者 |
PostDate |
日期/时间 | 发布日期 |
IsPublished |
是/否 | 是否已发布(用于后台控制) |
保存表,现在你的数据库结构就准备好了,你可以手动添加几条测试数据。

第三部分:网站结构与文件规划
为了保持代码的整洁和可维护性,我们采用以下文件结构:
MyBlog/
├── index.asp (首页,显示文章列表)
├── article.asp (文章详情页)
├── admin/
│ ├── login.asp (后台登录页)
│ ├── default.asp (后台管理主页面)
│ ├── add_article.asp (添加文章页面)
│ └── edit_article.asp (编辑文章页面)
├── includes/
│ ├── conn.asp (数据库连接文件)
│ └── header.asp (页面头部)
│ └── footer.asp (页面底部)
├── images/ (存放图片的文件夹)
├── styles/
│ └── main.css (CSS样式文件)
└── blog.mdb (我们的数据库文件)
第四部分:编写核心代码
我们开始一步步编写代码。
步骤 1:创建数据库连接文件 (includes/conn.asp)
这个文件用于连接我们的 Access 数据库,我们将在所有需要数据库操作的页面中包含它。
<!-- File: includes/conn.asp -->
<%
' 定义数据库路径
' Server.MapPath() 函数将虚拟路径转换为服务器上的物理路径
dbPath = Server.MapPath("../blog.mdb")
' 创建 ADO Connection 对象
Set conn = Server.CreateObject("ADODB.Connection")
' 设置连接字符串,用于连接 Access 数据库
' Provider=Microsoft.Jet.OLEDB.4.0 是较老的版本,如果你的系统是较新的,可能需要 Provider=Microsoft.ACE.OLEDB.12.0
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath
' 打开数据库连接
conn.Open connStr
%>
步骤 2:创建页面头部 (includes/header.asp)
这个文件包含了博客的通用头部,<head> 标签、CSS 引用和网站导航栏。

<!-- File: includes/header.asp -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">我的个人博客</title>
<link rel="stylesheet" href="../styles/main.css">
</head>
<body>
<div id="header">
<h1><a href="../index.asp">我的个人博客</a></h1>
<nav>
<a href="../index.asp">首页</a>
<!-- 检查是否是管理员,如果是则显示管理链接 -->
<% If Session("isAdmin") = true Then %>
<a href="../admin/default.asp">后台管理</a>
<% End If %>
</nav>
</div>
<div id="main-content">
步骤 3:创建页面底部 (includes/footer.asp)
<!-- File: includes/footer.asp -->
</div> <!-- end #main-content -->
<div id="footer">
<p>© 2025 我的个人博客. All Rights Reserved.</p>
</div>
</body>
</html>
步骤 4:创建首页 (index.asp)
首页显示所有已发布的文章列表。
<!-- File: index.asp -->
<!-- 包含头部和数据库连接 -->
<!--#include file="includes/header.asp" -->
<!--#include file="includes/conn.asp" -->
<h2>最新文章</h2>
<%
' 创建 ADO Recordset 对象
Set rs = Server.CreateObject("ADODB.Recordset")
' SQL 查询语句,只查询已发布的文章,并按发布日期降序排列
sql = "SELECT * FROM Articles WHERE IsPublished = True ORDER BY PostDate DESC"
' 打开记录集
rs.Open sql, conn, 1, 1 ' 1,1 表示只读、静态游标
' 检查是否有记录
If Not rs.EOF Then
%>
<ul class="article-list">
<% Do While Not rs.EOF %>
<li>
<h3><a href="article.asp?id=<%= rs("ID") %>"><%= rs("Title") %></a></h3>
<p class="post-meta">发布于: <%= rs("PostDate") %> | 作者: <%= rs("Author") %></p>
<p><%= Left(rs("Content"), 150) & "..." %></p> <!-- 只显示前150个字符 -->
</li>
<%
rs.MoveNext ' 移动到下一条记录
Loop
%>
</ul>
<%
Else
%>
<p>暂无文章。</p>
<%
End If
' 关闭并释放对象
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
%>
<!-- 包含底部 -->
<!--#include file="includes/footer.asp" -->
步骤 5:创建文章详情页 (article.asp)
通过 id 参数获取并显示单篇文章的完整内容。
<!-- File: article.asp -->
<!--#include file="includes/header.asp" -->
<!--#include file="includes/conn.asp" -->
<%
' 获取URL中的ID参数
articleId = Request.QueryString("id")
' 防止SQL注入(简单示例)
If Not IsNumeric(articleId) Then
Response.Write("无效的文章ID。")
Response.End
End If
' SQL查询
sql = "SELECT * FROM Articles WHERE ID = " & articleId & " AND IsPublished = True"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 1, 1
If Not rs.EOF Then
%>
<article class="full-article">
<h1><%= rs("Title") %></h1>
<p class="post-meta">发布于: <%= rs("PostDate") %> | 作者: <%= rs("Author") %></p>
<div class="article-content">
<p><%= Replace(rs("Content"), vbCrlf, "<br />") %></p> ' 将换行符转换为HTML的<br>标签
</div>
</article>
<%
Else
Response.Write("文章不存在或未发布。")
End If
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
%>
<!--#include file="includes/footer.asp" -->
步骤 6:创建后台登录 (admin/login.asp)
管理员登录页面。
<!-- File: admin/login.asp -->
<!--#include file="../includes/header.asp" -->
<h2>管理员登录</h2>
<%
' 如果登录失败,显示错误信息
If Request.QueryString("error") = "1" Then
Response.Write "<p style='color:red;'>用户名或密码错误!</p>"
End If
%>
<form action="check_login.asp" method="post">
<p>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
</p>
<p>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
</p>
<p><input type="submit" value="登录"></p>
</form>
<!--#include file="../includes/footer.asp" -->
步骤 7:处理登录逻辑 (admin/check_login.asp)
验证用户名和密码。注意: 在实际项目中,密码应该加密存储,这里仅为演示。
<!-- File: admin/check_login.asp -->
<%
' 简单的硬编码验证
adminUsername = "admin"
adminPassword = "123456"
inputUsername = Request.Form("username")
inputPassword = Request.Form("password")
If inputUsername = adminUsername And inputPassword = adminPassword Then
' 登录成功,创建会话
Session("isAdmin") = True
' 重定向到管理后台首页
Response.Redirect("default.asp")
Else
' 登录失败,重定向回登录页并传递错误参数
Response.Redirect("login.asp?error=1")
End If
%>
步骤 8:创建后台管理首页 (admin/default.asp)
只有登录的管理员才能访问。
<!-- File: admin/default.asp -->
<!-- 检查管理员是否已登录 -->
<%
If Session("isAdmin") <> true Then
Response.Redirect("login.asp") ' 如果未登录,重定向到登录页
End If
%>
<!--#include file="../includes/header.asp" -->
<h2>后台管理</h2>
<p>欢迎,管理员!</p>
<ul>
<li><a href="add_article.asp">添加新文章</a></li>
<li><a href="list_articles.asp">管理文章列表</a></li>
<li><a href="logout.asp">退出登录</a></li>
</ul>
<!--#include file="../includes/footer.asp" -->
步骤 9:添加文章页面 (admin/add_article.asp)
一个表单,用于输入新文章的信息。
<!-- File: admin/add_article.asp -->
<%
' 检查管理员登录
If Session("isAdmin") <> true Then
Response.Redirect("login.asp")
End If
%>
<!--#include file="../includes/header.asp" -->
<h2>添加新文章</h2>
<form action="save_article.asp" method="post">
<p>
<label for="title">标题:</label>
<input type="text" id="title" name="title" required>
</p>
<p>
<label for="author">作者:</label>
<input type="text" id="author" name="author" value="管理员" required>
</p>
<p>
<label for="content">内容:</label>
<textarea id="content" name="content" rows="15" required></textarea>
</p>
<p>
<input type="checkbox" id="ispublished" name="ispublished" value="True" checked>
<label for="ispublished">立即发布</label>
</p>
<p><input type="submit" value="保存文章"></p>
</form>
<!--#include file="../includes/footer.asp" -->
步骤 10:保存文章 (admin/save_article.asp)
将表单数据插入到数据库中。
<!-- File: admin/save_article.asp -->
<%
' 检查管理员登录
If Session("isAdmin") <> true Then
Response.Redirect("login.asp")
End If
' 包含数据库连接
<!--#include file="../includes/conn.asp" -->
' 获取表单数据= Request.Form("title")
author = Request.Form("author")
content = Request.Form("content")
isPublished = Request.Form("ispublished") ' 值为 "True" 或 空
' 创建并执行 SQL 插入语句
sql = "INSERT INTO Articles (Title, Content, Author, PostDate, IsPublished) VALUES "
sql = sql & "('" & Replace(title, "'", "''") & "', "
sql = sql & "'" & Replace(content, "'", "''") & "', "
sql = sql & "'" & Replace(author, "'", "''") & "', "
sql = sql & Now() & ", " ' Now() 函数获取当前日期和时间
sql = sql & (isPublished = "True") & ")" ' (isPublished = "True") 在ASP中会返回 -1 (True) 或 0 (False)
conn.Execute(sql)
' 关闭连接
conn.Close
Set conn = Nothing
' 插入成功后,重定向到管理页面
Response.Redirect("default.asp")
%>
第五部分:添加 CSS 样式 (styles/main.css)
为了让我们的博客看起来更美观,添加一些简单的 CSS。
/* File: styles/main.css */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
color: #333;
}
#header {
background-color: #333;
color: white;
padding: 1rem;
text-align: center;
}
#header h1 a {
color: white;
text-decoration: none;
}
nav a {
color: white;
margin: 0 15px;
text-decoration: none;
}
nav a:hover {
text-decoration: underline;
}
#main-content {
max-width: 800px;
margin: 20px auto;
padding: 20px;
background-color: white;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
#footer {
text-align: center;
padding: 20px;
margin-top: 20px;
background-color: #333;
color: white;
}
.article-list li {
border-bottom: 1px solid #ddd;
padding: 15px 0;
}
.post-meta {
font-size: 0.9em;
color: #777;
}
.full-article {
line-height: 1.6;
}
.full-article h1 {
border-bottom: 2px solid #333;
padding-bottom: 10px;
}
textarea {
width: 100%;
padding: 10px;
border-radius: 4px;
border: 1px solid #ccc;
}
第六部分:总结与后续学习
恭喜!你已经成功构建了一个功能完整的 ASP 经典博客网站,这个项目涵盖了:
- ASP 基础:
<%...%>代码块、VBScript 语法。 - 数据库交互:ADO 连接、SQL 查询(SELECT, INSERT)、Recordset 对象的使用。
- 会话管理:
Session对象用于用户认证(登录状态)。 - 包含文件:
<!--#include file="..."-->实现代码复用。 - 表单处理:
Request.Form获取 POST 数据。 - 安全性基础:简单的登录验证、防止 SQL 注入(使用
IsNumeric检查 ID)。
可以继续扩展的功能:
- 文章编辑和删除:创建
edit_article.asp和delete_article.asp,功能与添加文章类似,但需要先根据 ID 查询出原有数据。 - 用户评论系统:创建一个
Comments表,并在article.asp中显示评论,并提供一个表单让访客提交评论。 - 分页功能:当文章很多时,首页需要分页显示,这需要用到
Recordset的PageSize和AbsolutePage属性。 - 密码加密:使用 MD5 或 SHA256 等哈希算法对后台密码进行加密存储,而不是明文。
- 更友好的URL:使用 URL 重写技术,将
article.asp?id=123变成更美观的article/123/my-first-post.asp。
这个教程为你打下了坚实的基础,ASP 经典虽然技术较老,但其核心思想(服务器端脚本、数据库交互)在现代 Web 开发中依然非常重要,祝你在 ASP 的学习道路上越走越远!
