Javaweb-response Object

Source: Internet
Author: User

Javaweb-response Object
First, Response
1.Resonse inheritance structure:
Servletresponse–httpservletresponse

2.Response Representative Response
The status code, response header, and entity content in the response message can be manipulated by it.

3. Using response to output data to the client

response.getOutputStream().write("中文".getBytes())

Output data, which is a byte stream, what bytes are output,
The browser, by default, opens the data sent by the server using the platform bytecode,
If the server side uses non-platform code to output character byte data, you need to explicitly specify the browser encoding when the code table used to prevent garbled problems.

response.addHeader("Content-type","text/html;charset=gb2312")response.getWriter().write(“中文”);

The output data, which is a character stream, response this character to the browser after transcoding the operation,
This process uses the ISO8859-1 code table by default, and there is no Chinese in iso8859-1.
So in the process of transcoding to replace the Chinese, resulting in garbled problems. You can specify the target code table used by response during transcoding to prevent garbled characters.

response.setCharcterEncoding("gb2312");

In fact, response also provides

setContentType("text/html;charset=gb2312")

This method sets the Content-type response header, notifies the browser to open the Code table, and sets the transcoding code table for response, which resolves garbled lines.

4. Using response to set content-disposition header for file download

设置响应头content-disposition为“attachment;filename=xxx.xxx”

Use the stream to read the file in, and then use response to get the response stream output
If the file name is in, must be URL encoding, encoding the code table used must be utf-8

5.refresh Head Control timing Refresh
Sets the response header refresh to a numeric value, specifying how many seconds after the current page is refreshed
Set the response header refresh to 3;url=/day05/index.jsp, specify how many seconds to refresh to which page
Can be used to implement the registration "successful registration, 3 seconds after the jump to the home" function
In HTML you can use tags to simulate the function of the response header.

6. Use response to set expires, Cache-control, pragma to realize whether the browser cache resources, these three headers can be implemented, but due to historical reasons, different browser implementations, so generally with these three headers use

6.1 Control browser do not cache (Captcha picture not cached) set expires to 0 or-1 set Cache-control to No-cache, pragma to No-cache

6.2 Control Browser Cache resources. Resources are cached even if the browser is not explicitly specified, and the cache does not have a deadline. The cache is used when the address is re-entered in the Address bar, but the resource is re-accessed when the browser is refreshed or re-opened.
If you explicitly specify the cache time, the browser cache is, there will be a deadline, until the expiration date, when the address bar re-enter the address or re-open browser access will be used in the cache, and when the refresh will regain resources.

7.Response Implementing request redirection
7.1 Ancient Methods:

response.setStatus(302);response.addHeader("Location","URL");

7.2 Shortcuts:

response.sendRedirect("URL");

9.Response Note content:
9.1 For a single request, Response's Getoutputstream method and Getwriter method are mutually exclusive and can only be called one, paying special attention to forward and not violating this rule.

9.2 When using response output data, not directly to the browser, but write to the response buffer, wait until the entire service method is returned, the server takes out the information in the response to form an HTTP response message returned to the browser.

After the 9.3service method returns, the server will check itself response get OutputStream or writer is closed, if not closed, the server automatically help you shut down, generally do not shut down the two streams themselves.

Copyright NOTICE: Welcome to Exchange, QQ872785786

Javaweb-response Object

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.