First Look at a flowchart:
the process by which the server processes the request:
(1) Each time the server receives a request, it opens a new thread for the request.
(2) The server will be the client's request data package into the Request object, request is the carrier of data!
(3) The server also creates a response object that is connected to the client and can be used to send a response to the client.
As can be seen from the flowchart, the two most important parameters in Javaweb's request and response are requests and response, both of which are in the servlet's service () method.
1, Response concept:
Response is a parameter to the Servlet.service method, and the type is javax.servlet.http.HttpServletResponse. When each request is made by the client, 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 to the client.
The function of the response object is divided into the following four kinds:
(1) Set Response header information
(2) Send status code
(3) Set response body
(4) redirect
2, Response response body
Response is the response object, output response body (response body) to the client can use response response flow, Repsonse provides two response stream objects altogether:
(1) PrintWriter out = Response.getwriter (): Gets the character stream;
(2) Servletoutputstream out = Response.getoutputstream (): Get byte stream;
Of course, if the response body content is a character, use Response.getwriter () and use Response.getoutputstream () if the response content is byte, such as when downloading.
note that in one request, both streams cannot be used at the same time! In other words, either you use Repsonse.getwriter () or use Response.getoutputstream (), but you cannot use both streams at the same time. Otherwise, a IllegalStateException exception is thrown.
3, set the response header information
You can use the SetHeader () method of the response object to set the response header! The response headers set by using this method are eventually sent to the client browser!
(1) Response.setheader ("Content-type", "Text/html;charset=utf-8"): Sets the Content-type response header, which is the function of telling the browser that the response content is HTML type. Encoded as Utf-8. At the same time, it will set response character stream code as utf-8, namely response.setcharaceterencoding ("Utf-8");
(2) Response.setheader ("Refresh", "5; Url=http://www.baidu.com "): 5 seconds after the automatic jump to Baidu homepage.
4, set the status code and other methods
(1) Response.setcontenttype ("Text/html;charset=utf-8"): Equivalence and invocation of Response.setheader ("Content-type", "text/html; Charset=utf-8 ");
(2) response.setcharacterencoding ("Utf-8"): The character encoding for the character response stream is utf-8;
(3) Response.setstatus (200): Set the status code;
(4) Response.senderror (404, "The resource you are looking for does not exist"): When the error status code is sent, tomcat jumps to the fixed error page, but can display an error message.
5. REDIRECT (Key * * * * * *
5.1 What is redirection (two requests)
When you visit http://www.sun.com, you will notice that the URL in the browser's address bar becomes http://www.oracle.com/us/sun/index.htm, which is redirect. Redirection is when the server notifies the browser to access another address, and another request is issued.
5.2 How do I complete redirection?
A: The redirected status code is 302, we first use the Response object to send 302 status code to the browser, then set a location, that is, give an available URL, by the browser to access the new URL, to achieve redirection.
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 function of the above code is: When accessing the Aservlet, will notify the browser redirect to Baidu homepage. When the client browser resolves to a response code of 302, it knows that the server has redirected it, so it immediately gets the response header location and makes a second request.
There is also a quick way to redirect the use of the Response.sendredirect () method. For example, two sentences in the above example can be replaced with Response.sendredirect ("http://www.baidu.com").
request-encapsulates all request data for the client
1. Request overview
Request is a parameter of the Servlet.service () method, and the type is javax.servlet.http.HttpServletRequest. When each request is made by the client, the server creates a request object, encapsulates the requested data into the requests, and then passes to the service () method when the Servlet.service () method is invoked, indicating that the service () A Request object can be used to obtain the requested data from a method.
As shown in the figure:
The functions of the request can be grouped into the following categories:
(1) Encapsulates the request header data;
(2) Encapsulation of the request body data, if the GET request, then there is no body;
(3) Request is a domain object that can be used as a map to add access data;
(4) The request provides the function of requesting forwarding and request inclusion.
2. Request Domain method
Request is a domain Object! There are altogether four domain objects in Javaweb, where ServletContext is the domain object, which creates only one ServletContext object throughout the application. Request one of the requests that can share data in a single request.
A request creates a requests object, and if more than one servlet is experienced in a request, multiple servlet can use the request to share data. Now we don't know how to go through a few servlet in a single request.
The following is the domain method for the request:
(1)void setattribute (String name, object value): used to store an object, or to store a domain property, for example: Servletcontext.setattribute (" xxx "," xxx ", a domain attribute is saved in the request, the field property name is XXX, and the field property value is XXX. Note that if you call the method multiple times and use the same name, the last value is overwritten, which is the same as the map;
(2)Object getattribute (string name): used to get the data in the request, which needs to be stored before it is fetched, for example: String value = (string) Request.getattribute ("xxx");, get the domain attribute named xxx;
(3)void RemoveAttribute (String name): used to remove the domain attribute from request, if the domain attribute specified by the parameter name does not exist, then nothing is done with this method;
(4)Enumeration Getattributenames (): Gets the name of all domain properties;
3. Request Pass Parameter
The most common types of client-side delivery parameters can be found in two ways:
(1) The browser address bar input directly: must be GET request;
(2) Hyperlink: must be GET request;
(3) Form: either get or post, depending on the value of the method property of <form>;
difference between get request and POST request:
(1) Get request:
The request parameter is displayed in the browser's address bar, so it is unsafe;
The length of the request parameter length is within 1K;
The GET request has no request body and cannot set the parameter's encoding by Request.setcharacterencoding ();
(2) Post request:
Request parameters do not display the browser's address bar, relatively secure;
The request parameter length is not limited;
4. Request forwarding and request inclusion (* * * * * * * * *
Whether a request is forwarded or a request is included, it is represented by multiple servlet to work on a request. For example, SERVLET1 to process the request, and then Servlet1 to Servlet2 to continue processing the request.
Request forwarding and request contains
RequestDispatcher rd = Request.getrequestdispatcher ("/myservlet"); Use request to get the RequestDispatcher object, the parameter of the method is the servlet path of the servlet being forwarded or contained
Request Forwarding: Rd.forward (request,response);
Request contains: Rd.include (Request,response);
Sometimes a request requires multiple servlet collaborations to complete, so you need to jump to another servlet! in one servlet
> A request spans multiple servlet and requires the use of forwarding and inclusion.
> Request Forwarding: The next servlet completes the response body! The current servlet can set the response header! (Leaving the head without leaving the body) is the current servlet set the corresponding header is valid, the corresponding body is invalid.
> Request includes: Two servlet common incomplete response body! (all remain) are valid.
> Whether the request is forwarded or the request is included, it is within a request range! Using the same request and response!
Request forwarding contains a comparison to the request:
(1) If the request is forwarded to the Bservlet in the Aservlet, then the response body is not allowed in Aservlet, that is, the Response.getwriter () and Response.getoutputstream () can no longer be used. Output to the client, this work should be done by Bservlet, and if the request contains, then there is no limit;
(2) Request forwarding may not output the response body, but can also set the response header, such as: Response.setcontenttype ("Text/html;charset=utf-8");
(3) The request contains most of the application in the JSP page, complete multiple pages of the merger;
(4) Request forwarding is mostly applied in the servlet, the forwarding target is mostly JSP page;
As shown in the figure:
Comparison of request forwarding and redirection
(1) Request forwarding is a request, while redirection is two requests;
(2) The browser address bar will not change after the request is forwarded, and redirection will change, because redirection is two requests;
(3) The target of the request forwarding can only be the resources in this application, and the target of redirection may be other applications;
(4) Request forwarding to the Aservlet and Bservlet request method is the same, that is either get, or both are post, because the request forwarding is a request;
(5) The second request for redirection must be get;
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.