會話技術Cookie&Session

會話:從打開一個瀏覽器訪問某個站點,到關閉這個瀏覽器的整個過程,稱爲一次會話。html

Cookie:數據存儲在客戶端本地,減小服務器端的存儲的壓力,安全性很差,客戶端能夠清除cookie。java

Session:將數據存儲到服務器端,安全性相對好,增長服務器的壓力。數組

Cookie技術:瀏覽器

建立Cookie:Cookie cookie=new Cookie(String cookieName,String cookieValue);安全

Cookie中不能存儲中文服務器

cookie.setMaxAge(int seconds);--時間秒cookie

cookie.setPath(String path);設置Cookie的攜帶路徑session

response.addCookie(Cookie cookie);向客戶端發送cookieoracle

 

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 {
        //1.建立Cookie對象
        Cookie cookie=new Cookie("goods", "naiping");
        //設置持久化時間
        cookie.setMaxAge(60*2);    
        //設置Cookie攜帶路徑
        //cookie.setPath("/WEB07/SendCookieServlet");
        //2.發送Cookie
        response.addCookie(cookie);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

刪除客戶端的cookie:spa

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 RemovCookieServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //刪除Cookie
        Cookie cookie=new Cookie("goods", "naiping");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

 

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[] cookies=request.getCookies();
        //遍歷Cookie數組經過cookie名獲取cookie值
        for(Cookie c:cookies){
            if(c.getName().equals("goods")){
                System.out.println(c.getValue());
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

 

 

Session技術:

Session技術是將數據存儲在服務器端的技術,會爲每一個客戶端都建立一塊內存空間 存儲客戶的數據,但客戶端須要每次都攜帶一個標識ID去服務器中尋找屬於本身的內 存空間。

建立:HttpSession session = reques.getSession();

session中存取數據(session也是一個域對象)

存儲:session.setAttribute(String name,Object obj);

獲取:session.getAttribute(String name);

刪除:session.removeAttribute(String name);

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;
import javax.servlet.http.HttpSession;

public class SessionServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取Session對象
        HttpSession session=request.getSession();
        //向session域中存儲數據
        session.setAttribute("goods", "奶瓶");
        //獲取JSESSIONID
        String id=session.getId();
        //建立Cookie
        Cookie cookie=new Cookie("JSESSIONID", id);
        cookie.setPath("WEB08");
        cookie.setMaxAge(60*3);
        //發送cookie
        response.addCookie(cookie);
        response.getWriter().write("JSESSIONID:"+id);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
package com.oracle.demo01;

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 Servlet02 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取Session對象
        HttpSession session=request.getSession();
        //獲取session中的內容
        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);
    }
}

 Session的生命週期:

建立:第一次執行request.getSession()時。

銷燬:服務器關閉時;

   session失效(默認30分鐘):時間從不操做服務器端的資源開始計時。

   手動調用invalidate();

做用範圍:默認在一次會話中,一次會話中任何資源公用一個session對象。

相關文章
相關標籤/搜索