Java cookie actions and detailed parameter descriptions

Source: Internet
Author: User

Syntax: Cookie variable name. Setmaxage (active cycle);

The time of the active cycle is in seconds, the larger the time setting, the longer the valid time for the cookie object, and if the valid period is set to 0, the cookie object is immediately invalidated after it is stored in the browser, and if the valid period is set to any negative number, the browser closes This cookie object is immediately invalidated.

All properties of the cookie class are manipulated as shown in the table.


Look at an example

Import java.io.unsupportedencodingexception;

Import Javax.servlet.http.cookie;
Import Javax.servlet.http.https tutorial ervletrequest;
Import Javax.servlet.http.httpservletresponse;

/**
* @author Administrator
*
* To change the template for this generated type comment go
* Window>preferences>java>code Generation>code and comments
*/
public class Cookies {

public int maxage; Set the validity period for this cookie in seconds
public string path; Cookie Path
Cookie[] Cookie_get = {};

Public cookies () {
MaxAge =-1;
Path = "/";
}

/**
* Put cookies to the client
*
* @param response
* @param name
* @param value
*/
public void Putcookie (httpservletresponse response, string name,
String value) {
try {
Cookie cookie = new Cookie (name, encode (value));
Cookie.setmaxage (MaxAge);
Cookie.setpath (path);
Response.addcookie (cookie);
catch (Exception e) {
E.printstacktrace ();
}
}

/**
* Get cookies from client
*
* @param request
* @param name
* @return
*/
public string GetCookie (HttpServletRequest request, string name) {
if (Cookie_get = null | | cookie_get.length = 0) {
Cookie_get = Request.getcookies ();
}
String Returnstr;
RETURNSTR = null;
try {
for (int i = 0; Cookie_get!= null && i < cookie_get.length; i++) {
Cookie_get[i].setpath (path);
if (Cookie_get[i].getname (). Equals (name)) {
Cookie_get[i].setmaxage (-1);
Returnstr = Cookie_get[i].getvalue (). ToString ();
Break
}
}
Return decode (RETURNSTR);
catch (Exception e) {
Return decode (RETURNSTR);
}
}

   /**
    * Erase cookies
    *
    * @param Response
    *            HttpServletResponse
    * @param name
    *             string
    */
    public void Removecookie (httpservletresponse response, string name) {
        Putcookie (response, name, NULL);
   }

   /**
    * URL decoding for a given character
    *
    * @para M value
    *            string
    * @return String
    */
    private static string decode (string Value) {
        string result = ';
        if (!isempty (value)) {
             try {
                 result = Java.net.urldecoder.decode (value, "GBK");
           } catch (Unsupportedencodingexception ex) {

           }
       }
        return result;
   }

   /**
    * URL-coded for a given character
    *
    * @para M value
    *            string
    * @return String
    */
    private static string encode (string Value) {
        string result = ';
        if (!isempty (value)) {
             try {
                 result = Java.net.urlencoder.encode (value, "GBK");
           } catch (Unsupportedencodingexception ex) {

}
}
return result;
}

/**
* To determine if NULL, return true for null
*
* @param value
* String
* @return Boolean
*/
private static Boolean IsEmpty (string value) {
if (value = null | | Value.trim (). Equals (""))
return true;
Else
return false;
}

/**
* Check two data is NULL, if NULL returns True
*
* @param value1
* @param value2
* @return
*/
public Boolean IsEmpty (string value1, String value2) {
if (null = = Value1 | | | null = value2 | | "". Equals (value1)
|| "". Equals (value2))
return true;
Else
return false;
}
}

All properties of the cookie class are manipulated as shown in the table.

Method Significance
Cookies (String, String) Generate a well-known and value cookie
Clone () Returns a copy of the current object
Getcomment () Returns a comment describing the cookie, not NULL
GetDomain () Returns the name of the cookie
Getmaxage () Returns the maximum lifetime of the cookie
GetName () Returns the name of the cookie
GetPath () Returns all URL prefixes that use the cookie
Getsecure () Returns the security flag for this cookie
GetValue () Returns the value of this cookie
GetVersion () Returns the version of this cookie
Setcomment (String) Set a comment that describes the cookie
SetDomain (String) Set the domain name for this cookie
Setmaxage (int) Set the maximum lifetime for this cookie
SetPath (String) Setting the cookie can only be raised from a request that uses the URL prefix
SetSecure (Boolean) Set the security flag for this cookie
SetValue (String) Set the value of this cookie
setversion (int) Set the version number of the protocol used by the cookie

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.