JSP servlet Basics Getting Started learning article processing cookies

Source: Internet
Author: User
Tags add array object constructor send cookies client

9.1 Cookie Overview

A cookie is a small, plain text message that the server sends to the browser, and the browser sends it to the server as it is when the user accesses the same Web server. By letting the server read the information it originally saved to the client, the site can provide a range of convenience for visitors, such as the identification of users in the online transaction process, the security requirements of the occasion to avoid users repeatedly enter the name and password, the homepage of the portal customization, targeted ads, and so on.

The purpose of cookies is to bring convenience to users and add value to the site. Although there are many misinformation, cookies do not pose a serious security threat. Cookies are never executed in any way, and therefore do not bring viruses or attack your system. In addition, because browsers generally allow only 300 cookies, each site holds up to 20 cookies, and each cookie has a size limit of 4 KB, so cookies won't fill your hard drive and will not be used as a denial of service attack.

Cookie API for 9.2 servlet

To send cookies to the client, the servlet first invokes the new cookie (Name,value) Create one or more cookies (section 2.1) with the appropriate name and value, set various properties (2.2) by cookie.setxxx, and add the cookie to the answer header (2.3) by means of a response.addcookie (cookie).

To read from the client cookie,servlet should call Request.getcookies (), and the GetCookies () method returns an array of Cookie objects. In most cases, you just need to iterate through each element of the array for a cookie with the specified name, and then call the GetValue method on the cookie to get the value associated with the specified name, which is discussed in section 2.4.

9.2.1 Create cookies

You can create cookies by calling the constructor of a cookie object. The constructor for the cookie object has two string parameters: the cookie name and the cookie value. Neither the name nor the value can contain whitespace characters and the following characters:

[ ] ( ) = , " / ? @ : ;



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.