JavaWeb之會話技術
會話技術
Cookie:
1. 概念:客戶端會話技術,將資料保存到客戶端
2. 快速入門:
3. 實作原理
4.注意事項:
5. Cookie的特點和作用
代碼實作
Session:
1. 概念:服務器端會話技術,在一次會話的多次請求間共享資料,將資料保存在服務器端的物件中,HttpSession
2. 快速入門:
3. 原理
4. 細節:
5. session的特點
代碼實作
session與Cookie的區別:
創作不易,如果本篇博客對您有一定的幫助,大家記得留言+點贊哦,
會話技術
1. 會話:一次會話中包含多次請求和回應,
一次會話:瀏覽器第一次給服務器資源發送請求,會話建立,直到有一方斷開為止
2. 功能:在一次會話的范圍內的多次請求間,共享資料
3. 方式:
1. 客戶端會話技術:Cookie
2. 服務器端會話技術:Session
Cookie:
1. 概念:客戶端會話技術,將資料保存到客戶端
2. 快速入門:
1. 創建Cookie物件,系結資料
new Cookie(String name, String value)
2. 發送Cookie物件
response.addCookie(Cookie cookie)
3. 獲取Cookie,拿到資料
Cookie[] request.getCookies()
3. 實作原理
基于回應頭set-cookie和請求頭cookie實作
4.注意事項:
1.一次可以發送多個cookie,可以創建多個Cookie物件,使用response呼叫多次addCookie方法發送cookie即可,
2. cookie在瀏覽器中保存的保存時間,使用setMaxAge(int seconds)方法可以將Cookie資料寫到硬碟的檔案中,并指定cookie存活時間
3. 默認情況下一個tomcat服務器中,部署了多個web專案,那么在這些web專案中cookie不能共享,如果要共享通過setPath(String path)設定當前虛擬目錄:path設定為"/"
4. 不同的tomcat服務器間cookie共享問題?使用setDomain(String path)方法:如果設定一級域名相同,那么多個服務器之間cookie可以共享
5. Cookie的特點和作用
1. cookie存盤資料在客戶端瀏覽器
2. 瀏覽器對于單個cookie 的大小有限制(4kb) 以及 對同一個域名下的總cookie數量也有限制(20個)
a. cookie一般用于存出少量的不太敏感的資料
b. 在不登錄的情況下,完成服務器對客戶端的身份識別
代碼實作
//記錄電腦上次打開網頁的時間
@WebServlet("/CookieTest")
public class CookieTest extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//設定回應體格式與編碼
resp.setContentType("text/html;charset=utf-8");
//獲取cookie
Cookie[] cookies = req.getCookies();
boolean flag=false;
//遍歷cookie資料
if (cookies!=null && cookies.length>0){
for (Cookie cookie : cookies) {
String name = cookie.getName();
//判斷名稱中是否有:lastTime
if ("lastTime".equals(name)){
//設定Cookie的value,下一次使用
flag=true;
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date);
//tomcat不支持特殊字符,需要通過URL編碼
System.out.println("編碼前 "+str_date);
str_date=URLEncoder.encode(str_date,"utf-8");
System.out.println("編碼后"+str_date);
//新值串回去,設定存活時間
cookie.setValue(str_date);
cookie.setMaxAge(60*60*24);
resp.addCookie(cookie);
//有,歡迎光臨,上次登錄時間
String value = cookie.getValue();
//URL 解碼
value=URLDecoder.decode(value,"utf-8");
resp.getWriter().write("<h1>歡迎回來,您上一次的訪問時間是:"+value+"</h1>");
break;
}
}
}
if (cookies==null || cookies.length==0 || flag==false){
//第一次訪問
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date);
//tomcat不支持特殊字符,需要通過URL編碼
System.out.println("編碼前 "+str_date);
str_date=URLEncoder.encode(str_date,"utf-8");
System.out.println("編碼后"+str_date);
Cookie cookie=new Cookie("lastTime",str_date);
//新值串回去,設定存活時間
cookie.setValue(str_date);
cookie.setMaxAge(60*60*24);
resp.addCookie(cookie);
resp.getWriter().write("<h1>您好,歡迎您首次訪問</h1>");
}
}
}
Session:
1. 概念:服務器端會話技術,在一次會話的多次請求間共享資料,將資料保存在服務器端的物件中,HttpSession
2. 快速入門:
1. 獲取HttpSession物件:
HttpSession session = request.getSession();
2. 使用HttpSession物件:
Object getAttribute(String name)
void setAttribute(String name, Object value)
void removeAttribute(String name)
3. 原理
Session的實作是依賴于Cookie的,
4. 細節:
1. 當客戶端關閉后,服務器不關閉,兩次獲取session是否為同一個?
默認情況下,不是,
如果需要相同,則可以創建Cookie,鍵為JSESSIONID,設定最大存活時間,讓cookie持久化保存,
Cookie c = new Cookie("JSESSIONID",session.getId());
c.setMaxAge(60*60);
response.addCookie(c);
2. 客戶端不關閉,服務器關閉后,兩次獲取的session是同一個嗎?
不是同一個,但是要確保資料不丟失,tomcat自動完成以下作業
session的鈍化: 在服務器正常關閉之前,將session物件系列化到硬碟上
session的活化: 在服務器啟動后,將session檔案轉化為記憶體中的session物件即可,
3.session什么時候被銷毀?
1. 服務器關閉
2. session物件呼叫invalidate() ,
3. session默認失效時間 30分鐘
5. session的特點
1. session用于存盤一次會話的多次請求的資料,存在服務器端
2. session可以存盤任意型別,任意大小的資料
代碼實作
@WebServlet("/SessionDemo1")
public class SessionDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//session
HttpSession session = request.getSession();
session.setAttribute("msg","hello session");
//請求轉發(這個是在一個URL中)
request.setAttribute("reqmsg","hello req.session");
request.getRequestDispatcher("/SessionDemo3").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
@WebServlet("/SessionDemo3")
public class SessionDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取資料
HttpSession session = request.getSession();
Object msg = session.getAttribute("msg");
System.out.println(msg);
//請求轉發
Object reqmsg = request.getAttribute("reqmsg");
System.out.println(reqmsg);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
session與Cookie的區別:
1.session存盤資料在服務器端,Cookie在客戶端
2.session沒有資料大小限制,Cookie有
3.session資料安全,Cookie相對于不安全
4.session會在一定時間內保存在服務器上,當訪問增多,會比較占用你服務器的性能,考慮到減輕服務器性能方面,應當使用cookie,
創作不易,如果本篇博客對您有一定的幫助,大家記得留言+點贊哦,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/264147.html
標籤:java
