标签:second get 网站 html for ott 功能 add 定制
Cookie objCookie=new Cookie(indexValue,stringValue) 例如:Cookie c=new Cookie("mycookie","Cookie Test");传送方式为response.addCookie(objCookie)。
写Cookie |
<%//从HTML表单中获取用户名 String username =request.getParameter(name); Cookie user_name=new Cookie("cookie_name",username);//创建一个cookie response.addCookie(user_name); %> |
读Cookie |
<% Cookie[]Cookies=request.getCookie();//创建一个Cookie对象数组 if(Cookie==null){ out.print("none any Cookie"); } else{ for(int i=0;i<Cookies.length;i++){ //设立一个循环,来访问Cookie对象数组的每一个元素 if(Cookie[i].getName().equals("cookie_name") ) { //判断元素的值是否为username中的值 out.print(Cookie[i].getValue()+"<br/>"); } } } %> |
<% Cookie login=new Cookie("today","true"); login.setMaxAge(86400);//设置Cookie存在的时间 reponse.addCookie(login); %> |
inport javax.servlet.http.*; public class LongLiveCookie extends Cookie{ public static final int SECONDS_PER_YEAR =60*60*24*365; public LongLivedCookie(String name,String value){ super(name ,value); setMaxAge(SECONDS_PER_YEAR); } } |
<% Cookie killMyCookie=new Cookie("mycookie",null); killMyCookie.setMaxAge(0); killMyCookie.setPath("/"); reponse.addCookie(killMyCookie); %> |
public static String getCookie Value(Cookie[]cookies, String cookieName,String defaultValue){ for(int i=0;i<cookies.length;i++){ Cookie cookie =cookies[i]; if(cookieName.equals(cookie.getName())) return(cookie.getValue()); } return(defaultValue); } |
标签:second get 网站 html for ott 功能 add 定制
原文地址:http://www.cnblogs.com/bonly-ge/p/7051225.html