[JavaWeb] Overview of Response and Request objects

Table of contents

1. What is Response

1. Draw a picture to illustrate the Response object

2. API of Response object

2.1Response about the response line method

2.2 Response method on response header

2.3 Response method on response body

2.4 Response other APIs 

3. Code demonstration of the API of the Response object

3.1 Set the status code

3.2 Complete Redirection 

3.3 Timing refresh effect

4. Extension of page timing jump

4.1 Complete the page jump in HTML

5. Chinese garbled processing of Response object response

5.1Response responds to the page in Chinese

2. An overview of the Request object and an introduction to the API

1. What is the Request object

API of the Request object:

1.1 Get client information:

2.2 The method of obtaining the request header 

2.3 Method of obtaining request parameters 

2.4Request as a method of domain object access data 

2.5Request as domain object scope

2. Code demonstration of the API of the Request object

2.1Request object obtains client information

2.2Request obtains the information of the request header

3. The Request object receives form request parameters

3.1 Write a static page:

3.2 Receive request parameters

4. The Request object receives the Chinese garbled characters of the form request parameters

4.1Request object receives Chinese data:


1. What is Response

The software developed is a B/S structure software, which can access the server software through a browser. Enter an address from the browser to access the server (this process is called a request). After receiving the request, the server needs to process it, and after processing, it needs to display the processing result back to the browser (this process is called a response).

1. Draw a picture to illustrate the Response object

2. API of Response object

2.1Response about the response line method

 Set the status code of the response:

  • 200 correct
  • 302 redirect
  • 304 Lookup local cache
  • 404 The requested resource does not exist
  • 500 Internal Server Error

2.2 Response method on response header

The method at the beginning of set: for the case where a key corresponds to a value.

Example: For example, if there is a header content-Type: text/html, we use setHeader("content-Type","text/plain");

Finally get the result of the header: content-Type: text/plain

The method at the beginning of add: for the case where one key corresponds to multiple values. (it is appended on the original basis)

Example: For example, if there is a content-Type: text/html, we use addHeader("content-Type", "text/plain");

Finally get the result of the header: content-Type: text/html, text/plain

2.3 Response method on response body

2.4 Response other APIs 

Redirect method:

 Set the character set used by the browser when opening the page:

 Set the buffer character set for the response character stream:

The method for the server to write the cookie back to the browser:

3. Code demonstration of the API of the Response object

3.1 Set the status code

3.2 Complete Redirection 

Redirection: the combined effect of the 302 status code and the Location response header.

In actual development, you can use:

response.sendRedirect(“/web01/ResponseDemo2”); replace the two sentences of redirection

3.3 Timing refresh effect

4. Extension of page timing jump

4.1 Complete the page jump in HTML

Use JS to complete the countdown effect:

5. Chinese garbled processing of Response object response

5.1Response responds to the page in Chinese

5.1.1 Use byte stream to respond to Chinese

Will there be garbled characters when using the above code to output Chinese to the page?

  1. Not necessarily , in fact, the generation of this garbled code is related to the conversion of Chinese into a byte array and the opening method of the browser (the default character set used when opening)
  2. Solution: When converting Chinese into a byte array, the character set used when the browser is opened by default can be the same.

5.1.2 Use character stream to respond to Chinese:

Will using the above code to output Chinese to the page produce garbled characters?

  1. It must be garbled , the reason: the character stream has a buffer, the response obtains the character stream, and the default buffer encoding of the response design is ISO-8859-1 . This character set does not support Chinese.
  2. Solution: Set response to obtain the encoding of the character stream buffer to be consistent with the character set used when the browser is opened by default.

 There is a simplified way for characters to flow to the page to respond to Chinese:

2. An overview of the Request object and an introduction to the API

1. What is the Request object

The developed software is all B/S structure software. Some data is submitted from the browser to the server, and the content is encapsulated into a request object (Request object).

API of the Request object:

1.1 Get client information:

  1. Ways to get requests:

     2. Obtain the string of submission parameters after the request path:

      3. Get the URL and URI of the request path:

4. Obtain the IP address         of the client

2.2 The method of obtaining the request header 

  1. Obtain a request header corresponding to a key with a value:

      2. Obtain a request header corresponding to multiple values ​​for a key

2.3 Method of obtaining request parameters 

  1. Obtain the submitted parameters (a name corresponds to a value)

     2. Obtain the submitted parameters (one name corresponds to multiple values)

     3. Obtain the submitted parameters, and store the name and corresponding value of the submitted parameters into a Map collection

2.4Request as a method of domain object access data 

  1. Store data in the request field

      2. Get data from the request field

       3. Remove data from the request field

2.5Request as domain object scope

The Request object is actually the encapsulation of a request message sent from the client browser to the server. In essence, the validity period of the data saved in the Request is also within the scope of a request.

One request scope: Send a request from the client browser to the server, and the server responds to the browser for this request. When the server responds, the request object is destroyed, and the data stored in it is invalid.

2. Code demonstration of the API of the Request object

2.1Request object obtains client information

2.2Request obtains the information of the request header

3. The Request object receives form request parameters

3.1 Write a static page:

3.2 Receive request parameters

4. The Request object receives the Chinese garbled characters of the form request parameters

4.1Request object receives Chinese data:

POST way to receive Chinese:

GET way to receive Chinese:

Guess you like

Origin blog.csdn.net/wang_qiu_hao/article/details/126513561