HttpServletResponse Object Introduction

Source: Internet
Author: User

First, 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. Look at the HttpServletResponse API to see these related methods.

1.1, responsible for sending data to the client (browser) related methods

  

1.2, responsible for sending the response header to the client (browser) related methods

  

  

1.3, responsible for sending the response status code to the client (browser) related methods

  

1.4, the constant response status code

HttpServletResponse defines a number of constants for the status code (see the Servlet's API), which can be used when a response status code needs to be sent to the client, avoiding direct write numbers, and constants that correspond to common status codes:

Constants corresponding to status code 404

  

Constants corresponding to status code 200

  

Constants corresponding to status code 500

  

Ii. Common Application of HttpServletResponse objects 2.1, use OutputStream flow to the client browser output Chinese data

Use OutputStream stream to output Chinese note the problem:

On the server side, the data is the Code table output, then control the client browser with the corresponding code table open, such as: Outputstream.write ("China". GetBytes ("UTF-8")); Use OutputStream flow to the client browser output Chinese, to UTF-8 encoding for output, at this time to control the client browser to UTF-8 encoding open, otherwise display will appear in Chinese garbled, So how does the server side control the client browser to display the data in UTF-8 encoding? You can control the behavior of the browser by setting the response header, for example: Response.setheader ("Content-type", "Text/html;charset=utf-8"), and by setting the response header to control the browser to display the data in UTF-8 encoding.

Example: Using the OutputStream to flow to the client browser output "China" two characters

 1 package gacl.response.study; 2 3 Import java.io.IOException; 4 Import Java.io.OutputStream; 5 Import javax.servlet.ServletException; 6 Import Javax.servlet.http.HttpServlet; 7 Import Javax.servlet.http.HttpServletRequest; 8 Import Javax.servlet.http.HttpServletResponse; 9 public class ResponseDemo01 extends HttpServlet {One to ten private static final long Serialversionuid = 4312868947607 181532l;13 public void doget (HttpServletRequest request, httpservletresponse response) throws Servlet      Exception, IOException {outputchinesebyoutputstream (response);//using OutputStream stream output Chinese 17}18 19/**20     * Use OutputStream Stream output Chinese * @param request22 * @param response23 * @throws IOException 24 */25 public void Outputchinesebyoutputstream (HttpServletResponse response) throws ioexception{26/** use OutputStream output Chinese note The question: 27 * On the server side, the data is the Code table output, then control the client browser with the corresponding Code table open, 28 * For example: Outputstream.write ("China". GetBytes ("UTF-8"))///Use OutputStream flow to the client browser to output Chinese, output in UTF-8 encoding 29 * At this time to control the client browser to UTF-8 encoding open, otherwise display will appear when the Chinese garbled, then the server side how to control the client browser to utf- 8 of the code shows the data? 30 * You can control the behavior of the browser by setting the response header, for example: * Response.setheader ("Content-type", "text/html;charset=utf-8");//By setting the response header control The browser displays the data in UTF-8 encoding */33 String data = "China"; outputstream outputstream = Response.getoutputstrea m ();//get OutputStream output stream Response.setheader ("Content-type", "text/html;charset=utf-8");//          By setting the response header to control the browser to display data in UTF-8 encoding, if you do not add this sentence, then the browser will display is garbled/**37 * data.getbytes () is a character into a byte array of the process, the process will certainly go to check the Code table, 38 * If it is a Chinese operating system environment, the default is to find the GB2312 code table, 39 * The process of converting characters to byte arrays is to convert the Chinese character to the corresponding number 40 on the GB2312 code table * For example: "Medium" in GB2312 The corresponding number on the code table is 9841 * "Country" the corresponding number on the GB2312 's Code table is 9942 */43/**44 * getBytes () method if without parameters, it will be based on Operating system language environment to choose the Conversion Code table, if it is a Chinese operating system, then use GB2312 Code table */46 byte[] Databytearr = data.getbytes ("UTF-8");//convert character to byte array , specifying the conversion of OutputStream with UTF-8 encoding.Write (Databytearr);//use OutputStream to flow to the client output byte array}49 public void DoPost (HttpServletRequest request, HttpServlet  Response Response) Wuyi throws Servletexception, IOException {doget (request, Response); 53}54 55}

The results of the operation are as follows:

  

After the client browser receives the data, it parses the data according to the character encoding set on the response header, as follows:

  

2.2, using PrintWriter flow to the client browser output Chinese data

Use PrintWriter stream to output Chinese note the problem:

Before getting the PrintWriter output stream, first use "Response.setcharacterencoding (CharSet)" To set the character to what encoding to output to the browser, such as: response.setcharacterencoding ("UTF-8"), set the character to "UTF-8" encoding output to the client browser, and then use Response.getwriter (); get PrintWriter output stream, These two steps cannot be reversed, as follows:

1 response.setcharacterencoding ("UTF-8");//Set the character to "UTF-8" encoding output to client browser 2/**3 * PrintWriter out = Response.getwriter (); This code must be placed in response.setcharacterencoding ("UTF-8"), followed by 4 * Otherwise response.setcharacterencoding ("UTF-8") This line of code will not be set, Browser display is still garbled 5 */6 printwriter out = Response.getwriter ();//get PrintWriter output stream

Then use Response.setheader ("Content-type", "text/html;charset= character encoding"), set the response header, control the browser to display with the specified character encoding encoding, for example:

1//By setting the response header control browser to UTF-8 encoding display data, if not add this sentence, then the browser will display is garbled 2 response.setheader ("Content-type", "Text/html;charset=utf-8") ;

In addition to using Response.setheader ("Content-type", "text/html;charset= character encoding"), and setting the response header to control the browser's display in the specified character encoding encoding, You can also simulate the action of the response head in the following ways

1/**2 * Learn a trick: Use the HTML language inside the <meta> tag to control browser behavior, simulation by setting the response header control browser behavior 3  *response.getwriter (). Write ("<meta http-equiv= ' Content-type ' content= ' text/html;charset=utf-8 '/> '); 4 * equivalent to Response.setheader ("Content-type", "text /html;charset=utf-8 "); 5 */6 Response.getwriter (). Write (" <meta http-equiv= ' content-type ' content= ' text/html; Charset=utf-8 '/> ");

Example: Using the PrintWriter to flow to the client browser output "China" two characters

 1 package gacl.response.study; 2 3 Import java.io.IOException; 4 Import Java.io.OutputStream; 5 Import Java.io.PrintWriter; 6 Import javax.servlet.ServletException; 7 Import Javax.servlet.http.HttpServlet; 8 Import Javax.servlet.http.HttpServletRequest; 9 Import javax.servlet.http.httpservletresponse;10 One public class ResponseDemo01 extends HttpServlet {all private S Tatic final Long serialversionuid = 4312868947607181532l;14 public void doget (HttpServletRequest request, HTTPSERVL Etresponse response) throws Servletexception, IOException {outputchinesebyprintwriter (response);      /use PrintWriter stream output Chinese 18}19 20/**21 * Output with PrintWriter stream Chinese * @param request23 * @param response24 * @throws ioexception */26 public void Outputchinesebyprintwriter (HttpServletResponse response) throws Ioex ception{27 String data = "China"; 28 29//By setting the response header control browser to display data in UTF-8 encoding, if you do not add this sentence, then the browser will display is garbled//r Esponse.sethEader ("Content-type", "Text/html;charset=utf-8"); Response.setcharacterencoding ("UTF-8");//Set the character to "UTF -8 "encoded output to client browser/**34 * PrintWriter out = Response.getwriter (); This code must be placed in response.setcharacterencoding (" UT F-8 "); after 35 * Otherwise response.setcharacterencoding (" UTF-8 ") This line of code will be invalid, the browser display time is garbled */37 PrintWriter out = Response.getwriter ();//get PrintWriter output Stream 38/**39 * Learn a trick: Use the HTML language <meta> tags to control browser behavior, simulation by setting the response header control Browser Behavior Out.write * ("<meta http-equiv= ' content-type ' content= ' text/html;charset=utf-8 '/> '); 41 * etc Same as Response.setheader ("Content-type", "Text/html;charset=utf-8"); */43 out.write ("<meta http-equiv=" C      Ontent-type ' content= ' text/html;charset=utf-8 '/> '); out.write (data);//use PrintWriter flow to client output characters 45}46 47 public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, I oexception {DOget (request, response); 50}51} 

When it is necessary to output character data to the browser, it is convenient to use printwriter, eliminating the step of converting characters to byte arrays.

2.3. Use OutputStream or printwriter to output numbers to the client browser

For example, there is the following code:

 1 package gacl.response.study; 2 3 Import java.io.IOException; 4 Import Java.io.OutputStream; 5 Import Java.io.PrintWriter; 6 7 Import Javax.servlet.ServletException; 8 Import Javax.servlet.http.HttpServlet; 9 Import javax.servlet.http.httpservletrequest;10 Import javax.servlet.http.httpservletresponse;11 public class     ResponseDemo01 extends HttpServlet {$ private static final Long Serialversionuid = 4312868947607181532l;15 16 public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcep       tion {outputonebyoutputstream (response);//Use OutputStream output 1 to client browser 20 21}22 23/**24     * Use OutputStream Stream output Digital * @param request26 * @param response27 * @throws IOException 28 */29 public void Outputonebyoutputstream (HttpServletResponse response) throws ioexception{30 Response.setheader ("cont Ent-type "," Text/html;charset=utf-8 "); OutputStream ouTputstream = Response.getoutputstream (); Outputstream.write ("Output number 1 with OutputStream stream:". GetBytes ("UTF-8"); 33outputstream.write (1);34}35 36}

Running the above code shows the following results:

  

The results of the operation and we imagined that the number 1 did not lose, the following we modify the above Outputonebyoutputstream method code, the modified code is as follows:

1     /** 2      * Output digital using OutputStream Stream 1 3      * @param request 4      * @param response 5      * @throws IOException  6
   */7 public     void Outputonebyoutputstream (HttpServletResponse response) throws ioexception{8         Response.setheader ("Content-type", "text/html;charset=utf-8"); 9         OutputStream outputstream = Response.getoutputstream (),         outputstream.write ("Use OutputStream stream output number 1:".) GetBytes ("UTF-8"));         //outputstream.write (1);         Outputstream.write ((1+ ""). GetBytes ()); -     }

  1+ "" This step is to add the number 1 and an empty string, so that after processing, the number 1 becomes the string 1, and then convert the string 1 into a byte array using OutputStream for output, this time see the following results:

  

This time we can see the output of 1, this shows a problem: in the development process, if you want the server output what browser can see what, then the server side will be in the form of a string output .

If you are using the PrintWriter stream to output a number, convert the number to a string before outputting it, as follows:

1     /** 2      * Output digital using PrintWriter Stream 1 3      * @param request 4      * @param response 5      * @throws IOException  6
   */7 public     void Outputonebyprintwriter (HttpServletResponse response) throws ioexception{8         Response.setheader ("Content-type", "text/html;charset=utf-8"); 9         response.setcharacterencoding ("UTF-8");         printwriter out = Response.getwriter ();//get PrintWriter output stream 11         out.write ("output number 1 with PrintWriter stream");         out.write (1+ "");     

HttpServletResponse Object Introduction

Related Article

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.