response request

4
Built-in Objects in ASP.NET ASP.NET provides some Classes which make web development easier. Some of the classes used specifically for ASP.NET development are: 1. System.Web.HttpRequest 2. System.Web.HttpResponse 3. System.Web.SessionState.HttpSessionState 4. System.Web.ApplicationState What is a web request? In simple words, a web request a 'request sent from a client to the server, asking for a specific web page'. When you type a URL in a web browser or when you click on a hyper link in any web page, your browser is actually making a 'Request' to the server for the specific URL. Who makes the Request? If you are using a browser to view a web page, then your browser is making the actual request to the server. You simply typed a URL in the browser, but the browser does lot of work in the background. It composes a Request in the proper format that the server can understand and sends the request to the server using the HTTP protocol. Composing the Request Composing and sending the request involves lot of work, even though a user need not worry about it. It is the responsibility of the browser to compose and send the request in the proper format. What is in a Request? A request obviously includes the URL which is requested. In addition to that, a Request also includes several other information, including: 1. Details about the browser who makes the request (like version number, browser type etc) 2. Computer information like screen resolution, IP address of the user etc. 3. Cookies - the information stored in the client machine by the same web site. 4. Data inputted by the user (for example, when you register in a site, you are entering your registration details in the input fields. These details are sent to the server as part of the 'Request') ASP.NET Request Object The ASP.NET provides a class called HttpRequest which is defined in the namespace System.Web. This class provides various methods and properties which help you use various information related to a web request. An instance of this class is created by default in all the pages, so that you can use this object without creating again each time in all the pages. The name of this object is Request. In old ASP days, the Request object was the only way to retrieve the input data entered by the user in the input fields in the page. ASP.NET provides the event handling mechanism and web controls so that you can access user inputs in a more easy way.

Upload: jenishbhavsarjoshi

Post on 10-Feb-2016

1 views

Category:

Documents


0 download

DESCRIPTION

Built-in objects in ASP.NET. Request and Response are two built-in objects used to redirect information from one page to another in the form of Query String. This query string is retrieved in form of Request.QueryString()...

TRANSCRIPT

Page 1: Response Request

Built-in Objects in ASP.NET ASP.NET provides some Classes which make web development easier. Some of the classes used specifically for ASP.NET development are:

1. System.Web.HttpRequest 2. System.Web.HttpResponse 3. System.Web.SessionState.HttpSessionState 4. System.Web.ApplicationState

What is a web request? In simple words, a web request a 'request sent from a client to the server, asking for a specific web

page'. When you type a URL in a web browser or when you click on a hyper link in any web page, your

browser is actually making a 'Request' to the server for the specific URL.

Who makes the Request? If you are using a browser to view a web page, then your browser is making the actual request to

the server. You simply typed a URL in the browser, but the browser does lot of work in the background. It composes a Request in the proper format that the server can understand and sends the request to the server using the HTTP protocol.

Composing the Request Composing and sending the request involves lot of work, even though a user need not worry about

it. It is the responsibility of the browser to compose and send the request in the proper format.

What is in a Request? A request obviously includes the URL which is requested. In addition to that, a Request also includes several other information, including: 1. Details about the browser who makes the request (like version number, browser type etc) 2. Computer information like screen resolution, IP address of the user etc. 3. Cookies - the information stored in the client machine by the same web site. 4. Data inputted by the user (for example, when you register in a site, you are entering your

registration details in the input fields. These details are sent to the server as part of the 'Request')

ASP.NET Request Object The ASP.NET provides a class called HttpRequest which is defined in the namespace System.Web. This class provides various methods and properties which help you use various information related

to a web request. An instance of this class is created by default in all the pages, so that you can use this object without

creating again each time in all the pages. The name of this object is Request. In old ASP days, the Request object was the only way to retrieve the input data entered by the user

in the input fields in the page. ASP.NET provides the event handling mechanism and web controls so that you can access user inputs in a more easy way.

Page 2: Response Request

Commonly used Methods and Properies of Request object 1. Request.FilePath – This property returns the currently executed file path. 2. Request.QueryString() – This method is used to retrieve short key value pairs passed along with the

URL. 3. Request.Cookies –

Cookies are used to store small pieces of information in the client computer. Many web sites store information like when did you visit their site last, your user name etc.

When you login to a site and choose the option 'Remember my password', do you know how they remember your password? Your user name is stored in a cookie in your computer.

