Tool Class _java Action cookie

Source: Internet
Author: User

Import java.io.UnsupportedEncodingException;
Import Java.net.URLDecoder;
Import Java.net.URLEncoder;
Import Javax.servlet.http.Cookie;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Cookieutil {
private static String Default_path
="/";
private static int default_age =365*24*3600;
/**
* @Function: Add a cookie to set the time
* @Author: ZZP
* @param name
* @param value
* @param response
* @param age
* @throws unsupportedencodingexception Void
* @Date: 2014-2-19
* @Modifications:
* @Modifier Name; Date; The Reason for modifying
*
*/
public static void Addcookie (String name,string value,
HttpServletResponse Response,int age) throws unsupportedencodingexception{
Cookie cookie =
New Cookie (Name,urlencoder.encode (Value, "utf-8"));
Cookie.setmaxage (age);
Cookie.setpath (Default_path);
Response.addcookie (cookie);
}
/**
* @Function: Add a cookie to the time of default
* @Author: ZZP
* @param name
* @param value
* @param response
* @throws unsupportedencodingexception Void
* @Date: 2014-2-19
* @Modifications:
* @Modifier Name; Date; The Reason for modifying
*
*/
public static void Addcookie (String name,string value,httpservletresponse
Response) throws unsupportedencodingexception{
Addcookie (Name,value,response,default_age);
}
/**
* @Function: Show All Cookies
* @Author: ZZP
* @param name
* @param request
* @return
* @throws unsupportedencodingexception String
* @Date: 2014-2-19
* @Modifications:
* @Modifier Name; Date; The Reason for modifying
*
*/
public static string Findcookie (string name,httpservletrequest request)
Throws unsupportedencodingexception{
String value = null;
cookie[] cookies = request.getcookies ();
if (cookies!=null) {
for (int i=0;i<cookies.length;i++) {
Cookie cookie = cookies[i];
if (Cookie.getname (). Equals (name)) {
Value = Urldecoder.decode (Cookie.getvalue (), "utf-8");
}
}
}
return value;
}
/**
* @Function: Delete cookies
* @Author: ZZP
* @param name
* @param response void
* @Date: 2014-2-19
* @Modifications:
* @Modifier Name; Date; The Reason for modifying
*
*/
public static void Deletecookie (String name,httpservletresponse
Response) {
Cookie cookie = new Cookie (name, "");
Cookie.setmaxage (0);
Cookie.setpath (Default_path);
Response.addcookie (cookie);
}
}

Tool Class _java Action 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.