Servlet三大域物件與會話
阿新 • • 發佈:2021-10-08
1 servlet域物件
在指定區域內實現資料共享的物件
域物件的統一方法:
Object getAttribute(String aName);
void removeAttribute(String aName);
void setAttribute(String aName,Object vObj);
Enumeration<String> getAttributeNames();
1 request域物件
requet域物件的共享區域:同一個請求鏈(請求轉發或者請求包含涉及的多個資源)
- 程式碼1:/s0101
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設定request域屬性 request.setAttribute("aa", "aa"+System.currentTimeMillis()); request.setAttribute("bb", "bb"+System.currentTimeMillis()); request.getRequestDispatcher("/s0100").forward(request, response); }
- 程式碼2:/s0100
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); //獲取request域屬性 out.print("request域屬性aa="+request.getAttribute("aa")+"<br/>"); out.print("request域屬性bb="+request.getAttribute("bb")+"<br/>"); }
2 session域物件
session:回話: 同一個瀏覽器對伺服器發出的幾個連貫(預設時間差不能大於30分鐘)的請求
回話跟蹤技術:解決http協議的無狀態(兩次請求之間不能實現資料共享)
- 程式碼1:/s0102
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設定session域屬性 HttpSession session=request.getSession(); session.setAttribute("aa", "aa_session_"+System.currentTimeMillis()); session.setAttribute("bb", "bb_session_"+System.currentTimeMillis()); response.getWriter().print("<a href='http://localhost:8080/java37_ee_06_servlet/s0100'>請求/s0100</a><br/>"); }
- 程式碼2:/s0100
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
//獲取request域屬性
out.print("request域屬性aa="+request.getAttribute("aa")+"<br/>");
out.print("request域屬性bb="+request.getAttribute("bb")+"<br/>");
//獲取session域屬性
HttpSession session=request.getSession();
out.print("session域屬性aa="+session.getAttribute("aa")+"<br/>");
out.print("session域屬性bb="+session.getAttribute("bb")+"<br/>");
}
3 application(servletContext)域物件
servletContext上下文物件:同一個專案的所有請求之間實現資料共享
- 程式碼1:/s0103
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取servletcontext:上下文物件
ServletContext context=request.getServletContext();
context=this.getServletContext();
context=request.getSession().getServletContext();
//新增servletcontext的域屬性
context.setAttribute("aa", "aa_context_"+System.currentTimeMillis());
context.setAttribute("bb", "bb_context_"+System.currentTimeMillis());
response.getWriter().print("<a href='http://localhost:8080/java37_ee_06_servlet/s0100'>請求/s0100</a><br/>");
}
- 程式碼2 :/s0100
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
//獲取request域屬性
out.print("request域屬性aa="+request.getAttribute("aa")+"<br/>");
out.print("request域屬性bb="+request.getAttribute("bb")+"<br/>");
//獲取session域屬性
HttpSession session=request.getSession();
out.print("session域屬性aa="+session.getAttribute("aa")+"<br/>");
out.print("session域屬性bb="+session.getAttribute("bb")+"<br/>");
//獲取servletcontext:上下文物件
ServletContext context=request.getServletContext();
out.print("servletContext域屬性aa="+context.getAttribute("aa")+"<br/>");
out.print("servletContext域屬性aa="+context.getAttribute("aa")+"<br/>");
}
2 會話跟蹤技術之cookie
cookie:由伺服器端建立和客戶端儲存 並通過請求頭和響應頭傳遞的鍵值對物件
程式碼1:在響應頭中新增cookie :::/s0201
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//建立cookie
Cookie c1=new Cookie("c1", "c1_v");
Cookie c2=new Cookie("c2", "c2_v");
//設定cookie的生命週期:
c1.setMaxAge(-1);//如果值為負數::等價於瀏覽器記憶體 瀏覽器不關閉 cookie一值存在,瀏覽器關閉 cookie消失
c1.setMaxAge(0);//如果值為零::刪除cookie
c1.setMaxAge(20);//如果值為正數::cookie儲存在硬碟中指定秒::與瀏覽器記憶體無關
//設定cookie的儲存目錄:預設是當前資源::/java37_ee_06_servlet/s0201
c1.setPath("/");
//把cookie新增到響應頭 傳遞給客戶端瀏覽器
response.setHeader("Set-Cookie", "c3=c3_v");
response.addCookie(c1);
response.addCookie(c2);
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("<a href='http://localhost:8080/java37_ee_06_servlet/s0200'>請求/s0200</a><br/>");
}
程式碼2: 通過請求頭獲取cookie:::/s0200
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取cookie
Cookie[] arr=request.getCookies();
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("接收到的cookie是:"+Arrays.toString(arr));
}
測試請求/s0201
測試請求/s0200
原理解釋
3 會話跟蹤技術之session
回話:同一個瀏覽器發出的幾個連貫(預設是30分鐘)的請求
程式碼1:在session中新增資料
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//在session中新增屬性
HttpSession session=request.getSession();
session.setAttribute("aa", "aa_session_v");
session.setAttribute("bb", "bb_session_v");
//session.invalidate();//銷燬session
//session.setMaxInactiveInterval(arg0);//設定session過期間隔 預設是30分鐘
//session.getMaxInactiveInterval();//獲取session過期間隔
// session.setMaxInactiveInterval(10);
session.getId();//獲取sessionid:::當前回話的session物件的ip地址
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.print("session.getId()="+session.getId()+"<br/>");
out.print("session.getMaxInactiveInterval()="+session.getMaxInactiveInterval()+"<br/>");
out.print("<a href='http://localhost:8080/java37_ee_06_servlet/s0200'>請求/s0200</a><br/>");
}
程式碼2:獲取session中的資料
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取cookie
Cookie[] arr=request.getCookies();
String message="";
for (Cookie c : arr) {
message+=c.getName()+"="+c.getValue()+"<br/>";
}
//獲取session
HttpSession session=request.getSession();
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.print("接收到的cookie是:"+message);
out.print("session域屬性aa="+session.getAttribute("aa")+"<br/>");
out.print("session域屬性bb="+session.getAttribute("bb")+"<br/>");
out.print("session.getId()="+session.getId()+"<br/>");
out.print("session.getMaxInactiveInterval()="+session.getMaxInactiveInterval()+"<br/>");
}
訪問/s0202
session原理
4 cookie和session的區別
參考:https://www.jianshu.com/p/2f7031a69f43
概念
cookie:符合http協議規範的 由伺服器建立 客戶端儲存的 通過請求頭和響應頭傳遞的鍵值對物件
session:由伺服器建立和儲存 但依賴於名為JSESSIONID的cookie的 用於儲存回話資料的物件
相同之處
1 都是解決http無狀態的回話跟蹤技術
2 都是伺服器端建立 都依賴http協議的請求頭和響應頭
不同之處
1 儲存位置不同
cookie由客戶端儲存
session伺服器儲存
2 預設有效時間不同
cookie生命週期預設是瀏覽器記憶體
session預設請求間隔是30分鐘
3 隱私策略不同
cookie對客戶端可見 不安全
session對客戶端是透明的 不存在敏感資訊的安全問題
4 依賴關係
session依賴名為JSESSIONID的cookie
5 儲存容量不同
cookie單個數據<=4kb 單個站點只能有20個cookie
sesion沒有限制
6 儲存的資料格式
cookie只能儲存asicc碼資訊+二進位制
session可以儲存任意資料