Web階段:第十七章:Session會(huì)話

什么是Session會(huì)話?
1.Session是會(huì)話,表示客戶端和服務(wù)器之間聯(lián)系的一個(gè)對(duì)象。
2.Session是一個(gè)域?qū)ο蟆?br> 3.Session經(jīng)常用來(lái)保存用戶的數(shù)據(jù)。

如何創(chuàng)建Session和獲取(id號(hào),是否為新)
調(diào)用一個(gè)方法request.getSession().
第一次調(diào)用是創(chuàng)建Session對(duì)象并返回
之后調(diào)用都是獲取Session對(duì)象。

isNew() 返回當(dāng)前Session是否是剛創(chuàng)建出來(lái)的,返回true表示剛創(chuàng)建。返回false表示獲取。

每個(gè)Session會(huì)話都有自己的唯一標(biāo)識(shí),就是ID。

Session域數(shù)據(jù)的存取
setAttribute 保存數(shù)據(jù)
getAttribute 獲取數(shù)據(jù)

protected void setAttribute(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    String username = request.getParameter("username");
    HttpSession session = request.getSession();
    session.setAttribute("key1", username);
    response.getWriter()
    .write("已經(jīng)把你請(qǐng)求過(guò)來(lái)的參數(shù)【" + username + "】給保存到Session域中");
}

protected void getAttribute(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    Object attribute = request.getSession().getAttribute("key1");
    response.getWriter().write("你剛剛保存的數(shù)據(jù)是:" + attribute);
}

Session生命周期控制
在Tomcat服務(wù)器上,Session的默認(rèn)存活時(shí)間是:30分鐘。因?yàn)樵赥omcat的配置文件web.xml中早以有如下的配置:
以下的配置決定了在此tomcat服務(wù)器上所有的web工程,創(chuàng)建出來(lái)的所有Session對(duì)象。默認(rèn)超時(shí)時(shí)間都是30分鐘。

 

  1. <!-- ==================== Default Session Configuration ================= -->
  2. <!-- You can set the default session timeout (in minutes) for all newly -->
  3. <!-- created sessions by modifying the value below. -->
  4. <session-config>
  5. <session-timeout>30</session-timeout>
  6. </session-config>

我們也可以給自己的web工程,單獨(dú)配置超時(shí)時(shí)間。只需要在自己的web工程中在web.xml配置文件里做相同的配置即可。
以下配置,把自己的web工程所有的Session都配置超時(shí)時(shí)間為20分鐘了。

  1. <!-- 表示當(dāng)前的web工程,所有創(chuàng)建出來(lái)的Session默認(rèn)超時(shí)時(shí)間為20分鐘 -->
  2. <session-config>
  3. <session-timeout>20</session-timeout>
  4. </session-config>

以上配置文件的配置的方式都是對(duì)Tomcat服務(wù)器,或?qū)eb工程創(chuàng)建出來(lái)的所有Session集體生效。
如果想對(duì)某個(gè)單個(gè)的Session進(jìn)行設(shè)置,可以使用api進(jìn)行單獨(dú)設(shè)置

setMaxInactiveInterval( time ); 這里以秒為單位
如果值是正數(shù),表示指定的秒數(shù)后Session就會(huì)超時(shí)(Session超時(shí)就會(huì)被銷毀)
如果值是負(fù)數(shù),表示Session永遠(yuǎn)不超時(shí)(極少使用)

Session的超時(shí),是指客戶端和服務(wù)器之間兩次請(qǐng)求的間隔時(shí)間。如果中間沒(méi)有任何的請(qǐng)求,就會(huì)把Session超時(shí)掉。

invalidate():使當(dāng)前會(huì)話,馬上被超時(shí)

protected void deleteNow(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession();    
    session.invalidate();// 結(jié)束當(dāng)前Session    
    response.getWriter().write("當(dāng)前會(huì)話已經(jīng)超時(shí)了");
}

protected void life3(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    HttpSession httpSession = request.getSession();
    httpSession.setMaxInactiveInterval(3);// 表示這個(gè)Session3秒之后就會(huì)超時(shí)。
    response.getWriter().write("已經(jīng)設(shè)置了當(dāng)前Session超時(shí)時(shí)間為3秒" );
}

protected void defaultLife(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession();
    // 獲取客戶端和服務(wù)器之間最大的請(qǐng)求間隔時(shí)間。
    // 就是超時(shí)時(shí)間。位置 為秒
    int maxInactiveInterval = session.getMaxInactiveInterval();
    response.getWriter().write("默認(rèn)的超時(shí)時(shí)間為:" + maxInactiveInterval);
}

瀏覽器和Session之間關(guān)聯(lián)的技術(shù)內(nèi)幕