JSP built-in object -- Response

Source: Internet
Author: User

Response indicates the server's response to the client. Most of the time, the program does not need to use response to respond to client requests, because there is a simpler response object ------ out, which represents the page output stream, and it is easier to directly use out to generate a response.

However, out is an example of jspwriter, jspwriter is a subclass of writer, and writer is a writable stream, so non-character content cannot be output. If you need to dynamically generate a bitmap in the JSP page, the PDF document, using out as the response object will not be able to complete, at this time you must use response as the response output.

In addition, you can use response to redirect requests and add cookies to clients.

1. The response generates a non-character response.

To generate a non-character response, use response to respond to client requests. Next, generate an image on the client. Response is an instance of the httpservletresponse interface, which provides a getoutputstream () method that returns a response to the output stream.

<% @ Page contenttype = "image/JPEG" Language = "Java" Import = "Java. util. *, Java. AWT. image. *, javax. imageIO. *, Java. AWT. * "pageencoding =" UTF-8 "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

Note: You can also use the IMG label on other pages to display the image page using this temporary image generation method, you can easily implement the graphic verification code function on the web page. In addition, you can use response to generate non-character responses, and directly use PDF files and Excel files as expressions.

2. Redirection

Redirection is another use of response. Unlike forward, redirection will lose all the attributes of the Request Parameters and request range, because redirection will generate a second request, this is not in the same request range as the previous request. Therefore, all attributes of the request parameter and request range are lost.

Httpservletresponse provides a sendredirect (string path) method, which is used to redirect to the path resource and send a request to the path resource again.

Response. sendredirect ("XXX. jsp ")

3. added the cookie function.

Cookies are usually used for websites to record certain customer information, such as the customer's username and customer preferences. The difference between a cookie and a session is that the session will expire with the closure of the browser, but the cookie will be stored on the client machine until the cookie expires.

Adding a cookie is also completed using the response built-in object. The response object provides the following methods:

Steps for adding cookies

(1) create a cookie instance. The cookie constructor is Cookie (string name, string value)

(2) set the life cycle of the cookie, that is, how long the cookie is valid

(3) Write cookie --- response. addcookie (cookie) to the client );

Access the client cookie using the request object. The request object provides the getcookies () method. This method returns an array of all cookies on the client machine and traverses each element of the array, find the cookie you want to access.

Cookie [] cookies = request. getcookies ();

For (cookie C: cookies ){

C. getname ()

C. getvalue ()

}

Note: 1. the life cycle of the cookie object must be set. Otherwise, the cookie will automatically disappear as the browser closes.

2. by default, the cookie value cannot contain Chinese characters. If you need a cookie with the value of Chinese content, you can use java.net. urlencoder first encodes the Chinese string and sets the encoded result to the cookie value. First read and then use java.net. urlencoder for decoding. Decodes the cookie. getvalue () after reading the output.

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.