Request data format
2.1.1 format introduction
The request data is divided into three parts: request line, request header and request body
The request line contains three parts: GET [request method] / [request URL path] HTTP/1.1[HTTP protocol and version]
There are seven request methods, the most commonly used are GET and POST
-
Request header: starting from the second line, in the form of key: value
The request header contains several attributes. Common HTTP request headers are:
Host: Indicates the host name of the request User-Agent: Browser version,for example Chrome The identity of the browser is similar Mozilla/5.0 ...Chrome/79,IE The identity of the browser is similar Mozilla/5.0 (Windows NT ...)like Gecko; Accept: Indicates the resource type that the browser can receive, such as text/*,image/*perhaps*/*Means all; Accept-Language: Indicates the language preferred by the browser, and the server can return web pages in different languages accordingly; Accept-Encoding: Indicates the type of compression that the browser can support, for example gzip, deflate Wait.
What is the use of these data?
For example: the server can obtain the relevant information of the client according to the content in the request header. With this information, the server can deal with different business requirements, such as:
- The results of parsing HTML and CSS tags in different browsers will be inconsistent, so the same code will have different effects in different browsers
- The server obtains the browser type of the client according to the data in the client request header, and can set different codes according to different browsers to achieve the same effect
- This is what we often call browser compatibility
- Request body: the last part of the POST request, which stores the request parameters
As shown in the figure above, the content of the red line box is the content of the request body. There is an empty line between the request body and the request header. At this time, the browser sends a POST request. Why can't you use GET? At this point, we need to review the differences between GET and POST requests:
- The GET request parameters are in the request line, there is no request body, and the POST request parameters are in the request body
- The size of GET request parameters is limited, but POST does not
2.1 Request inheritance system
Before learning this section, let's think about a problem. When introducing the Request and reply objects earlier, more careful students may have found:
- When our Servlet class implements the Servlet interface, the parameters in the service method are ServletRequest and ServletResponse
- When our Servlet class inherits the HttpServlet class, the parameters in the doGet and doPost methods become HttpServletRequest and httpservletreply
2.2 Request to obtain request data
HTTP request data is divided into three parts: request line, request header and request body. How to obtain the data of these three parts? First, let's learn how to obtain the data of request line?
2.2.1 get request line data
The request line contains three parts: request method, request resource path, HTTP protocol and version
For these three parts, the request object provides corresponding API methods to obtain, as follows:
- GET request method: GET
String getMethod()
- Get virtual directory (project access path): / request demo
String getContextPath()
- Get URL (uniform resource locator): http://localhost:8080/request-demo/req1
StringBuffer getRequestURL()
- Get URI (Uniform Resource Identifier): / request demo / req1
String getRequestURI()
- GET request parameters (GET method): username = Zhangsan & password = 123
String getQueryString()
2.2.2 get request header data
For the data of the request header, the format is key: value, as follows:
Therefore, the method to obtain the corresponding value according to the request header name is:
String getHeader(String name)
2.2.3 obtaining request body data
The browser does not have a request body when sending a GET request, so the request method needs to be changed to POST. The data format in the request body is as follows:
For the data in the Request body, the Request object provides the following two ways to obtain the data:
- Get byte input stream. If the front end sends byte data, such as file data, use this method
ServletInputStream getInputStream() This method can get bytes
- Get the character input stream. If the front end sends plain text data, use this method
BufferedReader getReader()
Use the above methods through code as follows:
/** * request Get request data */ @WebServlet("/req1") public class RequestDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // String getMethod(): GET request method: GET String method = req.getMethod(); System.out.println(method);//GET // String getContextPath(): get virtual directory (project access path): / request demo String contextPath = req.getContextPath(); System.out.println(contextPath); // StringBuffer getRequestURL(): get URL (uniform resource locator): http://localhost:8080/request-demo/req1 StringBuffer url = req.getRequestURL(); System.out.println(url.toString()); // String getRequestURI(): get URI (Uniform Resource Identifier): / request demo / req1 String uri = req.getRequestURI(); System.out.println(uri); // String getQueryString(): GET request parameters (GET method): username=zhangsan String queryString = req.getQueryString(); System.out.println(queryString); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
Start the server and access http://localhost:8080/request -demo/req1? Username = Zhangsan & passwrod = 123, the obtained results are as follows: