Java HTML cookie

Source: Internet
Author: User

Set cookie in Java:

Public static void setcookie (string cookiename, string value,

   Int expirehours ){
  Httpservletresponse response = servletactioncontext. getresponse ();
  Cookie = new cookie (cookiename, value );
  Cookie. setpath ("/"); // you need to set this
  Cookie. setdomain (".bessystem.com"); // you can use this setting to share two websites.
  If (expirehours! = 0 ){
   Expirehours = expirehours * 60*60; // if this parameter is not set, cookies are not written into the hard disk, but in the memory. They are only useful on the current page. If the unit is seconds, refresh the page, the cookie cannot be found,
  }
  Cookie. setmaxage (expirehours );
  Response. addcookie (cookie );

 }

Get cookie in HTML:


function getCookie(c_name){if(document.cookie.length>0){   c_start=document.cookie.indexOf(c_name + "=");   if(c_start!=-1){      c_start=c_start + c_name.length+1 ;     c_end=document.cookie.indexOf(";",c_start);     if(c_end==-1) {        c_end=document.cookie.length;     }     return unescape(document.cookie.substring(c_start,c_end));   }}return "";}


Get the cookie name defined in the background in JS and get the value through name

<SCRIPT>

VaR username = getcookie ("username ");

Alert (username );

</SCRIPT>


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.