前面的學習中咱們知道HTTP協議是一種無狀態協議。有時候咱們須要在多個頁面之間共享html
數據,這在web應用中如何實現呢?Session能夠幫助咱們作到!java
用戶訪問某個網站時,web服務器就會在服務器的內存中爲該瀏覽器分配一個空間,這個web
空間是被瀏覽器獨佔的。該空間就稱其爲Session空間,用戶經過瀏覽器訪問服務器,再到apache
瀏覽器退出訪問這段時間叫作session會話,這個會話時間一般爲30min(能夠手動修改)。
瀏覽器
理解Session最好從Session的做用入手,通常來講Session的做用以下:tomcat
一、保存用戶登陸信息。安全
二、同一客戶端瀏覽器各個頁面共享數據服務器
三、驗證用戶是否非法登陸某個頁面cookie
對於Session的理解咱們應該從瀏覽器對web服務器的一次會話中去理解。session
session數據的存在時間默認爲30min,當你瀏覽某個網站,中途出去吃飯
瀏覽器並未關閉。超過30min以後session數據會被清空。當你重新瀏覽頁面
時,服務器會查詢看session空間裏面的內容若爲空,則需用戶從新登陸。
這也是一種較爲安全的作法。
與Cookie不一樣的是Session是存在與服務器的,是有Tomcat服務器維護的,
而session存在與客戶端瀏覽器,有瀏覽器維護。基於這點Session相對於Cookie
來講更加的安全。
一、服務器爲客戶端建立並維護一個Session對象,用於存放數據。
二、在建立Session對象的同時,服務器將會爲該Session對象生成一個惟一的
sessionID
三、服務器以cookie的方式將sessionID存放在客戶端。
四、當瀏覽器再次訪問該服務器時,會將sessionID做爲cookie信息帶到服務器
服務器能夠經過sessionID檢索到之前的session對象,並對其進行訪問。
瞭解了session的概念和工做原理以後咱們來看看Session的源碼,經過其源碼的
閱讀了解下session有哪些操做。具體方法筆者也不過多的去闡述了,給個方法一覽
吧。
這裏咱們常常使用的是setAttribute和getAttribute方法,一樣setMaxInactiveInterval
方法設置Seesion會話有效的時間。以秒爲單位。
那麼HttpSession又是如何獲得的呢?HttpSession是從請求中的獲得的,具體的作法
就是。
HttpSession session = request.getSession();//返回當前request的session,若是沒有則建立一個兩外能夠參考:
/** * * Returns the current <code>HttpSession</code> * associated with this request or, if there is no * current session and <code>create</code> is true, returns * a new session. * * <p>If <code>create</code> is <code>false</code> * and the request has no valid <code>HttpSession</code>, * this method returns <code>null</code>. * * <p>To make sure the session is properly maintained, * you must call this method before * the response is committed. If the container is using cookies * to maintain session integrity and is asked to create a new session * when the response is committed, an IllegalStateException is thrown. * * * * * @param create <code>true</code> to create * a new session for this request if necessary; * <code>false</code> to return <code>null</code> * if there's no current session * * * @return the <code>HttpSession</code> associated * with this request or <code>null</code> if * <code>create</code> is <code>false</code> * and the request has no valid session * * @see #getSession() * * */ public HttpSession getSession(boolean create);
HttpServletRequest.getSession(ture) 等同於 HttpServletRequest.getSession()
HttpServletRequest.getSession(false) 等同於 若是當前Session沒有就爲null;
有這麼一種要求當用戶登陸一個網站,中途有事,但並未關閉瀏覽器,等到用戶回來的
時候若是Session會話時間結束處於安全性的考慮須要用戶從新登陸。這裏咱們作一個簡單
的實踐。
咱們如今Servlet中作簡單的登陸驗證,驗證經過以後就將用戶信息保存在Session中,以後
再在各個jsp頁面中添加驗證代碼,驗證不經過則返回登陸界面。
Servlet邏輯部分代碼以下:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(false);//獲得HttpSession對象 String email = request.getParameter("email"); String password = request.getParameter("pass"); User user = new User(); user.setEmail(email); user.setPassword(password); if(email.equals("1406754158@qq.com")&&password.equals("kiritor")) { session.setAttribute("user", user); session.setMaxInactiveInterval(20);//設定時間爲20秒 System.out.println(session.getAttribute("user")); response.sendRedirect("index.jsp"); } }以後咱們在index界面簡單的添加驗證代碼。
<%@page import="com.Dao.User"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% HttpSession httpSession = request.getSession(); User user = (User)httpSession.getAttribute("user"); out.print(user.getEmail()); %> 歡迎來到首頁 </body> </html>看看實際的效果吧。
登陸驗證成功以後咱們進入index界面,以後咱們耐心的等待20秒鐘,在刷新頁面
能夠發現的是從新跳轉到登陸界面了。
上述的代碼中咱們修改了Session存在時間,那麼有其餘的方式修改其存在時間嗎
答案是確定的,並且不止一種,如今筆者就簡要的對其進行下總結。
一、 在tomcat——>conf——>servler.xml文件中定義:
<Context path="/test" docBase="/test" defaultSessionTimeOut="3600" isWARExpanded="true" isWARValidated="false" isInvokerEnabled="true" isWorkDirPersistent="false"/>二、 在web.xml中定義:這個針對具體項目
<session-config> <session-timeout>20</session-timeout> </session-config>三、程序中定義,這個就很少提了,上面有實現。
4.Tomcat在conf/context.xml文件設置:這個是針對全部的項目了
<Manager className="org.apache.catalina.session.PersistentM anager" > debug=0 saveOnRestart="true" maxActiveSession="-1" minIdleSwap="-1" maxIdleSwap="-1" maxIdleBackup="-1" <Store className="org.apache.catalina.session.FileStore" directory="../session" /> //這裏表明的是文件持久化.也能夠本身實現Store </Manager>好了,今天的總結就到這裏了。
By Kiritor
2012/06/13