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