JavaWeb基礎 Cookie 發送和接收cookie
阿新 • • 發佈:2017-12-06
response html getname myeclipse cookies keyword server word head
禮悟:
好好學習合思考,尊師重道存感恩。葉見尋根三返一,江河湖海同一體。
虛懷若谷良心主,願行無悔給最苦。讀書鍛煉強身心,誠勸且行且珍惜。
javaEE:7
javaSE:1.8
JSTL:1.2.2
server:tomcat 8.5
browser:Chrome/Firefox
os:windows7 x64
ide:MyEclipse 2017
項目結構
addCookie.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘addCookie.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h2>客戶端進行保存cookie</h2> <% // cookie就是一個請求頭 // 創建cookie Cookie cookie = new Cookie("time","2017-11-20"); // 添加cookie response.addCookie(cookie); cookie = new Cookie("name","cnblogs-jizuiku"); response.addCookie(cookie); %> </body> </html>
getCookie.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘addCookie.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h2>服務器獲取cookie</h2> <% Cookie[] cookies = request.getCookies(); // 防止NPE if (cookies != null) { for (Cookie c : cookies) { out.print(c.getName() + " : " + c.getValue() + "<br />"); } } %> </body> </html>
瀏覽器進行訪問
跨瀏覽器
感想
發送和接收的還不一樣呢?這。。有意思。有待學習
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。
JavaWeb基礎 Cookie 發送和接收cookie