Response Write Object in ASP

Use Response.Write to send text, a variable, or generated HTML to the browser from Classic ASP (VBScript). The example below displays Hello World.

Response.Write "Hello World"

When used in a standalone .asp file, place VBScript inside <% and %> delimiters. See the complete page example further below.

On this page — Show page sectionsHide page sections

Response.Write syntax

Response.Write expression

The expression may be a string literal, number, variable or combined expression. Response.Write writes to the HTTP response body; the browser then interprets the resulting text as HTML if the page is served as HTML. This method does not automatically add a newline or an HTML break.

ExpressionPurpose
Response.Write "Hello"Writes a literal string.
Response.Write 25Writes a numeric value.
Response.Write myNameWrites a variable.
Response.Write "Hi " & myNameJoins text and a variable.
Response.Write "<br>"Writes an HTML line-break tag.

Print double quotation marks in a string

VBScript string literals use double quotes. To include a double-quote character inside the string, write it twice (""). This example demonstrates the rule:

Response.Write "Raju told ""He is learning ASP from plus2net"" So you can also learn from here "

Output:

Raju told "He is learning ASP from plus2net" So you can also learn from here

This is VBScript string escaping, not HTML output encoding. See quotation marks and escaping in VBScript.

Build an HTML attribute containing quotes

When creating HTML tags inside a VBScript string, double the quotes around an attribute value:

Response.Write "<a href=""/asp-tutorial/site_map.php"">ASP tutorials</a>"

The browser receives a normal <a href="/https/www.plus2net.com/asp-tutorial/site_map.php"> link. For fixed HTML that does not need to be constructed dynamically, writing HTML directly in the page is often simpler.

Display HTML tags and line breaks

HTML tags produced by Response.Write are interpreted by the browser. Use a matching closing tag when you generate HTML. For example, <strong> and </strong> must appear together:

Response.Write "Use <strong>bold text</strong> to highlight a message."

Browser display: Use bold text to highlight a message.

For HTML markup basics, see the HTML tags tutorial. To show literal tag characters, use Server.HTMLEncode() instead of outputting raw HTML tags:

Response.Write Server.HTMLEncode("<strong>Hello</strong>")

Output:

<strong>Hello</strong>

HTML line breaks versus text newlines

Response.Write "First line<br>"
Response.Write "Second line<br>"
Response.Write "Third line"

Output:

First line
Second line
Third line

A <br> tag creates a visible line break in HTML. Inserting vbCrLf into HTML source alone usually does not create a visible line break in normal browser rendering. For multiline text entered by visitors, see convert text line breaks for HTML display.

Output variables, numbers and dates

Dim userName, total
userName = "Raju"
total = 25
Response.Write "Name: " & userName & "<br>"
Response.Write "Total: " & total

Output:

Name: Raju
Total: 25

Use the ampersand (&) to concatenate strings and values. See concatenating strings in VBScript.

Display the current server date and time

Response.Write "Server date and time: " & Now()

The result changes when the page runs. It uses the server clock, not necessarily the visitor’s local time. See Date, Time and Now in Classic ASP.

Display visitor-provided text safely

When a value comes from a form, URL, database or other untrusted source, do not write it into HTML without context-appropriate encoding. For normal HTML text content, Server.HTMLEncode() converts characters such as < and & into harmless text.

Dim visitorName
visitorName = Request.QueryString("name")
Response.Write "Hello, " & Server.HTMLEncode(visitorName)

For a request like ?name=Raju, the browser displays Hello, Raju. Text resembling HTML markup is shown as text rather than executed as markup.

Output safety: Server.HTMLEncode() is appropriate for HTML text nodes. HTML attribute values, JavaScript strings, CSS, and URLs have different safety requirements. Do not use Response.Write to insert untrusted text directly into a <script> element.

Raw text compared with HTML-encoded output

Dim inputText
inputText = "<b>Example</b>"
Response.Write "Raw HTML: " & inputText & "<br>"
Response.Write "Encoded text: " & Server.HTMLEncode(inputText)

The first line creates bold markup; the second displays the literal characters <b>Example</b>. Use the second approach for user-generated content.

Complete form example using Request.Form()

