Javaweb Learning Summary 25 (usage of response object One)

Source: Internet
Author: User

One: the concept of Reponse objects

When the client sends an HTTP request, the server side creates the request object and the response object for each of the Requests.

The response object consists of three parts: response header, response status code, and response body

Two: Response Object Case Analysis

1: output Chinese data to client

1  packagecom.hlcui.servlet;2 3 Importjava.io.IOException;4 Importjava.io.OutputStream;5 Importjava.io.PrintWriter;6 7 Importjavax.servlet.ServletException;8 Importjavax.servlet.http.HttpServlet;9 Importjavax.servlet.http.HttpServletRequest;Ten Importjavax.servlet.http.HttpServletResponse; one  a  public classServletDemo6extendsHttpServlet { -  -      public voiddoget (httpservletrequest request, httpservletresponse response) the             throwsservletexception, IOException { -          -String data = "study hard!"; -         //gets the output stream that writes the data to the client +OutputStream out =Response.getoutputstream (); - out.write (data.getbytes ()); +     } a  at      public voiddoPost (httpservletrequest request, httpservletresponse response) -             throwsservletexception, IOException { - doget (request,response); -     } -  -}

Browser printing:

The results are correct!

It is important to note that because I do not specify which character set to use when I encode the data on the server side, the default is to use gb2312.

The Browser-side parsing is also gb2312, so the correct display, if I specify the encoding character is UTF-8, the result is as Follows:

To encode data using UTF-8:

1 out.write (data.getbytes ("UTF-8"));

The printed result is garbled:

Because I specify the Server-side encoding is UTF-8, and the browser is still using the default decoding method to decode, resulting in garbled characters.

How to Resolve:

You can set the Browser's view (using UTF-8 to parse the data sent by the server), but it is not the fundamental approach, the best way is to

The server side tells the browser how to decode it. Modify the Following:

1 response.setheader ("content-type", "text/html;charset=utf-8");

The result is correct!

You can use meta in HTML tags to simulate server response response Headers:

1  public voiddoget (httpservletrequest request, httpservletresponse response)2             throwsservletexception, IOException {3String data = "study hard!";4         //gets the output stream that writes the data to the client5OutputStream out =Response.getoutputstream ();6Out.write ("<meta http-equiv= ' content-type ' content= ' text/html;charset=utf-8 ' >". GetBytes ());7Out.write (data.getbytes ("UTF-8"));8}

The result is still right!

Here we understand the mechanism by which the server uses character set encoding and browser decoding:

1: server-side encoding of data, encoding using character set (gb2312 or UTF-8), no default is gb2312

2: The browser parses the response data, the default gb2312, it will take the coded data guide character set to fetch the query

If we want to output 99 in the browser, then the server Terminal 99 is encoded, on the browser side, will hold 99 of the code to

The content of the query encoding in the character set.

1 out.write ("getBytes") ("UTF-8");

Encodes the 99 string in UTF-8.

PrintWriter Objects and OutputStream objects

If you want a browser to write binary files such as pictures, videos, and audio files, you must use byte stream Outputstream.

If you write a string to the browser, you can use the PrintWriter object, and the following uses PrintWriter to write Chinese to the Browser:

1  public void doget (httpservletrequest request, httpservletresponse response) 2             throws servletexception, IOException {3         String data = "study hard!" ; 4         // gets the output stream that writes the data to the client 5         PrintWriter out = response.getwriter (); 6         out.write (data); 7     }

Output result:

garbled, because response to the data encoding when the use of the iso8859-1, and the browser to resolve the use of gb2312, so the query, the report???

Workaround:

1  public voiddoget (httpservletrequest request, httpservletresponse response)2             throwsservletexception, IOException {3         //setting response encoding of data4Response.setcharacterencoding ("UTF-8");5         //tell the browser how to decode6Response.setheader ("content-type", "text/html;charset=utf-8");7String data = "study hard!";8         //gets the output stream that writes the data to the client9PrintWriter out =Response.getwriter ();Ten Out.write (data); one}

Output correct result:

In fact, you can also use Response.setcontenttype ("text/html;charset=utf-8");

This is the servlet encapsulated api, equivalent to the following two sentences of the content

Set response to encode the data response.setcharacterencoding ("UTF-8");//tell The browser how to decode Response.setheader ("content-type", " Text/html;charset=utf-8 ");

Javaweb Learning Summary 25 (usage of response object One)

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.