從打開一個瀏覽器訪問某個站點,到關閉這個瀏覽器的整個過程,成爲一次會話。會 話技術就是記錄此次會話中客戶端態的狀與數據的。html
會話技術分爲Cookie和Session:java
Cookie:數據存儲在客戶端本地,減小服務器端的存儲的壓力,安全性很差,客戶端 能夠清除cookieweb
Session:將數據存儲到服務器端,安全性相對好,增長服務器的壓力面試
1)建立Cookie:數組
Cookie cookie = new Cookie(String cookieName,String cookieValue);
示例:瀏覽器
Cookie cookie = new Cookie("username","zhangsan");
該cookie會以響應頭的形式發送給客戶端:安全
Set-Cookie:"name=zhangsan"
注意:Cookie中不能存儲中文服務器
2)設置Cookie在客戶端的持久化時間:cookie
cookie.setMaxAge(int seconds); ---時間秒session
注意:若是不設置持久化時間,cookie會存儲在瀏覽器的內存中,瀏覽器關閉 cookie信息銷燬(會話級別的cookie),若是設置持久化時間,cookie信息會被持久化到瀏覽器的磁盤文件裏
示例:
cookie.setMaxAge(10*60);
設置cookie信息在瀏覽器的磁盤文件中存儲的時間是10分鐘,過時瀏覽器 自動刪除該cookie信息
3)設置Cookie的攜帶路徑:
cookie.setPath(String path);
注意:若是不設置攜帶路徑,那麼該cookie信息會在訪問產生該cookie的 web資源所在的路徑都攜帶cookie信息
//示例: cookie.setPath("/WEB16"); //表明訪問WEB16應用中的任何資源都攜帶cookie cookie.setPath("/WEB16/cookieServlet"); //表明訪問WEB16中的cookieServlet時才攜帶cookie信息
4)向客戶端發送cookie:
response.addCookie(Cookie cookie);
5)刪除客戶端的cookie:
若是想刪除客戶端的已經存儲的cookie信息,那麼就使用同名同路徑的持久化時 間爲0的cookie進行覆蓋便可
例子:
package com.oracle.demo01; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SendCookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //建立cookie Cookie cookie=new Cookie("goods","phone"); //設置cookie持久化存儲時間,也就是在磁盤中保存時間 cookie.setMaxAge(60*3); //設置cookie攜帶路徑 // cookie.setPath("/Web06/SendCookieServlet"); //cookie.setPath("/WEB16"); //表明訪問WEB16應用中的任何資源都攜帶cookie //cookie.setPath("/WEB16/cookieServlet"); //表明訪問WEB16中的cookieServlet時才攜帶cookie信息 //cookie.setPath("/");表明服務器下全部資源都攜帶 //刪除cookie //發送cookie response.addCookie(cookie); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
清除Cookies
package com.oracle.demo01; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class removecookieservlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //刪除客戶端保存的信息 //建立cookie Cookie cookie=new Cookie("goods","phone"); //設置cookie攜帶路徑 cookie.setPath("/Web06/SendCookieServlet"); cookie.setMaxAge(0); //發送cookie response.addCookie(cookie); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
cookie信息是以請求頭的方式發送到服務器端的:
Cookies:"name=zhangsan"
1)經過request得到全部的Cookie:
Cookie[] cookies = request.getCookies();
2)遍歷Cookie數組,經過Cookie的名稱得到咱們想要的Cookie
for(Cookie cookie : cookies){ if(cookie.getName().equal(cookieName)){ String cookieValue = cookie.getValue(); } }
例子:
package com.oracle.demo01; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GETCookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到客戶端攜帶的cookie Cookie[] cookies = request.getCookies(); //遍歷,經過cookie名稱得到想要的cookie值 for(Cookie c : cookies){ if(c.getName().equals("goods")){ String cookieValue = c.getValue(); System.out.println(cookieValue); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
例子:顯示用戶上次訪問時間
第一次訪問,你是第一次訪問,
非第一次訪問,顯示上次訪問時間
package com.oracle.demo02; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //顯示用戶上次訪問時間 //第一次訪問,你是第一次訪問, //非第一次訪問,顯示上次訪問時間 public class WelcomeServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取當前時間 Date date=new Date(); SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String time=sdf.format(date); //建立cookie對象 Cookie cookie=new Cookie("lastAccessTime",time); cookie.setMaxAge(60*3); response.addCookie(cookie); //獲取cookie值 Cookie[] cookies = request.getCookies(); String lastAccessTime=null; if(cookies!=null){ for(Cookie c : cookies){ if(c.getName().equals("lastAccessTime")){ lastAccessTime = c.getValue(); } } } //解決響應時中文亂碼問題 response.setContentType("text/html;charset=utf-8"); if(lastAccessTime==null){ response.getWriter().write("你是第一次訪問"); }else{ response.getWriter().write("你上次訪問的時間"+lastAccessTime); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Session技術是將數據存儲在服務器端的技術,會爲每一個客戶端都建立一塊內存空間 存儲客戶的數據,但客戶端須要每次都攜帶一個標識ID去服務器中尋找屬於本身的內存空間。因此說Session的實現是基於Cookie,Session須要藉助於Cookie存儲客 戶的惟一性標識JSESSIONID
HttpSession session = request.getSession();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //建立屬於該客戶端(會話)的私有的session區域 // request.getSession()方法內部會判斷 該客戶端是否在服務器端已經存在session // 若是該客戶端在此服務器不存在session 那麼就會建立一個新的session對象 // 若是該客戶端在此服務器已經存在session 那麼就得到已經存在的該session返回 HttpSession session=request.getSession(); String id=session.getId();//該session對象的編號id response.getWriter().write("JESSIONID:"+id); }
Session也是存儲數據的區域對象,因此session對象也具備以下三個方法:
session.setAttribute(String name,Object obj);
session.getAttribute(String name);
session.removeAttribute(String name);
Session對象的生命週期
建立:第一次執行request.getSession()時建立
銷燬:
1)服務器(非正常)關閉時
2)session過時/失效(默認30分鐘)
時間的起算點 從不操做服務器端的資源開始計時
能夠在工程的web.xml中進行配置
<session-config> <session-timeout>30</session-timeout> </session-config>
3)手動銷燬session
session.invalidate();
做用範圍:
默認在一次會話中,也就是說在,一次會話中任何資源公用一個session對象
例子:
package com.oracle.demo03; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class SessionServlet1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取session對象 HttpSession session = request.getSession(); //向session中存數據 session.setAttribute("goods","手機"); String id=session.getId(); //將JSESSIONID持久化存儲 Cookie cookie=new Cookie("JSESSIONID",id); cookie.setPath("/Web06"); cookie.setMaxAge(60*3); response.addCookie(cookie); response.getWriter().write("JSESSIONID:"+id); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.oracle.demo03; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class SessionServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到session對象 HttpSession session = request.getSession(); //獲取值 String goods=(String)session.getAttribute("goods"); response.setContentType("text/html;charset=utf-8"); response.getWriter().write(goods); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
總結:
會話技術:
Cookie技術:存到客戶端
發送cookie
Cookie cookie = new Cookie(name,value)
cookie.setMaxAge(秒)
cookie.setPath()
response.addCookie(cookie)
得到cookie
Cookie[] cookies = request.getCookies();
cookie.getName();
cookie.getValue();
Session技術:存到服務器端 藉助cookie存儲JSESSIONID
HttpSession session = request.getSession();
setAttribute(name,value);
getAttribute(name);
session生命週期
建立:第一次指定request.getSession();
銷燬:服務器關閉、session失效/過時、手動session.invalidate();
session做用範圍:默認一會話中