Request object and response object

Source: Internet
Author: User

Request object and response object

 

I. Request object

1. The client input information can be obtained through the request object. The request object contains the request information sent from the client.

Request Parameters are part of a request. They are transmitted from the client to the JSP/servlet container as strings and used to initialize the request object.

 

2. The request object is the javax. servlet. http. httpservletrequest interface. The main available methods are as follows:

1) getcookies ()

This method returns an array of client cookie objects, which are the instance objects of javax. servlet. http. Cookie.

 

2) getheader (string name)

This method can obtain the information of the specific HTTP header file defined by the HTTP protocol. Name indicates the name of the HTTP File Header, for example, User-Agent.

 

3) getattribute (string name)

Returns the value bound to the name attribute. If no value exists, null is returned.

 

4) getattributenames ()

Returns the names of all attributes bound to the current request object. The returned value is an enumerated object.

 

5) getheadernames ()

This method returns an enumeration object that contains the names of each HTTP header file contained in the current request object.

 

6) getmethod ()

This method can return the current client-side transmission protocol to the server, possibly post, get, put, and so on.

 

7) getparameter (string name)

You can obtain the information sent from the client to the server.

 

8) getparameternames ()

You can obtain the names of all parameters transmitted from the client to the server. The return value is an enumeration type.

 

9) getparametervalues (string name)

Unlike getparameter (string name), getparameter (string name) is used to return only the current value of name, while the previous value is ignored, while getparametervalue (string name) all values are returned.

 

10) getquerystring ()

You can use this method to obtain the request string, which is transmitted to the server by the client in get mode.

 

11) getrequesturl ()

The getrequesturl () method can obtain the URL address corresponding to the current request object. Some people say the URL address requested by the client through the browser.

 

12) getservletpath ()

This method can obtain the servlet Address requested by the client.

 

13) setattribute (string name, java. Lang. Object OBJ)

This method can bind a parameter to the current request object.

 

14) getservername ()

This method returns the name of the server.

 

15) getserverport ()

Used to obtain the port of the HTTP service provided by the server.

 

16) getremoteaddr ()

This method is used to obtain the client address for sending an HTTP request. With this method, we can shield access from some IP addresses.

 

17) getremotehost ()

This method obtains the name of the client computer currently connected to HTTP, and returns an IP address.

 

18) getprotocol ()

You can obtain the name of the protocol used by the client to send information to the server.

 

Example:

 

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <body bgcolor = "white"> <br /> <p> Request Information </p> <br/> <font size = "4"> <br/> jsp request method: <% Out. println (request. getmethod (); %> <br/> request URI: <% Out. println (request. getrequesturi (); %> <br/> request protocol: <% Out. println (request. getprotocol (); %> <br/> Servlet Path: <% Out. println (request. getservletpath (); %> <br/> Path Info: <% Out. println (request. getpathinfo (); %> <br/> path translated: <% Out. println (request. getpathtranslated (); %> <br/> query string: <% Out. println (request. getquerystring (); %> <br/> Content Length: <% Out. println (request. getcontentlength (); %> <br/> content type: <% Out. println (request. getcontenttype (); %> <br/> server name: <% Out. println (request. getservername (); %> <br/> server port: <% Out. println (request. getserverport (); %> <br/> remote user: <% Out. println (request. getremoteuser (); %> <br/> remote address: <% Out. println (request. getremoteaddr (); %> <br/> remote host: <% Out. println (request. getremotehost (); %> <br/> authorization scheme: <% Out. println (request. getauthtype (); %> <br/> <HR> <br/> the browser you are using is: <% Out. println (request. getheader ("User-Agent ")); %> <br/> <HR> <br/> </font> <br/> </body> <br/> </ptml>

 

 

Ii. Response object

1. The response object is used to process HTTP connection information, such as cookies and HTTP file header information. It has many functions that match the request object.

 

2. Common Methods for response objects:

1) addcookie)

When accessing a website, we can use this method to add a cookie object, send it to the client, and save it to a specific directory of the client operating system, used to save specific customer information. When the user accesses the browser again, the browser will automatically send the cookie object to the server, and call the getcookie () method of the request object to obtain all existing cookie objects, getcookie returns an array. Cookies are proprietary to different customers.

 

2) addheader (string name, string value)

Addheader is used to add HTTP file header information, which will be uploaded to the client, send specific HTTP file header information, and control the connection between the client and the server.

 

3) containsheader (string name)

This method is used to determine whether the specified Http File Header exists. The returned value is boolean.

 

4) senderror (int SC)

This method is used to send an error message to the client, for example, 505: internal message of the server.

 

5) sendredirect (string URL)

The functions of the <JSP: Forward> action commands are the same, but it is not convenient to pass HTTP parameters to the target program. before using this method, make sure that no visible output of the JSP program is sent to the client.

 

6) setheader ()

Is a common header setting method, you can use it to set any "name/value" header.

7) setintheader ()

Used to set the version of the integer header.

8) setdateheader ()

It is the date set version of setheader (). The second parameter is the long value of date. 0 indicates GMT.

 

9) setstatus ()

Is used to set the response status code, for example, 404 not found, the httpservletresponse class provides some helper constant settings, for example, SC _not_found indicates the 404 status code (you can query the relevant mnemonic constants in the servlet API file ).

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.