Request and Response principle, requestresponse
* Request and Response principles:
* The request object and response object are created by the server. You only need to use these two objects in the service method.
* Inheritance architecture:
ServletRequest <-- Inherit -- HttpServletRequest <-- Implement --- RequestFacade
* Response: encapsulate the Response Message and set the Response Message.
* Set the response line: HTTP/1.1 200 OK
* SetStatus (int code): sets the response status code.
* Demo redirection: 302
// Brief redirection
Response. sendRedirect ("http: // 192.168.1.100: 8080/zxc/one ");
* Implementation principle
// 1. Set the response status code to 302
Response. setStatus (302 );
// 2. Set the Response Header location
Response. setHeader ("location", "http: // 192.168.1.100: 8080/zxc/one ");
* Redirection features:
1. the address bar has changed.
2. Two requests and two responses
3. access external sites
* Set the response header:
* SetHeader (String name, String value ):
* SetDateHeader (String name, long date)
* SetIntHeader (String name, int value)
* Set the response body:
* GetOutputStream (): sends byte data.
* Verification Code:
* Prevent malicious Registration
* GetWriter (): Send character data
* When sending Chinese data, you must set the encoding method before obtaining the stream, and notify the client browser of the encoding method used by the server.
// Simplified encoding method
Response. setContentType ("text/html; charset = UTF-8 ");
* Request: encapsulate the Request message to obtain the Request message.
* Request message retrieval:
* GET request line: GET/day08/req1? Name = zs https/1.1
* GetMethod (): GET Request Method
* GetContextPath (): obtains the virtual directory.
* GetRequestURI (): Get URI (Uniform Resource Identifier)/day07/one
* GetRequestURL (): Get the URL (Uniform Resource Locator) http: // localhost: 8080/day07/one
* GetRemoteAddr (): obtains the IP address of the client.
* GetQueryString (): get Request Parameters
* GET request headers:
* String getHeader (String name)
* Long getDateHeader (String name)
* Int getIntHeader (String name)
* GET Request body: encapsulate post request parameter data
* BufferedReader getReader ():
* ServletInputStream getInputStream ():
* Common methods for obtaining request parameters:
* String getParameter (String name): obtains the value based on the key name.
* String [] getParameterValues (String name): An array that obtains values based on the key name. It is generally used as a check box.
* Map <String, String []> getParameterMap (): gets the map set of all parameters.
* Enumeration getParameterNames (): obtains the Enumeration of all key names.
* Chinese garbled characters in request parameters:
1. General purpose:
// 1. Decode iso
Byte [] bytes = username. getBytes ("iso-8859-1 ");
// 2. UTF-8 encoding
Username = new String (bytes, "UTF-8 ");
2. post-specific
// Set the input stream encoding to UTF-8
Request. setCharacterEncoding ("UTF-8 ");
* Domain object: indicates a request
* Request forwarding:
* Forwarding features:
1. the address bar does not change
2. One request and one response
3. external sites cannot be forwarded
* Redirection features:
1. the address bar has changed.
2. Two requests and two responses
3. access external sites