HttpServletResponse servletresponse Return Response Setting response header setting response body weight-oriented common methods how to redirect response encoding response garbled

Source: Internet
Author: User
Tags error status code response code

HttpServletResponse and Servletresponse are all interfaces . the specific type object is passed by the servlet container. the functions of the Servletresponse object are divided into the following four types:? Set the response header information;? Send status code;? Set the response body;? redirect; Setting the response header information HttpServletResponse (not in Servletresponse)

void setHeader(String var1, String var2);

The response headers that are set using this method are eventually sent to the client browser Example:

Response. SetHeader ("Content-type", "text/html;charset=utf-8");

sets the Content-type response header, which functions as:
    1. Tells the browser that the response content is of type HTML, encoded as UTF-8.
    2. and also set response character stream encoding to Utf-8, that is response.setcharaceterencoding ("Utf-8");

Auto Jump

response.setHeader("Refresh","5; URL=http://www.baidu.com");

automatically jump to Baidu after 5 seconds. Send status code and type

response.setContentType("text/html;charset=utf-8");

//equals to call Response.setheader ("Content-type", "Text/html;charset=utf-8");

response.setCharacterEncoding(“utf-8”);//设置字符响应流的字符编码为utf-8;

response.setStatus(200);//设置状态码;

Response. Senderror (404, "The resource you are looking for does not exist"); //When sending an error status code, tomcat jumps to the fixed error page, but can display an error message.

For example:Response.senderror (404, "The resource you are looking for does not exist ha"); Setting the response body
Servletresponse is a response object that outputs the response body (response body) to the client and can use the Servletresponse response streamRepsonse provides a total of two response stream objects:? PrintWriter out = response. getwriter (): Gets the character stream;? Servletoutputstream out = response. Getoutputstream (): Gets the byte stream;two methods are HttpServletResponse inherited by Servletresponse. Note:
    1. Of course, if the response body content is a character, then use Response.getwriter ()
    2. If the response content is byte, then you can use Response.getoutputstream () such as when downloading
in one request, you cannot use both streams at the same time! That is, either you use Repsonse.getwriter (), or you use Response.getoutputstream (), but you cannot use both streams at the same time. Otherwise, the illegalstateexception exception will be thrown. character encoding
when using Response.getwriter (), be aware that the default character encoding is iso-8859-1,If you want to set character streams for characters encoded as Utf-8can use response.setcharaceterencoding ("Utf-8") to set. This ensures that the characters you output to the client are encoded using UTF-8! But the client browser does not know what the response data is encoded! if you want to notify the client to use UTF-8 to interpret the response data, use the response.setcontenttype ("Text/html;charset=utf-8") method is betterbecause this method will not only call response.setcharaceterencoding ("Utf-8"), it will also set the Content-type response headerThe client browser uses the Content-type header to interpret the response data.

Buffer

Response.getwriter () is a printwriter type, so it has a buffer, and the default size of the buffer is 8KB.

That is, before the response data is output 8KB , the data is stored in the buffer and is not immediately sent to the client.

When the Servlet executes, the server flushes the stream so that the data in the buffer is sent to the client.

If you want the response data to be sent to the client immediately:

writes data greater than 8KB to the stream;

Call the Response.flushbuffer () method to flush the buffer manually;

redirectwhen you visit a URL, you will find that the URL of the browser address bar becomes B, which is the redirectThe so-called redirect is the server repositioning your direction and telling you to go somewhere elseThe request was made again, with a total of two requests . The first step is to set the response code to 302.
a response code of 200 indicates a successful response, and a response code of 302 indicates a redirect, and you need to tell the browser that it needs to
redirect
The second step sets the URL of the redirect
Because redirection is the second request that notifies the browser, the browser needs to know the URL of the second request so the second step in completing the redirect is to set the location header, specifying the URL address of the second request.
response response .setheader ( " Location " "https:/ /www.cnblogs.com/noteless/"
you'll find the address bar immediately jumps a simplified way to redirect:
Response . Sendredirect ("https://www.cnblogs.com/noteless/");
relative paths can also be used on the same server
Response . Sendredirect ("/servlet/servletb");
Effect Ditto REDIRECT Note points:? Redirection is a two-time request;? Redirected URLs can be other applications, not limited to the current application;? The redirected response header is 302 and must have a location response header;? REDIRECT will not use Response.getwriter () or response.getoutputstream () output data, otherwise there may be an exception;

HttpServletResponse servletresponse Return Response Setting response header setting response body weight-oriented common methods how to redirect response encoding response garbled

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.