Você está na página 1de 19

Basic Response and Request Processing in ASP.

IT 4203 Advanced Web Development

Jack G. Zheng Fall 2010


Dynamically generating content Processing incoming HTTP messages and user data

URL parameters HTML form data

Request and Response

Web servers and web clients communicate through HTTP messages (protocols)

Request message: client server Response message: server client

Most HTTP communications consist of a cycle of request and response

Request: getting the resource, with user data submitted to server. The server will process user data. Response: generating content and send it back to browsers.

ASP.Net Response Processing

Most of the HTTP response processing functionalities are provided through the System.Web.HttpResponse class

The methods and properties of the HttpResponse class are exposed through the Response property of the Page class (ASP.Net web forms). Response is a built-in object and can be directly used in .aspx pages and .aspx.cs code-behind pages.

Basic methods in ASP.Net to generate dynamic content

Response.Write() Expression code block: <%= %> Server controls



The method writes information to an HTTP response output stream.

String title = Hello; Response.Write(<h1>" +title+"</h1>");


A shortcut to print out a value or an expression

<%= [expression]%>

An expression can be a variable, formula, object property, string concatenation, method with return value, or anything that returns a value
<% String title = Hello; %> <%= Title%>

Expression code block is equivalent to Response.Write(), and are mixed with static content

Basic Methods to Mix Dynamic Content and Static Content (1)


String concatenation: + operator

String title = Hello; Response.Write(<h1>" +title+"</h1>");


Expression (spaghetti/mixed output)

<h1><%= title %></h1>


Response.Write(String.Format(<h1>,0-</h1>, title));

Basic Methods to Mix Dynamic Content and Static Content (2)




String title = Hello; StringBuilder html=new StringBuilder(); html.Append(<h1>); html.Append(title); html.Append(<h1>); Response.Write(html.ToString());

ASP.Net Request Processing

Most of the HTTP request processing functionalities are provided through the System.Web.HttpRequest class

The methods and properties of the HttpRequest class are exposed through the Request property of the Page class (ASP.Net web forms). Request is a built-in object and can be directly used in .aspx pages and .aspx.cs code-behind pages.

Major request method type


User data is sent as part of the request URL; no content in request message body Triggering actions: address bar (in browser), link, form, User data is sent in the request message body Triggering actions: form,


Two basic ways to accept user input

URL parameter (Get) HTML Form (Get or Post)

URL Parameter

URL parameter


QueryString Property wraps URL parameters to collections of parameter names and values

Example URL: /page.aspx?para1=ibm&para2=msft

String p1 = Request.QueryString["para1"]; Use name Or String p1 = Request.QueryString[0]; Use index
Parameter values are always treated as strings

URL Parameters Applications

User input


Data item details in a template page

http://www.newegg.com/Product/Product.aspx?Item=N82E16827135204 http://www2.cis.gsu.edu/cis/people/display.asp?pk=3

Page content selection

http://forum.java.sun.com/category.jspa?categoryID=20 http://cis3270.jackzheng.net/index.jsp?page=schedule


http://finance.yahoo.com/q/bc?s=MSFT&t=5d&l=on&z=l&q=l http://msdn.e-academy.com/elms/Storefront/Home.aspx?campus=gsu_cis

Page display/format, page content, error message,


HTML Form Structure

<form method="post" action=processing.aspx"> Action points to the target which processes HTTP method: form data. It can be another page/resource get or post (even in another site) or the same page. (Form elements and other normal HTML tags can be place here.) <input type="submit" value="Submit" /> </form> A submit button triggers the
submission action.

HTML Form Elements

Form element types

Textbox: <input type=text> Password: <input type=password> Text area: <textarea> Combo box: <select> List: <select multiple=multiple> Checkbox: <input type=text> Radio button: <input type=text> Hidden: <input type=hidden> Buttons : Submit, Reset

More details


HTML Form Processing

Each form element has name attribute and value attribute

name attribute parameter name value attribute parameter value

Form data can be sent either using Get or Post


Form data are encoded as URL parameters Using Request.QueryString to process form data Using Request.Form



Used when the HTML form method is set to post

String p1 = Request.QueryString*textbox1"+; Use parameter name Or String p1 = Request.QueryString[0];
Use index

Parameter values are always treated as strings


Input Validation

Server side validation

Use server side programming capability to validate user inputs Additional request/response cycles Need special handling to keep user data in the form

Client side validation

Use JavaScript to check form data before the form is submitted to the server side No additional transmission between server and browser <form onsubmit=javascript: return checkForm()>
checkForm() is a JavaScript function that either returns true or false, depending on validation results

Retrieving More Request and Server Information

Request object has more properties about

Brower type Client host and IP address URL information Requested resource information Referrer

Most of these can be retrieved from the Request Object

http://msdn.microsoft.com/enus/library/system.web.httprequest(v=VS.90).aspx And particularly

Request.Browser Request.Url Request.ServerVariables



Key concepts

HTTP request and response URL parameter HTML form, method, action HTML form element name and value attribute

Key skills

Use Request (System.Web.HttpRequest) and Response (System.Web.HttpResponse) object in ASP.Net pages. Create dynamic content and mix them with static content. Get user input through URL parameters or HTML forms.


More Resources




http://msdn.microsoft.com/enus/library/system.web.httpresponse(v=VS.90).asp x