国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

cookie案例

2019-11-15 00:13:37
字體:
來源:轉載
供稿:網友
cookie案例

Summary:

cookie是以字符串的形式保存數據的,把數據保存在客戶端.

一個cookie只能標識一種信息,它至少包含有一個標識該信息的名稱(name)和設置值(value)

setMaxAge(0)//可以刪除cookie,用javaScript也可以刪除

//常用的方法

setMaxAge()//設置最大的有效期

setValue()|getValue()

getName();

setPath()|getPath()

/**

* cookie測試

* @author 邵海雄

* @date 2015-5-18 下午07:12:33

*/

public class CookieServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=UTF-8");

PRintWriter out = response.getWriter();

out.print("你上次訪問的時間是:");

//獲取用戶時間的cookie

Cookie cookies[] =request.getCookies();

for (int i = 0;cookies!=null&& i < cookies.length; i++) {

if (cookies[i].getName().equals("lastaccessTime")) {

//得到用戶上次的訪問時間

long cookieValue = Long.parseLong(cookies[i].getValue());

Date date = new Date(cookieValue);

out.print(DateFormat.getDateInstance().format(date));

}

}

//給用戶回送最新訪問的時間

Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+"");

//設置有效期

cookie.setMaxAge(1*30*24*3600);//1*30*24*3600 這是一個月的時間

//設置cookie保存的路徑

cookie.setPath("/Cookie");

//添加Cookie

response.addCookie(cookie);

}

}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麦盖提县| 万山特区| 太和县| 华坪县| 福安市| 东辽县| 高台县| 武义县| 郯城县| 郓城县| 东乡县| 江西省| 曲周县| 陕西省| 海阳市| 浦东新区| 含山县| 同德县| 化州市| 五大连池市| 石河子市| 林芝县| 东乌珠穆沁旗| 新和县| 蚌埠市| 潮安县| 恭城| 珲春市| 乐山市| 西贡区| 吉林省| 繁昌县| 溧水县| 辽宁省| 霸州市| 台东市| 祁阳县| 鹤岗市| 昭通市| 庐江县| 静宁县|