This example displays a submitted name only after the visitor uses the button. The value is read from Request.Form() and encoded before output.

<form action="" method="post">
  <label for="visitorName">Your name</label>
  <input id="visitorName" name="visitorName" type="text" required>
  <button type="submit">Show greeting</button>
</form>
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
    Dim visitorName
    visitorName = Trim(Request.Form("visitorName"))
    If Len(visitorName) > 0 Then
        Response.Write "<p>Hello, " & Server.HTMLEncode(visitorName) & "</p>"
    Else
        Response.Write "<p>Please enter your name.</p>"
    End If
End If
%>

The browser-side required attribute improves usability, but the Len() check still validates the submitted value on the server. Add additional validation when storing or using user input.

Generate repeated HTML output in a loop

When you need several similar items, combine Response.Write with a For...Next loop. Fixed HTML structure and loop-generated rows are kept separate here:

<ul>
<%
Dim i
For i = 1 To 5
    Response.Write "<li>Item " & i & "</li>"
Next
%>
</ul>

Browser display:

  • Item 1
  • Item 2
  • Item 3
  • Item 4
  • Item 5

Build table rows

<table class="table table-bordered">
  <thead><tr><th>Number</th><th>Square</th></tr></thead>
  <tbody>
<%
Dim i
For i = 1 To 3
    Response.Write "<tr><td>" & i & "</td><td>" & (i * i) & "</td></tr>"
Next
%>
  </tbody>
</table>
NumberSquare
11
24
39

Response.Write versus the ASP output expression

Classic ASP supports the shorter <%= expression %> syntax for displaying a value directly in markup. Both forms add content to the response:

<%
Dim city
city = "Hyderabad"
Response.Write "<p>City: " & Server.HTMLEncode(city) & "</p>"
%>
<p>City: <%= Server.HTMLEncode(city) %></p>

The two lines display the same city. The shorter form is convenient in HTML templates; Response.Write is useful inside VBScript statements and procedures. Neither form automatically encodes untrusted data.

Complete standalone Classic ASP page

Save the following example as response-write-example.asp on an IIS server with Classic ASP enabled. It includes the ASP language directive, HTML structure, a variable, and an encoded output example.

<%@ Language="VBScript" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Response.Write demo</title>
</head>
<body>
  <h1>Response.Write example</h1>
<%
Dim message
message = "Welcome to plus2net.com"
Response.Write "<p>" & Server.HTMLEncode(message) & "</p>"
Response.Write "<p>Server date: " & Server.HTMLEncode(CStr(Date())) & "</p>"
%>
</body>
</html>

The displayed date depends on the server clock and regional settings. In a real application, keep presentation and complex business logic organized rather than building every HTML element with strings.

Troubleshooting common problems

ProblemExplanation or solution
HTML tags appear as plain textCheck whether they were encoded using Server.HTMLEncode(). Encoding is appropriate for displaying user text but not when intentionally constructing trusted markup.
Text is on one lineResponse.Write does not add line breaks. Use <br> or paragraphs for visible HTML breaks.
Quotes produce a syntax errorDouble quotation marks within VBScript string literals: "".
Output shows user-supplied HTML markupUse Server.HTMLEncode() when writing the value into HTML text content.
Nothing displays in the browserVerify the page is served as .asp by IIS, Classic ASP is enabled, and the code is inside <% ... %>.
Headers cannot be changedHeaders and redirects may need to be set before buffered output is flushed. See response buffering and Response.Flush().

Does Response.Write add a newline automatically?

No. It writes the supplied value. Add an HTML <br> element or use semantic paragraphs or list items to control visible layout.

Is Response.Write the same as Response.Flush?

No. Response.Write adds content to the response. Response.Flush() sends buffered content to the client when buffering is enabled.

Can I use Response.Write inside an If or a loop?

Yes. It works inside If...Then...Else statements, For...Next loops, procedures, and other VBScript code blocks.

How can I stop output after a condition?

For details on ending request processing, see Response.End(). Use it intentionally because it stops processing the remaining ASP script.

Can Response.Write output JavaScript?

It can generate HTML that contains scripts, but concatenating untrusted values into JavaScript creates security risks. Use context-appropriate encoding and avoid building scripts from visitor input.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer