Let’s take a look at a flow chart:
The process of server processing requests:
(1) Every time the server receives a request, it will open a new thread for the request.
(2) The server will encapsulate the client's request data into the request object, and the request is the carrier of the request data!
(3) The server will also create a response object, which is connected to the client and can be used to send a response to the client.
As can be seen from the flow chart, in JavaWeb's request and response, the two most important parameters are request and response, which are in the service( ) method of the Servlet.
1. Response concept:
The response is a parameter of the Servlet.service method and is of type javax.servlet.http.HttpServletResponse. When the client makes each request, the server creates a response object and passes it to the Servlet.service() method. The response object is used to respond to the client, which means that using the response object in the service() method can complete the response work to the client.
The functions of the response object are divided into the following four types:
(1) Set response header information
(2) Send status code
(3) Set the response text
(4) Redirect
2. Response response text
The response is a response object. The response flow of the response body (response body) can be used to output the response body to the client. Repsonse provides a total of two response flow objects:
(1) PrintWriter out = response.getWriter(): Get character stream;
(2) ServletOutputStream out = response.getOutputStream(): Get the byte stream;
Of course, if the response body content is a character, then use response.getWriter(), and if the response content is bytes, for example, when downloading, then you can use response.getOutputStream().
Note that in one request, both streams cannot be used at the same time! That is, either you use reply.getWriter() or response.getOutputStream(), but you can't use both streams at the same time. Otherwise, an illegalStateException will be thrown.
3. Set response header information
You can use the setHeader() method of the response object to set the response header! The response header set using this method will eventually be sent to the client browser!
(1) response.setHeader("content-type", "text/html;charset=utf-8"): Set the content-type response header. The function of this header is to tell the browser that the response content is html type and encoded as utf-8. And at the same time, the character stream encoding of the response will be set to utf-8, that is, response.setCharaceterEncoding("utf-8");
(2) response.setHeader("Refresh","5; URL=http://www.baidu.com"): It will automatically jump to Baidu homepage after 5 seconds.
4. Set status code and other methods
(1) response.setContentType("text/html;charset=utf-8"): equivalent to calling response.setHeader("content-type", "text/html;charset=utf-8");
(2) response.setCharacterEncoding("utf-8"): Set the character encoding of the character response stream to utf-8;
(3) response.setStatus(200): Set the status code;
(4) response.sendError(404, "The resource you are looking for does not exist"): When sending an error status code, Tomcat will jump to a fixed error page, but the error message can be displayed.
5. Redirect (******key*******)
5.1 What is redirection (two requests)
When you visit http://www.sun.com, you will find that the URL in the browser address bar will become http://www.oracle.com/us/sun/index.htm, which is the redirection. Redirection is when the server notifies the browser to access another address, that is, to issue another request.
5.2 How to complete redirection?
Answer: The status code for redirection is 302. We first use the response object to send the status code of 302 to the browser, and then set a Location, that is, give an available URL, and the browser accesses the new URL to realize redirection.
For example:
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setStatus(302); response.setHeader("Location", "http://www.baidu.com"); } }The purpose of the above code is: after accessing the AServlet, the browser will be notified to redirect to the Baidu homepage. After the client browser parses the response code of 302, it knows that the server lets it redirect, so it will immediately obtain the response header location and then issue a second request.
There is also a quick redirect method, that is, use the response.sendRedirect() method. For example, the two sentences in the above example can be replaced by response.sendRedirect("http://www.baidu.com").
request-encapsulates all request data from the client
1. Request Overview
request is a parameter of the Servlet.service() method and is of type javax.servlet.http.HttpServletRequest. When the client makes each request, the server will create a request object and encapsulate the request data into the request, and then pass it to the service() method when the Servlet.service() method is called, which shows that the request data can be obtained through the request object in the service() method.
As shown in the figure:
The function of request can be divided into the following types:
(1) Encapsulate the request header data;
(2) Encapsulate the request text data. If it is a GET request, then there is no text;
(3) request is a domain object, which can be added and retrieved as a map;
(4) Request provides request forwarding and request inclusion functions.
2. Request domain method
request is a domain object! There are four domain objects in JavaWeb, among which ServletContext is a domain object, which only creates one ServletContext object in the entire application. Request one of the requests, which can share data in a request.
A request creates a request object. If multiple servlets have been experienced in a request, multiple servlets can use requests to share data. Now we don't know how to go through several Servlets in a request.
Here is the domain method of request:
(1) void setAttribute(String name, Object value): used to store an object, it can also be called storing a domain attribute, for example: servletContext.setAttribute("xxx", "XXX"), a domain attribute is saved in the request, the domain attribute name is xxx and the value of the domain attribute is XXX. Note that if the method is called multiple times and the same name is used, the last value will be overwritten, which is the same as the Map;
(2) Object getAttribute(String name): used to obtain the data in the request. It needs to be stored before obtaining it. For example: String value = (String)request.getAttribute("xxx"); to obtain the domain attribute named xxx;
(3) void removeAttribute(String name): used to remove the domain attribute in the request. If the domain attribute specified by the parameter name does not exist, then this method does nothing;
(4) Enumeration getAttributeNames(): Get the names of all domain attributes;
3. Pass parameters by request
There are two most common ways to pass parameters on the client:
(1) Direct input from the browser address bar: it must be a GET request;
(2) Hyperlink: It must be a GET request;
(3) Form: can be GET or POST, which depends on the method attribute value of <form>;
The difference between GET request and POST request:
(1) GET request:
The request parameters will be displayed in the browser's address bar, so it is not safe;
The length limit of the request parameter is within 1K;
The GET request has no request body, so the encoding of the parameter cannot be set through request.setCharacterEncoding();
(2) POST request:
The request parameters will not display the browser's address bar, which is relatively safe;
There is no limit on the length of the requested parameter;
4. Request forwarding and request inclusion (****** Key*******)
Whether it is a request forwarding or a request inclusion, it means that multiple servlets jointly handle a request. For example, Servlet1 handles the request, and then Servlet1 forwards it to Servlet2 to continue processing the request.
Request forwarding and request including
RequestDispatcher rd = request.getRequestDispatcher("/MyServlet"); Use request to get the RequestDispatcher object. The parameter of the method is the Servlet path of the forwarded or contained Servlet.
Request forwarding: rd.forward(request,response);
The request includes: rd.include(request,response);
Sometimes a request requires multiple servlets to collaborate, so you need to jump in one servlet to another!
> A request spans multiple servlets and requires forwarding and inclusion.
> Request forwarding: The response body is completed by the next Servlet! The current Servlet can set the response header! (Leave the head but not the body) That is, the corresponding head set by the current Servlet is valid, and the corresponding body is invalid.
> The request contains: The response body is not completed by two servlets! (Stay all) All are valid.
> Whether it is a request forwarding or a request containing, it is within the scope of a request! Use the same request and response!
Request forwarding and request include comparison:
(1) If the request is forwarded to the BServlet in the AServlet, then the response body is not allowed to be output in the AServlet, that is, the response.getWriter() and response.getOutputStream() can no longer be output to the client. This work should be done by the BServlet; if it is used to include the request, there is no such restriction;
(2) Although the request forwarding cannot output the response body, the response header can still be set, for example: response.setContentType("text/html;charset=utf-8");
(3) Most of the requests are applied in JSP pages to complete the merge of multiple pages;
(4) Request forwarding is mostly applied in servlets, and the forwarding target is mostly JSP pages;
As shown in the figure:
Comparison of request forwarding and redirection
(1) Request forwarding is one request, while redirection is two requests;
(2) After the request forwarding, the browser address bar will not change, but the redirect will change, because the redirect is two requests;
(3) The target of requesting forwarding can only be the resources in this application, and the target of redirection can be other applications;
(4) The request forwarding method for AServlet and BServlet is the same, that is, both are GETs or both are POSTs, because the request forwarding is a request;
(5) The second request for redirect must be GET;
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.