java cookie共享_JavaWeb的session及其共享技术
原標題:JavaWeb的session及其共享技術
1.什么叫會話
一次會話指的是:就好比打電話,A給B打電話,接通之后,會話開始,直到掛斷電話,該次會話就結(jié)束了,而瀏覽器訪問服務器,就跟打電話一樣,瀏覽器A給服務器發(fā)送請求,訪問web程序,該次會話就已經(jīng)接通,其中不管瀏覽器發(fā)送多少請求(就相當于接通電話后說話一樣),都視為一次會話,直到瀏覽器關閉,本次會話結(jié)束。
其中注意,一個瀏覽器就相當于一部電話,如果使用火狐瀏覽器,訪問服務器,就是一次會話了,然后打開google瀏覽器,訪問服務器,這是另一個會話,雖然是在同一臺電腦,同一個用戶在訪問,但是,這是兩次不同的會話。
2.引入cookie和session
思考一個問題,一個瀏覽器訪問一個服務器就能建立一個會話,如果別的電腦,都同時訪問該服務器,就會創(chuàng)建很多會話,就拿一些購物網(wǎng)站來說,我們訪問一個購物網(wǎng)站的服務器,會話就被創(chuàng)建了,然后就點擊瀏覽商品,對感興趣的商品就先加入購物車,等待一起付賬,這看起來是很普通的操作,但是想一下,如果有很多別的電腦上的瀏覽器同時也在訪問該購物網(wǎng)站的服務器,跟我們做類似的操作呢?服務器又是怎么記住用戶,怎么知道用戶A購買的任何商品都應該放在A的購物車內(nèi),不論是用戶A什么時間購買的,不能放入用戶B或用戶C的購物車內(nèi)的呢?
這里我們就用cookie和session兩種會話跟蹤技術來跟蹤整個會話。
3.cookie簡介
3.1.cookie的工作原理
1)首先瀏覽器向服務器發(fā)出請求。
2)服務器就會根據(jù)需要生成一個Cookie對象,并且把數(shù)據(jù)保存在該對象內(nèi)。
3)然后把該Cookie對象放在響應頭,一并發(fā)送回瀏覽器。
4)瀏覽器接收服務器響應后,提出該Cookie保存在瀏覽器端。
5)當下一次瀏覽器再次訪問那個服務器,就會把這個Cookie放在請求頭內(nèi)一并發(fā)給服務器。
服務器從請求頭提取出該Cookie,判別里面的數(shù)據(jù),然后作出相應的動作。
3.2.cookie中的常用方法
Cookie cookie=new Cookie(String name,String value) 構(gòu)造一個cookie對象
response.addCookie(Cookie cookie) 是將一個cookie對象傳入客戶端。
request.getCookies() 得到所有的cookie對象
cookie.getName() 得到此cookie對象的名字
cookie.getValue() 得到對應名稱的cookie的值
cookie.setMaxAge() 設置過期時間
3.3.案例
1@WebServlet( "/CookieServletDemo1")
2publicclassCookieServletDemo1extendsHttpServlet{
3privatestaticfinallongserialVersionUID = 1L;
4
5/**
6* @seeHttpServlet#HttpServlet()
7*/
8publicCookieServletDemo1(){
9super();
10// TODO Auto-generated constructor stub
11}
12
13/**
14* @seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse
15* response)
16*/
17protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)
18throwsServletException, IOException {
19// 創(chuàng)建cookie對象
20// cookie中`存放的數(shù)據(jù)以鍵值對存在map 鍵和值都只能是字符串,不支持中文
21Cookie cookie = newCookie( "username", "zhangsan");
22Cookie cookie1 = newCookie( "password", "1234");
23// 失效時間 以秒為單位
24cookie.setMaxAge( 60* 60);
25response.addCookie(cookie);
26response.addCookie(cookie1);
27}
28
29/**
30* @seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse
31* response)
32*/
33protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)
34throwsServletException, IOException {
35// TODO Auto-generated method stub
36doGet(request, response);
37}
38
39}
1@WebServlet( "/CookieServletDemo2")
2publicclassCookieServletDemo2extendsHttpServlet{
3privatestaticfinallongserialVersionUID = 1L;
4
5/**
6* @seeHttpServlet#HttpServlet()
7*/
8publicCookieServletDemo2(){
9super();
10// TODO Auto-generated constructor stub
11}
12
13/**
14* @seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse
15* response)
16*/
17protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)
18throwsServletException, IOException {
19// 從請求頭中獲取cookie信息
20Cookie[] cookies = request.getCookies();
21for(Cookie c:cookies){
22System.out.println(c.getName()+ "===="+c.getValue());
23}
24}
25
26/**
27* @seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse
28* response)
29*/
30protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)
31throwsServletException, IOException {
32// TODO Auto-generated method stub
33doGet(request, response);
34}
35
36}
3.4.瀏覽器中查看cookie
不同的瀏覽器略有差別,這里以谷歌瀏覽器為例。
F12打開開發(fā)者工具 —點擊Application選項—選中其中的cookie—選擇相應的站點—看到該站點中的所有cookie信息。
3.5.cookie的應用場景
cookie的應用場景有很多,最具代表性的當屬網(wǎng)站的記錄用戶賬號和密碼的功能了,大家可能經(jīng)??吹降卿浤衬痴搲?#xff0c;某某網(wǎng)站時,下面有個選項為N天內(nèi)自動登錄,其實這就是cookie的應用。當用戶第一次輸入賬號密碼時給服務器發(fā)送請求時,服務器會根據(jù)賬號密碼回寫一個字符串cookie,當用戶下次再向該服務器發(fā)送登錄請求時,則帶著這個字符串cookie一起去訪問服務器,這時,服務器只需要對比次字符串和數(shù)據(jù)庫中存儲的字符串是否相同,則可以達到用戶自動登錄功能。
3.6.cookie的局限性
Cookie數(shù)量和長度的限制。每個站點最多只能有20條cookie,每個cookie長度不能超過4KB,否則會被截掉。
cookie中只能存字符串。且不支持中文
cookie不適合保存敏感數(shù)據(jù)(例如密碼)可見的
4.session
4.1.session的工作原理
1)瀏覽器發(fā)出請求到服務器。
2)服務器會根據(jù)需求生成Session對象,并且給這個Session對象一個編號,一個編號對應一個Session對象
3)服務器把需要記錄的數(shù)據(jù)封裝到這個Session對象里,然后把這個Session對象保存下來。
4)服務器把這個Session對象的編號放到一個Cookie里,隨著響應發(fā)送給瀏覽器
5)瀏覽器接收到這個cookie就會保存下來
6)當下一次瀏覽器再次請求該服務器服務,就會發(fā)送該Cookie
7)服務器得到這個Cookie,取出它的內(nèi)容,它的內(nèi)容就是一個Session的編號!!!
8)憑借這個Session編號找到對應的Session對象,然后利用該Session對象把保存的數(shù)據(jù)取出來!
4.2.session的常用方法
4.3.案例
1@WebServlet( "/SeesionServletDemo1")
2publicclassSeesionServletDemo1extendsHttpServlet{
3privatestaticfinallongserialVersionUID = 1L;
4
5/**
6* @seeHttpServlet#HttpServlet()
7*/
8publicSeesionServletDemo1(){
9super();
10// TODO Auto-generated constructor stub
11}
12
13/**
14* @seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse
15* response)
16*/
17protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)
18throwsServletException, IOException {
19
20// 創(chuàng)建session對象
21HttpSession session = request.getSession();
22System.out.println(session.getId());
23
24session.setAttribute( "name", "zhangsan");
25
26session.setAttribute( "student", newStudent( 1, "zhangsan", 12));
27Student stu = (Student) session.getAttribute( "student");
28
29stu.setName( "lisi");
30
31// 設置過期時間 單位是秒
32// session.setMaxInactiveInterval(2);
33// 直接銷毀session 注銷登錄
34// session.invalidate();
35
36}
37
38/**
39* @seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse
40* response)
41*/
42protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)
43throwsServletException, IOException {
44// TODO Auto-generated method stub
45doGet(request, response);
46}
47
48}
1publicclassStudent{
2
3privateInteger id;
4privateString name;
5privateInteger age;
6publicInteger getId(){
7returnid;
8}
9publicvoidsetId(Integer id){
10this.id = id;
11}
12publicString getName(){
13returnname;
14}
15publicvoidsetName(String name){
16this.name = name;
17}
18publicInteger getAge(){
19returnage;
20}
21publicvoidsetAge(Integer age){
22this.age = age;
23}
24publicStudent(Integer id, String name, Integer age){
25super();
26this.id = id;
27this.name = name;
28this.age = age;
29}
30publicStudent(){
31super();
32// TODO Auto-generated constructor stub
33}
34}
1@WebServlet( "/SeesionServletDemo2")
2publicclassSeesionServletDemo2extendsHttpServlet{
3privatestaticfinallongserialVersionUID = 1L;
4
5/**
6* @seeHttpServlet#HttpServlet()
7*/
8publicSeesionServletDemo2(){
9super();
10// TODO Auto-generated constructor stub
11}
12
13/**
14* @seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse
15* response)
16*/
17protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)
18throwsServletException, IOException {
19
20// 創(chuàng)建session對象
21HttpSession session = request.getSession();
22
23System.out.println(session.getAttribute( "name"));
24
25Student stu = (Student) session.getAttribute( "student");
26System.out.println(stu.getName());
27
28}
29
30/**
31* @seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse
32* response)
33*/
34protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)
35throwsServletException, IOException {
36// TODO Auto-generated method stub
37doGet(request, response);
38}
39
40}
4.4.session的生命周期
session對象生命周期:
session對象什么創(chuàng)建?
執(zhí)行request.getSession()方法時
session對象什么銷毀?
①默認情況下,session對象在30分鐘之后服務器自動銷毀。
②手動設置session有效時長
void setMaxInactiveInterval(int interval) -以秒為單位。
③手動銷毀
void invalidate()
4.5.session在一次會話結(jié)束后消失的原因
由于session的使用需要依賴cookie,cookie每次從瀏覽器端傳輸session的id到后臺,然后查找對應編號的session進行使用,但由于此時的cookie默認的失效時間是一次會話,當一次會話結(jié)束后,存放id的cookie對象就消失了,下一次會話訪問時就會生成新的id,那存儲在原來的session對象中的數(shù)據(jù)就無法找到了。
4.6.瀏覽器禁用cookie能否使用session
可以,但需要手動拼接id傳過去,例如
1http: //localhost:8080/day06/session.jsp;jsessionid=AE62ECBAAD2CA16DA6AEBF1D1527CD45
jsessionid指的就是session對應的id
4.7.session共享
4.7.1.基于數(shù)據(jù)庫的Session共享
首選當然是大名鼎鼎的Mysql數(shù)據(jù)庫,并且建議使用內(nèi)存表Heap,提高session操作的讀寫效率。這個方案的實用性比較強,相信大家普遍在使用,它的缺點在于session的并發(fā)讀寫能力取決于Mysql數(shù)據(jù)庫的性能,同時需要自己實現(xiàn)session淘汰邏輯,以便定時從數(shù)據(jù)表中更新、刪除 session記錄,當并發(fā)過高時容易出現(xiàn)表鎖,雖然我們可以選擇行級鎖的表引擎,但不得不否認使用數(shù)據(jù)庫存儲Session還是有些殺雞用牛刀的架勢。
4.7.2.基于Cookie的Session共享
這個方案我們可能比較陌生,但它在大型網(wǎng)站中還是比較普遍被使用。原理是將全站用戶的Session信息加密、序列化后以Cookie的方式, 統(tǒng)一種植在根域名下(如:.host.com),利用瀏覽器訪問該根域名下的所有二級域名站點時,會傳遞與之域名對應的所有Cookie內(nèi)容的特性,從而實現(xiàn) 用戶的Cookie化Session 在多服務間的共享訪問。
這個方案的優(yōu)點無需額外的服務器資源;缺點是由于受http協(xié)議頭信心長度的限制,僅能夠存儲小部分的用戶信息,同時Cookie化的 Session內(nèi)容需要進行安全加解密(如:采用DES、RSA等進行明文加解密;再由MD5、SHA-1等算法進行防偽認證),另外它也會占用一定的帶寬資源,因為瀏覽器會在請求當前域名下任何資源時將本地Cookie附加在http頭中傳遞到服務器。
4.7.3.基于Memcache的Session共享
Memcache由于是一款基于Libevent多路異步I/O技術的內(nèi)存共享系統(tǒng),簡單的Key + Value數(shù)據(jù)存儲模式使得代碼邏輯小巧高效,因此在并發(fā)處理能力上占據(jù)了絕對優(yōu)勢,目前本人所經(jīng)歷的項目達到2000/秒 平均查詢,并且服務器CPU消耗依然不到10%。
另外值得一提的是Memcache的內(nèi)存hash表所特有的Expires數(shù)據(jù)過期淘汰機制,正好和Session的過期機制不謀而合,降低了 過期Session數(shù)據(jù)刪除的代碼復雜度,對比“基于數(shù)據(jù)庫的存儲方案”,僅這塊邏輯就給數(shù)據(jù)表產(chǎn)生巨大的查詢壓力。
更多資深講師相關課程資料、學習筆記請入群后向管理員免費獲取,更有專業(yè)知識答疑解惑。入群即送價值499元在線課程一份。
QQ群號:560819979
敲門磚(驗證信息):醉漁唱晚返回搜狐,查看更多
責任編輯:
總結(jié)
以上是生活随笔為你收集整理的java cookie共享_JavaWeb的session及其共享技术的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fso 拒绝访问_java.sql.sq
- 下一篇: java dagger2_从零开始搭建一