目录
- Response 对象简介
- Response.Write 方法
- Response.Redirect 方法
- Response.End 方法
- Response.Buffer 和 Response.Flush 方法
- Response.Expires 和 Response.ExpiresAbsolute 方法
- Response.Cookies 对象
- Response.ContentType 属性
- Response.Charset 属性
- 完整示例代码
- 参考资料
- 出站链接
1. Response 对象简介
Response
对象用于向客户端(浏览器)发送 HTTP 响应数据,例如网页内容、重定向、Cookies 以及 HTTP 头信息等。它是 ASP 编程中最常用的内置对象之一。
📌 常见功能:
- 输出网页内容 (
Response.Write
) - 页面跳转 (
Response.Redirect
) - 终止脚本执行 (
Response.End
) - 控制页面缓存 (
Response.Expires
) - 发送 Cookies (
Response.Cookies
)
2. Response.Write 方法
用于向客户端输出文本或变量值。
示例:
<%
Response.Write "Hello, ASP!"
%>
等价于:
<%
Response.Write("Hello, ASP!")
%>
🔹 输出变量:
<%
Dim name
name = "张三"
Response.Write "欢迎 " & name & " 来到我们的网站!"
%>
3. Response.Redirect 方法
用于将用户重定向到另一个 URL。
示例:
<%
Response.Redirect "https://www.example.com"
%>
🔹 带参数跳转:
<%
Response.Redirect "welcome.asp?user=zhangsan"
%>
⚠️ 注意事项:
Response.Redirect
之后的代码仍然会执行,建议在其后添加Response.End
以终止脚本运行。
4. Response.End 方法
用于立即终止页面的执行。通常与 Response.Redirect
一起使用。
示例:
<%
Response.Write "这条信息不会显示!"
Response.End
Response.Write "这条信息不会执行!"
%>
🔹 与 Redirect 配合使用:
<%
Response.Redirect "https://www.example.com"
Response.End
%>
5. Response.Buffer 和 Response.Flush 方法
🔹 Response.Buffer = True
:启用缓冲,所有输出内容在发送前都会被存储在服务器的内存中。
🔹 Response.Flush
:立即发送缓冲区中的数据到浏览器,而不结束脚本。
示例:
<%
Response.Buffer = True
Response.Write "第一行<br>"
Response.Flush
Response.Write "第二行"
%>
📌 好处:
Response.Buffer = True
可以在发送完整 HTML 之前修改 HTTP 头部(如Response.ContentType
)。Response.Flush
允许分批发送内容,提高用户体验。
6. Response.Expires 和 Response.ExpiresAbsolute 方法
🔹 控制页面缓存,用于指定页面在浏览器缓存中的存储时间。
🔹 Response.Expires
(以分钟为单位):
<%
Response.Expires = 30 ' 页面在缓存中存储 30 分钟
%>
🔹 Response.ExpiresAbsolute
(指定绝对过期时间):
<%
Response.ExpiresAbsolute = #2025-12-31 23:59:59#
%>
📌 如果希望浏览器不缓存**页面:
<%
Response.Expires = 0
Response.CacheControl = "no-cache"
%>
7. Response.Cookies 对象
用于向客户端发送 Cookie。
🔹 设置 Cookie:
<%
Response.Cookies("username") = "zhangsan"
Response.Cookies("username").Expires = Date + 7 ' 设置 7 天后过期
%>
🔹 读取 Cookie:
<%
Response.Write "用户名:" & Request.Cookies("username")
%>
🔹 多级 Cookie(子键值):
<%
Response.Cookies("user")("name") = "zhangsan"
Response.Cookies("user")("email") = "zhangsan@example.com"
%>
8. Response.ContentType 属性
用于指定服务器返回的 MIME
类型,默认是 text/html
。
🔹 返回 JSON 数据:
<%
Response.ContentType = "application/json"
Response.Write "{""message"": ""Hello, JSON!""}"
%>
🔹 返回 Excel 文件:
<%
Response.ContentType = "application/vnd.ms-excel"
Response.Write "ID, Name, Score<br>"
Response.Write "1, 张三, 90<br>"
Response.Write "2, 李四, 85"
%>
9. Response.Charset 属性
指定网页的字符编码,防止乱码。
🔹 设置 UTF-8 编码:
<%
Response.Charset = "UTF-8"
Response.ContentType = "text/html"
%>
🔹 设置 GB2312 编码:
<%
Response.Charset = "GB2312"
%>
10. 完整示例代码
<%
' 启用缓冲
Response.Buffer = True
' 设置编码
Response.Charset = "UTF-8"
' 设置页面缓存
Response.Expires = 0
Response.CacheControl = "no-cache"
' 输出内容
Response.Write "<h2>ASP Response 对象示例</h2>"
' 发送 Cookie
Response.Cookies("username") = "zhangsan"
Response.Cookies("username").Expires = Date + 7
' 重定向(5 秒后)
Response.Write "<p>5 秒后跳转到首页...</p>"
Response.Flush
Response.Redirect "index.asp"
Response.End
%>
11. 参考资料
- Microsoft Docs – ASP Response 对象
- W3Schools – ASP Response Methods
- IIS 官方文档 – ASP 编程指南
12. 出站链接
这样,你就能全面掌握 ASP Response 对象 的使用方法,包括内容输出、重定向、Cookies 处理、缓存控制等!🚀
发表回复