Javaweb-httpservletresponse (i)

Source: Internet
Author: User

I. Using OutputStream to flow to the client browser output "China"

1  PackageGacl.response.study;2 3 Importjava.io.IOException;4 ImportJava.io.OutputStream;5 Importjavax.servlet.ServletException;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten  Public classResponseDemo01extendsHttpServlet { One  A      Public voiddoget (httpservletrequest request, httpservletresponse response) -             throwsservletexception, IOException { -Outputchinesebyoutputstream (response);//output Chinese using outputstream stream the     } -      -     /** - * Output Chinese using outputstream stream +      */ -      Public voidOutputchinesebyoutputstream (httpservletresponse response)throwsioexception{ +         /**use OutputStream to output Chinese note questions: A * On the server side, the data is the Code table output, then control the client browser to open the corresponding code table, at * For example: Outputstream.write ("China". GetBytes ("UTF-8"));//use OutputStream to flow to the client browser output Chinese, output in UTF-8 encoding - * At this time to control the client browser to UTF-8 encoding open, otherwise display will appear when the Chinese garbled, then on the server side how to control the client browser to display 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");//Display data with UTF-8 encoding by setting the response header control browser -          */ -String data = "China"; inOutputStream outputstream = Response.getoutputstream ();//get OutputStream output stream -Response.setheader ("Content-type", "text/html;charset=utf-8");//by setting the response header control browser to display data in UTF-8 encoding, if not add this sentence, then the browser will display is garbled to         /** + * Data.getbytes () is a process of converting a character into a byte array, which is sure to check the Code table, - * If it is a Chinese operating system environment, the default is to look for the GB2312 code table, the * The process of converting a character to a byte array is to convert the Chinese characters to the corresponding numbers on the GB2312 's Code table . * * For example: "Medium" on the GB2312 's Code table corresponding to the number is 98 $ * The corresponding number of "Guo" in GB2312 's code table isPanax Notoginseng          */ -         /** the * GetBytes () method if no parameters, then according to the operating system's language environment to choose the Conversion Code table, if it is a Chinese operating system, then use the GB2312 code table +          */ A         byte[] Databytearr = Data.getbytes ("UTF-8");//Converts a character to a byte array, specifying conversion with UTF-8 encoding theOutputstream.write (Databytearr);//using OutputStream to flow to the client output byte array +     } -  $      Public voidDoPost (httpservletrequest request, httpservletresponse response) $             throwsservletexception, IOException { - doget (request, response); -     } the  -}

Two. Use PrintWriter to flow to the client browser to output Chinese data

1  PackageGacl.response.study;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten  One /** A * Output Chinese data using PrintWriter flow to client browser - * or the output of the "China" of the two Zi -  */ the  Public classResponseDemo02extendsHttpServlet { -  -     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -         //output Chinese using printwriter stream + Outputchinesebyprintwriter (response); -          +     } A     /* at * Output Chinese using PrintWriter stream -      */ -     Private voidOutputchinesebyprintwriter (httpservletresponse response)throwsIOException { -String data = "China"; -         //set to output characters in "Utf-8" encoding to the client browser -Response.setcharacterencoding ("UTF-8"); in         //get PrintWriter output stream -PrintWriter out =Response.getwriter (); toResponse.setheader ("Content-type", "Text/html;charset=utf-8"); +  -         //use PrintWriter to flow to client output characters the out.write (data); *     } $ Panax Notoginseng     protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { - doget (request, response); the     } +  A}

Three. Use OutputStream or printwriter to output a number to the client browser

1  PackageGacl.response.study;2 3 Importjava.io.IOException;4 ImportJava.io.OutputStream;5 ImportJava.io.PrintWriter;6 7 Importjavax.servlet.ServletException;8 ImportJavax.servlet.ServletOutputStream;9 ImportJavax.servlet.http.HttpServlet;Ten Importjavax.servlet.http.HttpServletRequest; One ImportJavax.servlet.http.HttpServletResponse; A  - /** - * Output numbers to the client browser using OutputStream or PrintWriter the  */ -  Public classResponseDemo03extendsHttpServlet { -  -     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { +         //using OutputStream Output 1 to the client browser -         //Outputonebyoutputstream (response); +         //using the PrintWriter stream to output the number 1 A Outputonebyprintwriter (response); at     } -     //using the PrintWriter stream to output the number 1 -      Public voidOutputonebyprintwriter (httpservletresponse response)throwsioexception{ -Response.setheader ("Content-type", "Text/html;charset=utf-8"); -Response.setcharacterencoding ("UTF-8"); -PrintWriter out = Response.getwriter ();//get PrintWriter output stream inOut.write ("Output number 1 with PrintWriter stream":); -Out.write (1+ ""); to     } +     //using OutputStream Output 1 to the client browser -     Private voidOutputonebyoutputstream (httpservletresponse response)throwsIOException { theResponse.setheader ("Content-type", "Text/html;charset=utf-8"); *OutputStream OutputStream =Response.getoutputstream (); $Outputstream.write ("Output number 1 with OutputStream stream:". GetBytes ("UTF-8")));Panax Notoginseng         //outputstream.write (1); -Outputstream.write (1+ ""). GetBytes ()); the     } +  A     protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { the doget (request, response); +     } -  $}

Javaweb-httpservletresponse (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.