Article Directory
HTTP protocol
Features
- Based on TCP/IP
- It is an application layer protocol, which defines the format of the data sent when the server and the client communicate
- The default port is 80
- Is based on the request/response model: one request corresponds to one response
- 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
-
Request line
Request method: There are 7 request methods in the HTTP protocol, 2 commonly used
- GET:
- The request parameter is in the request line, after the url
- The requested URL length is limited
- Not very safe
- POST:
- The request parameters are in the request body (it can also contain url parameters)
- There is no limit to the length of the requested url
- Relatively safe
The method to get the request line contained in HttpServletRequest
- getMethod()
- (*) getContextPath(): Get virtual directory (directory of the project)
- getServletPath(): Get the Servlet path (Servlvet url-pattern)
- getQueryString(): Get request parameters in get mode
- (*) getRequestURI():
/yogurt
- getRequestURL() :
http://localhost:8080/yogurt
- getProtocol()
- getRemoteAddr(): Get the IP address of the client
- GET:
-
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
- getHeader(String name)
- getHeaderNames()
-
Request blank line
Blank line, used to split
-
Request body
Encapsulate the request parameters of the POST request message
step
- 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
- Then read data from the stream object
- Get the stream object (byte stream, character stream)
Other functions of Request
-
General way to get request parameters (either GET/POST, you can use the following methods)
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)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)Enumeration<String> getParameterNames()
Get parameter nameMap<String,String[]> getParameterMap()
: Get the map collection of all parameters
-
Request forwarding
The way of resource jump inside the server, you can jump from one Servlet inside the server to another Servlet
step
-
Obtain the forwarding object through request:
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/yogurt");
-
Forward through this forwarding object:
requestDispatcher.forward(request,response);
Features
- The url in the address bar of the browser will not change
- Can only be forwarded to the internal resources of the current server
- The same request is used
-
-
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.setAttribute
methods 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()
-
-
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");