筆記-JavaWeb學習之旅12

會話技術

Cookie:客戶端會話技術,將數據保存到客戶端java

package com.data.Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/CookieDemo1")
public class CookieDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //建立Cookie對象
        Cookie cookie = new Cookie("msg","hello");
        //發送Cookie
        response.addCookie(cookie);
        //當訪問CookieDemo1的Servlet的時候,會建立一個Cookie對象
        //而後把綁定的數據發送給瀏覽器
    }
}
package com.data.Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/CookieDemo2")
public class CookieDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取Cookie
        Cookie[] cs = request.getCookies();
        if( cs != null){
            //遍歷cs,拿到每個cookie對象
            for(Cookie c : cs){
                String name = c.getName();
                String value = c.getValue();
                System.out.println(name+":"+value);
            }
        }

    }
}
輸出結果
msg:hello
Idea-a0bb733:23efa255-9ce7-42d9-8d0b-9853348226b8

會話技術原理分析瀏覽器

img

img

JSP

概念:Java Server Pages : java服務器端頁面,能夠理解爲一個特殊的頁面,其中既能夠定義Html標籤,又能夠定義java代碼安全

JSP的腳本:JSP定義代碼的方式服務器

1.<% 代碼 %>: 定義的代碼,在service方法中。cookie

2.<%! 代碼 %>:定義的java代碼,在jsp轉換後的java類的成員文字session

3.<%+ 代碼 %>:定義的java代碼,會輸出到頁面上jsp

會話技術_Session

概念:在服務器端會話技術,在一次會話的屢次請求間共享數據,將數據保存在服務器端的對象中。HttpSessionide

package com.data.session;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/sessionDemo1")
public class SessionDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //獲取HttpSession對象
        HttpSession session = req.getSession();
        //當客戶端關閉後,服務器不關閉,兩次獲取的session不是同一個對象
        //若是須要相同,能夠建立Cookie對象,設置id值,由於session的實現依賴於cookie,只要id值不變就是同一個對象
        Cookie cookie = new Cookie("JSESSIONID",session.getId());//JSESSIONID=226E48B3AD9F203B2BCBFCA2E6EA5D71
        cookie.setMaxAge(60*60);//一個小時內關閉客戶端都是同一個對象,由於他們的ID值相同
        resp.addCookie(cookie);
        //在服務器端存儲數據
        session.setAttribute("msg","hello,Session");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
package com.data.session;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/sessionDemo2")
public class SessionDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //建立對象
        HttpSession session = req.getSession();
        //經過鍵獲取值
        Object msg = session.getAttribute("msg");
        System.out.println(msg);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}

session原理:Session的實現依賴於Cookie的,服務在一次會話範圍內,屢次獲取的Session對象是同一個。this

session細節

客戶端關閉後,服務器不關閉,兩次獲取session不是同一個對象,如需相同,能夠建立Cookie,設置屬性。code

客戶端不關閉,服務器關閉後,兩次獲取的session不是同一個對象,

session在服務器被關閉時銷燬,在調用invalidata()時也會銷燬,默認失效時間是30分鐘

session特色

session用於存儲異常會話的屢次請求數據,存在服務器端,session能夠存儲任意類型,任意大小的數據

session與cookie的區別:

session存儲數據在服務器端,cookie在客戶端

session沒有數據大小限制,cookie有

session數據安全,cookie相對於不安全

驗證碼案列 寫了4個小時 ,獲得的結果500錯誤,尚沒有找到bug,明天再看吧,

相關文章
相關標籤/搜索