๐ค ASP โ Response / Request โ Handle Server Communication in Classic ASP
๐งฒ Introduction โ What Are Response and Request in Classic ASP?
In Classic ASP, the Request and Response objects are core components that allow your server-side scripts to interact with browser clients.
Requestis used to receive data from a client (such as form input, cookies, or query strings).Responseis used to send data back to the client (such as HTML content, headers, cookies, or redirects).
๐ฏ In this guide, youโll learn:
- How to use the
Requestobject to retrieve user input - How to use the
Responseobject to send content and headers - Real-world usage with forms, cookies, and redirects
- Output examples and best practices
๐ฅ The Request Object โ Receiving Client Data
๐ Common Request Collections
| Collection | Description |
|---|---|
Request.Form | Data from HTML forms using POST |
Request.QueryString | Data from the URL (GET method) |
Request.Cookies | Cookies sent by the client |
Request.ServerVariables | Info about the server and environment |
Request.Headers | HTTP headers sent from client (IIS 5+) |
โ Example: Read Form Input
HTML form (POST):
<form method="post" action="submit.asp">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
submit.asp (ASP):
<%
Dim user
user = Request.Form("username")
Response.Write "Hello, " & user
%>
๐งช Output:Hello, Alex (if Alex was entered)
๐ Example: Read Query String Parameters
<%
Dim page
page = Request.QueryString("id")
Response.Write "Page ID: " & page
%>
๐งช URL: example.asp?id=5
๐งช Output: Page ID: 5
๐ช Example: Access Client Cookies
<%
Dim theme
theme = Request.Cookies("Theme")
Response.Write "Your theme: " & theme
%>
๐ค The Response Object โ Sending Output to Browser
โ
Basic Output with Response.Write
<%
Response.Write "Welcome to my site!"
%>
๐งช Output:Welcome to my site!
๐ Redirect to Another Page
<%
Response.Redirect "home.asp"
%>
๐ Immediately redirects the user to home.asp.
๐ช Set a Cookie
<%
Response.Cookies("Theme") = "Dark"
Response.Cookies("Theme").Expires = Date + 30
%>
๐งช Sets a cookie named Theme valid for 30 days.
๐งพ Set Content Type (e.g., JSON or CSV)
<%
Response.ContentType = "application/json"
Response.Write "{""status"":""success""}"
%>
๐งช Used in API or AJAX responses.
โ ๏ธ Common Mistakes to Avoid
| Mistake | Correction |
|---|---|
Using Request("field") without checking | Use IsEmpty() or Len() to validate input |
Writing to Response after Redirect | Always Exit after Response.Redirect() |
| Not escaping user input | Always sanitize inputs to prevent XSS or SQLi |
๐ง Bonus: Loop Through All Form Values
<%
Dim field
For Each field In Request.Form
Response.Write field & ": " & Request.Form(field) & "<br>"
Next
%>
๐งช Output: Displays all submitted form fields and their values.
๐ Best Practices for Request/Response
โ Do:
- Use
Request.Form("field")for POST data - Always validate and sanitize input values
- Use
Response.Writeto debug server logic Exitimmediately afterResponse.Redirect
โ Avoid:
- Mixing GET and POST unless intentional
- Trusting
Requestdata directly in queries - Forgetting to set
ContentTypefor APIs
๐ Summary โ Recap & Next Steps
The Request and Response objects are the heartbeat of Classic ASP communication. Together, they allow your ASP pages to receive user input, send dynamic content, and manage browser interaction smoothly.
๐ Key Takeaways:
- Use
Request.FormandRequest.QueryStringto capture input - Use
Response.Write,Redirect, andCookiesto control output - Always validate and sanitize data from
Request
โ๏ธ Real-world Use Cases:
- Handling form submissions
- Redirecting users after login
- Setting language/theme preferences using cookies
โ FAQs โ Classic ASP Request / Response
โ What’s the difference between Request.Form and Request.QueryString?
โ
Request.Form is for POST data, while Request.QueryString reads from the URL (GET data).
โ How do I output a variable in Classic ASP?
โ
Use Response.Write, or the shorthand <%= variable %>.
โ Can I write to the response after a redirect?
โ No. Use Response.Redirect("url") followed by Response.End or Exit.
