Web Learning Note-httpservletresponse One

Source: Internet
Author: User

HttpServletResponse and Httpservletresquest Brief introduction

The Web server receives an HTTP request from the client, creating a Request object for each request, and a response object representing the response, respectively.
The request and response objects represent requests and responses, so we need to get the data submitted by the client, just find the request object. To output data to a client, you just need to find the response object.

HttpServletResponse

Post reference Address

Java EE API 1.7

Fields

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

    • Send data to the client (this is the method in the Servletresponse class)

    • How to send a response header to a client (browser)

    • How to send a response status code to a client (browser)

Httpservletrespose Output Chinese

The difference between a byte stream and a character stream

A byte is a 8-bit binary. The characters in Java are Unicode code, accounting for 2 bytes, that is, 16-bit stream end is a byte stream, reader and writer End is a stream of characters, the character stream is mainly read the text file content, you can read a character of one character, you can read a line of text file content. and the byte stream reading unit is byte.byte as the computer storage most basic unit, may use the byte stream to read many other formats the file, the slice video and so on;

    • Output Chinese using character stream
protected void Doget (javax. servlet. HTTP. HttpServletRequestRequest, Javax. servlet. HTTP. HttpServletResponseResponse) throws Javax. servlet. Servletexception, IOException {//sets the character to"UTF-8"Encode output to client browser response. setcharacterencoding("Utf-8");Control the browser by setting the response header to utf-8Code display data, if not add this sentence, then the browser will display is garbled response. SetHeader("Content-type","Text/html;charset = Utf-8");String string ="Sima Dust";Response. Getwriter(). Write(string);System. out. println(string);}

In the browser grab the package as follows:

Output Chinese using a byte stream

The first method: protected void Doget (javax. servlet. HTTP. HttpServletRequestRequest, Javax. servlet. HTTP. HttpServletResponseResponse) throws Javax. servlet. Servletexception, IOException {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 garbledString string ="Sima Dust";System. out. println(string);Response. Getoutputstream(). Write(String. GetBytes("Utf-8"));}
The second method: protected void Doget (javax. servlet. HTTP. HttpServletRequestRequest, Javax. servlet. HTTP. HttpServletResponseResponse) throws Javax. servlet. Servletexception, IOException {String string ="Sima Dust";System. out. println(string);Response. setContentType("Text/html;charset=utf-8"); This code does two things: 1 sets the code table that the server uses to encode data 2. To notify the browser to decode the code tableResponse. Getoutputstream(). Write(String. GetBytes("Utf-8"));}

Download the Chinese file name

 Public void Doget(HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//Get the path to the fileString path = Getservletcontext (). Getrealpath ("/web-inf/classes/beautiful. jpg") ;//Start from Project path by default        //Create an input streamInputStream is=NewFileInputStream (path);byte[] bs =New byte[1024x768] ;intb =0;//Create an output stream objectServletoutputstream OS = Response.getoutputstream ();//Encode in Chinese        //Get the Chinese filenameString name = path.substring (Path.lastindexof ("\\") +1, Path.length ()); System. out. println (name);//Encode the Chinese file nameName = Urlencoder.encode (name,"Utf-8") ; System. out. println ("after encoding:"+ name);//Notify browser to open file in the way downloadedResponse.setheader ("Content-disposition","Attachment;filename="+ name); while((b = is. Read (BS))! =-1) {Os.write (BS,0, b); }

Encountered an error in this process:

Workaround:
Fill in the Init method with the Super.init () system By default add this sentence, or delete the Init method directly

Then we can download it in the browser and grab the package as follows:

Attention:

Character stream and byte stream cannot be mixed
The output of the esponse is not output directly to the page, but is returned to the server and returned to the browser by the server
The response generates a character stream and a byte stream without shutting down, and the servlet engine will help shut down.

Web Learning Note-httpservletresponse 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.