Today’s content
HTTP protocol: Response message 2. Response object 3. ServletContext objectCopy the code
The HTTP protocol:
1. Request message: data sent from the client to the server * Data format: 1. Request line 2. Request header 3. Request blank line 4. Request body 2. Response message: data sent from server to client * Data format: 1. Response line 1. Composition: Protocol/version Response status code Status Code Description 2. Response status code: the server tells the client browser the status of the request and response. Classification: 1. 1XX: The server receives the message from the client but fails to complete it. After a period of time, the server sends the 1XX multi-status code. Representative: 200 3. 3xx: redirection. Delegates: 302(redirection), 304(access cache) 4.4xx: Client error. * indicates: * 404 (request path does not have corresponding resource) * 405: Request mode does not have corresponding doXxx method 5. 5xx: Server side error. Representative: 500(internal server exception) 2. Response header: 1. Format: Header Name: Value 2. Content-disposition: The server tells the client in what format to open the body data * value of the response. Content-disposition: The server tells the client in what format to open the body data * value of the response. * in-line: default value, open * attachment in the current page; Filename = XXX: Opens the response body as an attachment. 4. Response body: Data transferred * Response string format HTTP/1.1 200 OK Content-Type: text/ HTML; charset=UTF-8 Content-Length: 101 Date: Wed, 06 Jun 2018 07:08:42 GMT <html> <head> <title>$Title$</title> </head> <body> hello , response </body> </html>Copy the code
The Response object
* Function: Set response message 1. set response line 1. format: HTTP/1.1 200 OK 2. SetStatus (int SC) 2. Set response header: setHeader(String name, String Value) 3. Output stream: PrintWriter getWriter() * Output stream: ServletOutputStream getOutputStream() 2. Use output stream, output data to client browser * Example: 1. Complete redirection * Redirection: resource jump way * code implementation: //1. Set the status code to 302 Response.setStatus (302); // set response.setHeader("location","/day15/responseDemo2"); Response. sendRedirect("/day15/responseDemo2"); Redirect 1. Address bar changes 2. Redirection can access resources of other sites (servers). 3. Redirection is two requests. Cannot use request objects to share data. The forwarding address bar path remains the same. 2. Forwarding can access only resources on the current server. A forward is a request that can be shared using a request object. Path classification 1. Relative path: a relative path cannot be used to determine unique resources * such as./index.html * Does not start with /. Start path * Rule: Find the relative location relationship between the current resource and the target resource *./ : Current directory *.. / : back one level 2. An absolute path: * such as determined by the absolute path can only resources: http://localhost/day15/responseDemo2 / day15 responseDemo2 * path starting with / * rules: Who is the path defined for? Determine from where the request will be sent * to the client browser for use: a virtual directory (the project's access path) is required * It is recommended that the virtual directory be dynamically obtained: request.getContextPath() * <a>, <form> redirect... * For server use: no need to add virtual directory * forwarding path 2. Server output character data to browser * Step: 1. 1. PrintWriter pw = response.getwriter (); The default encoding of the obtained stream is ISO-8859-1. SetContentType ("text/ HTML; ") response.setContentType("text/ HTML; charset=utf-8"); 3. The server outputs byte data to the browser * Steps: 1. Get byte output stream 2. Output data 4. Verification code 1. Essence: Picture 2. Purpose: Prevent malicious form registrationCopy the code
ServletContext object:
1. Concept: Represents the entire Web application, can communicate with the application container (server) 2. Obtain: 1. Through the request object for the request. GetServletContext (); 2. Get this.getServletContext() from HttpServlet; MIME type: * MIME type: a file data type defined during Internet communication * Format: large type/small type Text/HTML image/ JPEG * String getMimeType(String file) 2. Domain objects: Share data 1. setAttribute(String name,Object value) 2. getAttribute(String name) 3. removeAttribute(String name) * ServletContext object scope: all users all requested data 3. Method: String getRealPath(String path) String b = context.getrealPath ("/b.txt"); System.out.println(b); String c = context.getRealPath("/WEB-INF/c.txt"); // Access system.out.println (c); // Access system.out.println (c); String a = context.getRealPath("/WEB-INF/classes/a.txt"); // access system.out.println (a); // access system.out.println (a);Copy the code
Case study:
* File download requirements: 1. Display hyperlink on the page 2. Click the hyperlink and the download prompt box will pop up 3. 1. If the resource pointed to by the hyperlink can be parsed by the browser, it will be displayed in the browser. If it cannot be parsed, a download prompt box will pop up. * Content-disposition :attachment; * Content-disposition :attachment; * Content-disposition :attachment; Filename = XXX * Steps: 1. Define the page, edit the hyperlink href attribute, point to the Servlet, and pass the resource name filename 2. Define Servlet 1. Get file name 2. Load file into memory using byte input stream 3. Specifies the response header for response: Content-disposition :attachment; Filename = XXX 4. Write the data to the response output stream. Obtaining the browser version used by the client 2. Set the encoding mode of filename to different versionsCopy the code