Http protocol and Chinese garbled

HTTP protocol

Features

  1. Based on TCP/IP
  2. It is an application layer protocol, which defines the format of the data sent when the server and the client communicate
  3. The default port is 80
  4. Is based on the request/response model: one request corresponds to one response
  5. Stateless: each request is independent of each other

historic version

  • 1.0: A new connection will be established every time i Ang Ying is requested
  • 1.1: Connections can be reused

Data format of the request message

  1. Request line

    Request method: There are 7 request methods in the HTTP protocol, 2 commonly used

    • GET:
      1. The request parameter is in the request line, after the url
      2. The requested URL length is limited
      3. Not very safe
    • POST:
      1. The request parameters are in the request body (it can also contain url parameters)
      2. There is no limit to the length of the requested url
      3. Relatively safe

    The method to get the request line contained in HttpServletRequest

    1. getMethod()
    2. (*) getContextPath(): Get virtual directory (directory of the project)
    3. getServletPath(): Get the Servlet path (Servlvet url-pattern)
    4. getQueryString(): Get request parameters in get mode
    5. (*) getRequestURI():/yogurt
    6. getRequestURL() : http://localhost:8080/yogurt
    7. getProtocol()
    8. getRemoteAddr(): Get the IP address of the client
  2. Request Header

    The client tells the server some information, such as

    User-Agent: Tell the server what browser I am using

    Referer: You can tell the server which page the current request comes from (if the request is directly accessed, Referer is null)

    Referer can be used to: 1. Anti-leech 2. Statistics

    How to get the request header

    1. getHeader(String name)
    2. getHeaderNames()
  3. Request blank line

    Blank line, used to split

  4. Request body

    Encapsulate the request parameters of the POST request message

    step

    1. Get the stream object (byte stream, character stream)
      • BufferedReader getReader(): Get character input stream
      • ServletInputStream getInputStream(): Get byte input stream
        • inputStream is applied in file upload
    2. Then read data from the stream object

Other functions of Request

  1. General way to get request parameters (either GET/POST, you can use the following methods)

    1. String getParameter(String name): Obtain the parameter value according to the parameter name. If the parameter corresponds to multiple values, only the first one will be obtained. (If it is a POST, calling getParameter will consume the Reader, if you call getReader subsequently to read, nothing will be read)
    2. String[] getParameterValues(String name) : Get an array of parameter values ​​(such as hobby=soccer&hobby=music, which can be used to get the value of the check box)
    3. Enumeration<String> getParameterNames() Get parameter name
    4. Map<String,String[]> getParameterMap() : Get the map collection of all parameters
  2. Request forwarding

    The way of resource jump inside the server, you can jump from one Servlet inside the server to another Servlet

    step

    1. Obtain the forwarding object through request:

      RequestDispatcher requestDispatcher = request.getRequestDispatcher("/yogurt");

    2. Forward through this forwarding object:requestDispatcher.forward(request,response);

    Features

    1. The url in the address bar of the browser will not change
    2. Can only be forwarded to the internal resources of the current server
    3. The same request is used
  3. data sharing

    When forwarding, data is shared between the two Servlets.

    Domain object: an object with a scope, within which data can be shared

    • Request domain: The scope is the scope of a request, and data can be shared when the request is forwarded. You can use request.setAttributemethods to set some data in A Servlet, and you can access the data just set in B Servlet after forwarding.

      request.setAttribute()

      request.getAttribute()

      request.removeAttribute()

  4. Get ServletContext

    request.getServletContext();

Response message data format

Response line

Http status code:

  • 1xx: The server received the client message, but did not complete the reception
  • 2xx: success
  • 3xx: Redirect
    • 302 (redirect)
    • 304 (prompt to let the browser fetch from the local cache, for example, a picture that has not changed, has been requested once before, and it has been cached locally by the browser, the next time it is requested, it will return 304, prompting to fetch from the local cache )
  • 4xx: Client error. The client path is wrong, or the request method is wrong
    • 404 (the request path has no corresponding resources)
    • 405 (the request method does not have a corresponding doXXX method)
  • 5xx: server error, need to modify server code
    • 500 (Internal server error)

Response header

  • Content-Type: The server tells the client that the data format and encoding format of the response body this time
  • Content-disposition: The server tells the client to open the response body data in what format
    • in-line: The default value, which means to open in the current page
    • attachment: means to open the response body as an attachment, which is used when the file is downloaded

Response body

Specific response data

Chinese garbled problem

The root cause of the garbled code is that the code table used by the codec is different.

Well, in web development. There is an encoding method on the browser side, the request/response encapsulated by tomcat has an encoding method, and the IDE we developed also has an encoding method.

On the browser side, you can use the Content-Type of the response header to tell the browser what encoding method to use to parse the response data.

On the Tomcat side, you can modify the encoding method used on the Connector tab by modifying the Tomcat server.xml configuration file. Or set the encoding method for request/response in the Servlet.

//设置response流的编码方式
response.setCharacterEncoding("utf-8");

//建议浏览器采用如下的编码方式对响应消息进行解码
response.setHeader("Content-Type","text/html;charset=utf-8");
//可以有简单的方式来设置Content-Type
response.setContentType("text/html;charset=utf-8");
//一定要在获取流之前对编码进行设置,一般放在最前面

response.getWriter.write("你好");

On the IDE side, you can set the JVM startup parameters and modify the IDEA file.encoding configuration file

-Dfile.encoding=UTF-8

Configure in the Connector tab under tomcat's server.xml

URIEncoding="UTF-8" useBodyEncodingForURI="true"

Before processing the request, setrequest.setCharacterEncoding("utf-8");

And add in the startup parameters of the JVM-Dfile.encoding=UTF-8

The display garbled output to the front-end page

Before exporting, setresponse.setHeader("Content-Type","text/html;charset=utf-8");

Guess you like

Origin blog.csdn.net/vcj1009784814/article/details/106083805