JS Java Operation Cookie__js

Source: Internet
Author: User

Document.cookie= "info=" +name+ "," +PASSW + ";"; Store cookies

var info = document.cookie.split (";"); Read cookies


A cookie in ①js is the end of a ";" that represents the end of a data in a cookie.

② in a cookie file, the cookie data exists as a key-value pair, which means that only one of the same names is allowed.


JS Read cookies:

Get the value of a cookie according to the name
function Getcookieval (cookiename)
{
var search = cookiename + "="
var returnvalue = "";
if (Document.cookie.length > 0)
{
offset = document.cookie.indexOf (search);
if (offset!=-1)
{
Offset + = Search.length
End = Document.cookie.indexOf (";", offset);
if (end = = 1)
end = Document.cookie.length;
Returnvalue=decodeuricomponent (document.cookie.substring (offset, end));
}
}
returnvalue = Returnvalue.replace (/\ "/g,"); Replace all double quotes
Return returnvalue;
}


Java Delete cookies:

/**
* Delete Cookies
* @param cookiename
* @param response
* @param request
*/
private void Deletecookie (httpservletresponse response, HttpServletRequest request) {
cookie[] cookies = request.getcookies ();
if (cookies!= null && cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
Cookie Tmpcookie = new Cookie (Cookies[i].getname (), Cookies[i].getvalue ());
Tmpcookie.setmaxage (0);
Tmpcookie.setpath ("/");
Response.addcookie (Tmpcookie);
}
}
}



Java adds a value to a cookie:

Cookie cookie = new Cookie (name of cookie, value of cookie);

Cookie.setpath ("/");
Cookie.setmaxage (99999999);
Response.addcookie (cookie);



Reference: http://mojianpo.iteye.com/blog/1137122

If you want to see JS write cookies, search in Baidu, anyway, Java Cookie,js can read, JS write, Java can also read. More and more like using cookies.

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.