Servlet's response (a)

http protocol: He is based request and response mode

Response composition

  1. State line

  2. Response header

  3. Blank line

  4. Response Body

State line

Status line -> http + version status code / status + State Description

Status Code: Tell the customer's request status, success or failure

100 ~ 199: the request has been issued, but did not receive the response 200 ~ 299: the request identification and successful response 300 ~ 399: denotes that the resource has been removed elsewhere need to request 400 to 499: that caused by the client issues 500 to 599: that is the server-side problem

Status Description: The description of the status code

Common status codes

Common status codes status description
200 Indicates that the request is successful, a successful response
302 A resource has been removed, you need to go somewhere else request
304 Represents access to resources is obtained from the cache
400 Parameters sent by the client in question
404 Can not find resource / url typically clerical error
405 Request incorrectly
500 Server-side abnormal
502 Server downtime
Configuration status code file

Role: to prevent exposure code information

<! - written in the web.xml file -> 
<! - Configuration Error page -> 
    < error-Page > 
        <! - status code -> 
        < error-code > 500 </ error-code > 
        <-! navigate to the error page enacted -> 
        < LOCATION > /html/fuwei.html </ LOCATION > 
    </ error-page >
Setting the status code
// Set status code (to know about) 
    resp.setStatus (208 ); 
    resp.setStatus (HttpServletResponse.SC_NOT_FOUND); 
// custom description (know it) 
    resp.sendError (404, "url abnormality check url the address is correct. ");

 

Setting response header type (such as sending a TXT, MP4 ...), the back volume size class, the version of the server, the response time, whether the content needs to be cached.

Overall, the message is to tell the client server sent back.

Common response headers
  1. Content-Disposition: form tells the client has downloaded the file to open

Applications - Download file

protected  void doPost (REQ the HttpServletRequest, HttpServletResponse RESP) throws ServletException, IOException {
     // first step: get the name of the downloaded file 
        String fileName = req.getParameter ( "fileName" );
     // Step Two: Get the file extension 
        String = fileName.substring EXT (fileName.lastIndexOf ( "." ));
     // third step: get a uuid (to download the file a new name) 
        String newfilename = UUID.randomUUID () toString ();.
     // step four: set the response header Content-Disposition: form tells the client to open the downloaded file 
        resp.setHeader ( "Content-Disposition", "Attachment; filename =" + newfilename + EXT);
     // step five: get download file physical path
        DownPath = String the this .getServletContext () getRealPath ( "DownloadImage /" +. FileName);
     // Step Six: Download the complete
         // create a file 
        File File = new new File (downPath);
         // input stream 
        InputStream in = null ;
         // obtain an output stream of 
        the ServletOutputStream OUT = resp.getOutputStream ();
         the try { 
            in = new new the FileInputStream (File);
             // buffer stream 
            byte [] = buf new new  byte [1024 ];
             //读取字节的长度
            int length=0;
            //读取文件
            while( (length= in.read(buf,0,buf.length))!=-1 ){

                out.write(buf,0,buf.length);
            }
            out.flush();

        }catch(Exception e){
            e.printStackTrace();
        }finally{

            if(in!=null){
                in.close();
            }
            if(out!=null){
                out.close();
            }
        }
    }

 

 

Guess you like

Origin www.cnblogs.com/-Archenemy-/p/12657468.html