Java topic notes ~ Request request

1. Request message format

The request message that the client sends an HTTP request to the server includes the following format:

It consists of four parts: request line, request header, blank line and request data. The following figure shows the general format of the request message.

GET request:

POST request:

 

 

Data format description:
    
1. Request line

// request method request url request protocol/version
// example: GET /login.html HTTP/1.1

​ Request method:

There are 7 request methods in the HTTP protocol, and 2 are commonly used

​        * GET:

​ 1. The request parameters are in the request line, after the url.

2. The requested url length is limited

3. Less secure

​        * POST:

1. The request parameters are in the request body

2. There is no limit to the length of the requested url

3. Relatively safe

2. Request header: The client browser tells the server some information

​ Request header name: Request header value

​ Common request headers:

1. User-Agent: The browser tells the server that I visit the browser version information you use

The header information can be obtained on the server side to solve browser compatibility issues
2. Referer: http://localhost/login.html

Tell the server, where did I (the current request) come from?

effect:

1. Anti-leech

2. Statistical work

3. Request a blank line

​ Empty lines are used to separate the request header and request body of the POST request.

4. Request body (text):

​ Encapsulates the request parameters of the POST request message

1. The method of obtaining the request line data

GET /day14/demo1?name=zhangsan HTTP/1.1

method:

  1. Get request method: GET

* String getMethod()

2. Get the virtual directory: /day14

* String getContextPath()

3. Obtain the Servlet path: /demo1

* String getServletPath()

4. Obtain the get method request parameter: name=zhangsan

* String getQueryString()

5. Get the request URI: /day14/demo1

* String getRequestURI(): /day14/demo1

* StringBuffer getRequestURL() :http://localhost/day14/demo1

* URL: Uniform Resource Locator: http://localhost/day14/demo1 People's Republic of China

* URI: Uniform Resource Identifier: /day14/demo1 Republic

6. Obtain protocol and version: HTTP/1.1

* String getProtocol()

7. Obtain the IP address of the client:

* String getRemoteAddr()

2. Get request header data

* method:

* String getHeader(String name): Get the value of the request header through the name of the request header

* Enumeration<String> getHeaderNames(): Get all request header names

 

3. Get the request body data:

* Request body: Only the POST request method has a request body, and the request parameters of the POST request are encapsulated in the request body

* Steps:

1. Get the stream object

* BufferedReader getReader(): Get character input stream, only character data can be manipulated

* ServletInputStream getInputStream(): Get byte input stream, which can operate all types of data

* Explained after file upload knowledge points

2. Then get the data from the stream object

4. Other common methods

1. General way to obtain request parameters: Regardless of get or post request methods, the following methods can be used to obtain request parameters

1. String getParameter(String name): Obtain the parameter value according to the parameter name username=zs&password=123

2. String[] getParameterValues(String name): Obtain the array of parameter values ​​according to the parameter name hobby=xx&hobby=game

3. Enumeration<String> getParameterNames(): Get the parameter names of all requests

4. Map<String,String[]> getParameterMap(): Get the map collection of all parameters

* Chinese garbled problem:

* Get method: tomcat 8 has solved the problem of garbled characters in the get method

* post method: garbled characters

* Solution: Before getting the parameters, set the encoding of the request request.setCharacterEncoding("utf-8");

2. Request forwarding: a resource jump method inside the server

1. Steps:

1. Obtain the request forwarder object through the request object: RequestDispatcher getRequestDispatcher(String path)

2. Use the RequestDispatcher object to forward: forward(ServletRequest request, ServletResponse response)

2. Features:

1. The path in the browser address bar does not change

2. It can only be forwarded to the internal resources of the current server.

3. Forwarding is a request

3. Sharing data:

* Domain object: a scoped object that can share data within the scope

* request field: represents the scope of a request, generally used to share data among multiple resources for request forwarding

* method:

1. void setAttribute(String name,Object obj): store data

2. Object getAttitude(String name): get value by key

3. void removeAttribute(String name): remove the key-value pair by key

4. Get the ServletContext:

  • ServletContext getServletContext()

 

Guess you like

Origin blog.csdn.net/qq_53142796/article/details/132213682