session等作用域
作用域分為四種,其中常用的只有兩種:
page:頁面級作用域,基本用不到
request:請求級作用域,當伺服器產生響應時,請求作用域銷燬
sessoin:會話級作用域,保證在多次互動過程中,回話作用域始終存在
application:應用級作用域,只要伺服器不關閉,一直存在(在Spring框架中會用到這個)
作用域能用小的,儘量用小的,不用大的,可以節約伺服器的記憶體
000000000000分隔符00000000000
這裡是session作用域使用主要用到的程式碼:
儲存session:
//session 作用域中儲存hehe:lname HttpSession session = request.getSession(); session.setAttribute("hehe",lname);
在網頁中呼叫session儲存的 資料:
網頁上面寫java程式碼,得到儲存的資料:
<%
String lname1 = (String)request.getAttribute("lname");
String lname2 = (String)session.getAttribute("hehe");
%>
使用<%=lname2 %>得到儲存的資料內容:
<body> <div> 來自request作用域的: <%=lname1 %><br> 來自session作用域的:<%=lname2 %><br> <a href="test1.jsp">test1</a> </div> </body>
000000000000分隔符00000000000
今天整理的是session作用域(重點):
原理:在伺服器上找一塊記憶體,實際上是一個鍵值對,前面是 String name,後面是Object value,每一個客戶端都有一個session,每個客戶端之間,當伺服器為客戶端下發一個臨時的cookie(瀏覽器的記憶體中),名字叫JSESSIONID,是一組字母加數字的序列,表示訪問伺服器session記憶體的“憑證”,客戶端通過JSESSIONID這個憑證訪問自己對應的伺服器儲存session的記憶體
session失效的情況:
1、客戶端關閉瀏覽器程式,造成了JSESSIONID憑證丟失
2、時效性,session超時(會話超時):a、客戶端關閉後,一段時間內沒有訪問過該session記憶體,伺服器會清理。b、或者一段時間內沒有訪問過該session記憶體,伺服器會清理。c、回話超時的時間,預設是30分鐘,只要發起請求,會話時間從0重新計算
session工作原理圖
000000000000分隔符00000000000
獲取session物件:
HttpSession session = request,getSession();
存session:
session.setAttribute(“name”,value);
取session:
Object value = session.getAttribute(“name”);
網頁程式碼如下:
index:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
請登入:<a href="login.jsp">登入</a>
登出:<a href="/20181130/LogoffServlet">登出</a>
<hr>
<a href="test1,jsp">test1</a>
</body>
</html>
LoginServlet:
package com.neuedu.servlet;
import java.io.IOException;
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 javax.servlet.http.HttpSession;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("tuf-8");
String lname = request.getParameter("lname");
String lpass = request.getParameter("lpass");
request.setAttribute("lname", lname);
if(lname.equals("admin")&&lpass.equals("123"))
{
//獲得rem是否勾選
String rem = request.getParameter("rem");
System.out.println(rem);
if(rem != null)
{
//如果rem不等於null,說面選中了記住,儲存cookie
Cookie c1 = new Cookie("uname", lname);
Cookie c2 = new Cookie("upass", lpass);
response.addCookie(c1);//向客戶端儲存cookie
response.addCookie(c2);//向客戶端儲存cookie
c1.setMaxAge(9999);
}
request.setAttribute("haha", lname);
//session 作用域中儲存hehe:lname
HttpSession session = request.getSession();
session.setAttribute("hehe",lname);
//進行跳轉
request.getRequestDispatcher("/welcome.jsp").forward(request, response);
}
else{
//請求重定向
response.sendRedirect("/login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
welcome:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String lname1 = (String)request.getAttribute("lname");
String lname2 = (String)session.getAttribute("hehe");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
來自request作用域的: <%=lname1 %><br>
來自session作用域的:<%=lname2 %><br>
<a href="test1.jsp">test1</a>
</div>
</body>
</html>
test1:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String lname1 = (String)request.getAttribute("lname");
String lname2 = (String)session.getAttribute("hehe");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="test2.jsp">test2</a><br>
<div>
來自request作用域的: <%=lname1 %><br>
來自session作用域的:<%=lname2 %><br>
</div>
</body>
</html>
test2:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String lname1 = (String)request.getAttribute("lname");
String lname2 = (String)session.getAttribute("hehe");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
來自request作用域的: <%=lname1 %><br>
來自session作用域的:<%=lname2 %><br>
</div>
</body>
</html>