For example, when you login to this site, if you choose the option 'Remember my password', we will save your user name into a cookie in your computer. If you come back to our site again later, we will read the value from the Cookie as shown below: Dim cookie As System.Web.HttpCookie = Request.Cookies("AspSpiderUserId") if cookie is nothing then Response.write ("Cookie 'AspSpiderUserId' not found.") else Response.write ("Value of Cookie 'AspSpiderUserId': " & cookie.Value) end if If the user name already exists in Cookie, we understand that you selected the option 'Remember my password' last time. So, we will not ask for your password again. We will automatically login you to the application. Many sites use Cookies to store more information like when did you visit their site last, what are the pages you visited etc. For example, if you go to an online shopping site and add few items to the shopping cart, they will store this information to the Cookies. When you come back to the same site again, those items will be automatically there in your Shopping cart because they had saved it in your computer cookie and put into your shopping cart when you came back to the site. 4. Request.ServerVariables

This property can be used to extract specific information about the client who is making the request.

Eg: if you want to find the server IP address of the computer who is visiting your web page – dim ipAddress as string = Request.ServerVariables("REMOTE_HOST'") Response.Write ("Your IP Address is : " & ipAddress) You can find which version of browser they are using to access your web site. dim browser as string = Request.ServerVariables("HTTP_USER_AGENT'") Response.Write ("You are using this browser to access our web site: " & browser) There are several server variables available to find more information from the Request. Try the following server side code in your web page: dim str as string For Each str in Request.ServerVariables.AllKeys Response.write ("Key : '" & str & "' - Value '" & Request.ServerVariables(str) & "'")

Page 3: Response Request

What is a Response?

In the web server world, a Response is exactly opposite to the Request. A 'Response' is the message sent from the web server to the client, when client makes a 'Request'. For each request from a client, the server gives a response, unless there is an error.

When you type a URL in a web browser or when you click on a hyper link in any web page, your browser makes a 'Request' to the server for the specific URL. Then server process the request and send a response back.

How does a browser process the Response? The response includes several information about the page requested, including the cookies to be

saved on the client machine, the actual content to be displayed to the user, etc. The browser accepts the response and processes it. Browser does several things including saving the

cookies, checking the security etc and then displays the page content to the user.

ASP.NET Response Object The ASP.NET provides a class called HttpResponse which is defined in the namespace System.Web. This class provides various methods and properties which help you use various information related

to a web response. An instance of this class is created by default in all the pages, so that you can use this object without

creating again each time in all the pages. The name of this object is Response. In old ASP days, the Response object was the only way to write data to the page. ASP.NET provides

the event handling mechanism and web controls so that you can write content to the page in an easier way.

Frequently used Methods and Properies of Request object 1. Response.Write() – This method is used to write dynamic text to the web page. In old ASP days, this

was the only way to generate dynamic text and display to the user. Response.Write(DateTime.Now.ToString()) The above code will generate the current time as text and display to the user. ASP.NET provides several web controls including the Label control which allow you to specify the exact location where you want the control to be displayed. Due to this, the Response.Write() method is not widely used now. 2. Response.Cookies Cookies are used to store small pieces of information in the client computer. In ASP.NET, the Response object is used to send cookies to the client browser. If you select the

'Remember Me' option at the time of login, we use the following code to store your user id in a cookie in your computer.

When you come back to this site later, we retrieve the user id from the Request and load your information automatically, without asking you to login again.

Response.Cookies("AspSpiderUserId").Value = "your user id" Response.Cookies("AspSpiderUserId").Expires = DateTime.Now.AddDays(7)

Page 4: Response Request

We use the above code to store your user id in a cookie. The name of the cookie used is "AspSpiderUserId". The cookie will expire after 7 days. This means, if you come back to our site after 7 days, we will not remember you! So, come back often! Dim cookie As System.Web.HttpCookie = Request.Cookies("AspSpiderUserId") if cookie is nothing then Response.write ("Cookie 'AspSpiderUserId' not found.") else Response.write ("Value of Cookie 'AspSpiderUserId': " & cookie.Value) end if Response.Redirect() This may be the most frequently used method of the Response object. Response.Redirect() is used to redirect user from one page to another page or website. Response.Redirect("Login.aspx") When the above line of code is executed, the page will be redirected to the login page. Let us see another example. if (bStatus = True) then Response.Redirect("Success.aspx") else Response.Redirect("Error.aspx") end if The above code redirects to error or success page according to the status of some operation performed. For example, when user try to login to the website, if the user name and password are correct, the page can be re directed to a welcome page. Otherwise, an error page can be displayed.

Questions

1. Explain features added in ASP.NET. How can you justify that ASP.NET provides more developer productivity?

2. ADO.NET Xerox