session的生命周期

一 创建


 
 
二 活动


 
 
三 销毁


 
四 实例
1、session_page1.jsp
<%@   page   language = "java"   import = "java.util.*,java.text.*"   contentType = "text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<! DOCTYPE   HTML   PUBLIC   "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    < head >
      < base   href = " <%= basePath %> " >
   
      < title > My JSP 'index.jsp' starting page </ title >
         < meta   http-equiv = "pragma"   content = "no-cache" >
         < meta   http-equiv = "cache-control"   content = "no-cache" >
         < meta   http-equiv = "expires"   content = "0" >    
         < meta   http-equiv = "keywords"   content = "keyword1,keyword2,keyword3" >
         < meta   http-equiv = "description"   content = "This is my page" >
        <!--
        <link   rel =" stylesheet " type="text/ css "   href ="styles.css">
        -->
    </ head >
 
    < body >
      < h1 > session内置对象 </ h1 >
      < hr >
      <%
      SimpleDateFormat sdf =   new   SimpleDateFormat( "yyyy年MM月dd日 HH:mm:ss" );
      Date d =   new   Date(session.getCreationTime());
      session.setAttribute( "username" ,   "admin" );
      session.setAttribute( "password" ,   "123456" );
      session.setAttribute("age", 20);
     
        //设置当前session最大生成期限单位是秒
        //session.setMaxInactiveInterval(10);//10秒钟
        //session.invalidate();//销毁当前会话。
      %>
    
    Session创建时间:<%=sdf.format(d)%><br>   
    Session的ID编号:<%=session.getId()%><BR>
         从Session中获取用户名: <%= session.getAttribute( "username" )   %> < br >
      <%
         //session.invalidate();//销毁当前会话
      %>
      < a   href = "session_page2.jsp"   target = "_blank" > 跳转到Session_page2. jsp </ a >     
       
    </ body >
</html>
2、session_page2.jsp
<%@   page   language = "java"   import = "java.util.*,java.text.*"   contentType = "text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<! DOCTYPE   HTML   PUBLIC   "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    < head >
      < base   href = " <%= basePath %> " >
   
      < title > My JSP 'index.jsp' starting page </ title >
         < meta   http-equiv = "pragma"   content = "no-cache" >
         < meta   http-equiv = "cache-control"   content = "no-cache" >
         < meta   http-equiv = "expires"   content = "0" >    
         < meta   http-equiv = "keywords"   content = "keyword1,keyword2,keyword3" >
         < meta   http-equiv = "description"   content = "This is my page" >
        <!--
        <link   rel =" stylesheet " type="text/ css "   href ="styles.css">
        -->
    </ head >
 
    < body >
      < h1 > session内置对象 </ h1 >
      < hr >
      <%
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        //Date d = new Date(session.getCreationTime());
        //session.setAttribute("username", "admin");
      %>
       
    Session的ID编号:<%=session.getId()%><BR>
         从Session中获取用户名: <%= session.getAttribute( "username" )   %> < br >
    Session中保存的属性有: <%
                     String[] names =session.getValueNames();
                       for ( int   i=0;i<names.length;i++)
                     {
                        out.println(names[i]+"&nbsp;&nbsp;");
                     }
   
      %>   < br >    
    </ body >
</html>
 
五 测试某次会话中通过超链接打开的新页面属于同一次会话


 


 
 
六 测试只要当前会话页面没有全部关闭,重新打开新的浏览器窗口访问同一项目资源时,属于同一次会话


 
 
七 测试通过调用session.invalidate()方法对session进行销毁
在代码中调用session.invalidate();//销毁当前会话
 
八 Tomcat超时时间设置方法


 
<? xml   version = "1.0"   encoding = "UTF-8" ?>
< web-app   version = "2.5"
         xmlns = " http://java.sun.com/xml/ns/javaee" ;
         xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance" ;
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    < display-name ></ display-name >     
    < welcome-file-list >
      < welcome-file > index.jsp </ welcome-file >
    </ welcome-file-list >
    <!-- 设置会话一分钟后过期 -->
    < session-config >
      < session-timeout > 1 </ session-timeout >
    </ session-config >
</web-app>
 
九 小知识点
1、Tomcat后台管理方法


 
  • 大小: 69.4 KB
  • 大小: 127.3 KB
  • 大小: 48 KB
  • 大小: 117.2 KB
  • 大小: 40.8 KB
  • 大小: 37.1 KB
  • 大小: 87.8 KB
  • 大小: 105.5 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值