HttpServletResponse Object (i)

Source: Internet
Author: User

The Web server receives an HTTP request from the client, creates a request object for each request, and a response object representing the response, respectively.

Since request and response objects represent requests and responses, I get the data submitted by the client, find the request object, and output the data to the client, only to find the response object.

A: HttpServletResponse Object introduction

  

The HttpServletResponse object represents the response of the server. This object encapsulates the method of sending data to the client, sending a response header, and sending a response status code.

1.1: Responsible for sending data to the client (browser) related methods

  

1.2: Related methods for sending response headers

1.3: Related method of sending response status code

  

1.4: Constant for response status code

HttpServletResponse defines a number of constants for the status code, and the specific view API

Common: 404

200:

500:

II: Common Applications of HttpServletResponse objects

2.1 Output Chinese data using OutputStream flow to client

Example: public class ResponseDemo01 extends httpservlet{
@Override
protected void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String data= "China";
OutputStream Op=response.getoutputstream ();
Response.setheader ("Content-type", "TEXT/HTML;CHARSE=GBK");
Byte[] Databytearr=data.getbytes ("GBK");
Op.write (Databytearr);
}
}

2.2: Output Chinese data using PrintWriter flow to client

public class ResponseDemo01 extends httpservlet{
@Override
protected void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String data= "China";
Response.setheader ("Content-type", "TEXT/HTML;CHARSET=GBK");
Response.setcharacterencoding ("GBK");
PrintWriter Out=response.getwriter ();
Out.print (data);
}
}

Using PrintWriter does not convert a string into a byte array that step.

2.3: File Download

Realization of the idea::

1. Get the absolute path to the file you want to download

2. Get the file name to download

3. Set the Content-disposition response header to control the browser to download the form of open file

4. Get the file input stream to download

5. Create a data buffer

6. Get the OutputStream stream from the response object

7. Writing the FileInputStream stream to buffer buffers

8. Using OutputStream to output buffer data to the client browser

Code:

public class ResponseDemo02 extends httpservlet{
@Override
protected void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
1: Get the absolute path to the downloaded file
String Realpath=this.getservletcontext (). Getrealpath ("/download/2.jpg");
2 Get the downloaded file name
String filename=realpath.substring (realpath.lastindexof ("\ \") +1);
3 Setting the Content-disposition response header to control the browser to download the form of open file

The Chinese file name should be encoded using the Urlencoder.encode method, or the file name will appear garbled
Response.setheader ("Content-disposition", "attachment;filename=" +urlencoder.encode (filename, "UTF-8"));

        response.setheader ("Content-disposition", "attachment;filename=" + FileName);
        //4: Get the file input stream to download
        inputstream in= New FileInputStream (Realpath);
        int len=0;
        //5: Create a data buffer
    byte[] buffer=new byte[1024];
        //6 Get outputstream stream
    outputstream out= via Response object Response.getoutputstream ();
        //7 writes the FileInputStream stream to the buffer buffer
    while (len= In.read (buffer)) >0) {
        //8 use OutputStream to output buffer data to the client browser
         out.write (Buffer,0,len);
    }
        in.close ();
    }
}

The third step is to open the file in the form of the following download, and then Inputsteam writes the content to the path, and then outputstream the output to the client display.

File Download Note: It is recommended to use the OutputStream stream when writing the file download function, avoiding the use of the PrintWriter stream, because the OutputStream stream is a byte stream, can handle any type of data, and the PrintWriter stream is a character stream , only character data can be processed, and data loss can result if byte data is processed in a character stream.

Response Implementation Details:

The Getoutputstream and Getwriter methods are used to obtain the output binary data , output text data servletouputstream, PrintWriter objects respectively.
The two methods of Getoutputstream and Getwriter are mutually exclusive , and any one of them is called, and no other method can be called.
The data written to the Servletoutputstream or PrintWriter object by the servlet program is fetched by the servlet engine from the response, and the servlet engine treats the data as the body of the response message. It is then combined with the response status line and each response header to output to the client.
After the Serlvet service method finishes, the Servlet engine checks whether the output stream object returned by the Getwriter or Getoutputstream method has already called the Close method, and if not, The servlet engine calls the Close method to close the output stream object.

  

HttpServletResponse Object (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.