Session解析

  1.除非关闭所有页面 或者超时session才销毁

  2.在几个页面之间切换的时候 session保存用户状态。

  3.遍历数组时候for循环中从0开始小于长度,不等于长度,用Matlab用习惯了,竟然从1开始了。

<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="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 'MyJsp.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>
    This is my JSP page. <br>
    <% 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        //d是毫秒数
        Date d = new Date(session.getCreationTime());
        session.setAttribute("username", "admin");
        session.setAttribute("password", "root");
        //设置session生存期限
        session.setMaxInactiveInterval(10); //10s
    %>
    <!-- 是创建的系统日期 不是创建花费的时间 -->
    session创建时间为:<%=sdf.format(d) %>
    session的ID编号为:<%=session.getId() %>
    session的用户名:<%=session.getAttribute("username") %>
    
    <a href="session2.jsp" target="_blank">链接到session2</a>
  </body>
</html>

  超链接是为了证明虽然去了另一个页面,但是session的id还是一样的,是同一个session。

<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="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 'MyJsp.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>
    This is my JSP page. <br>
    <% 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        //d是毫秒数
        Date d = new Date(session.getCreationTime());
        session.setAttribute("username", "admin");
    %>
    <!-- 是创建的系统日期 不是创建花费的时间 -->
    session创建时间为:<%=sdf.format(d) %>
    session的ID编号为:<%=session.getId() %>
    session的用户名:<%=session.getAttribute("username") %>
    session中保存的属性有:<%
        String[] names = session.getValueNames();
        for(int i=0;i<names.length; i++) {
        out.println(names[i]+"  ");
        }
     %>
  </body>
</html>

  设置session的最大生存时间,先点击session1,停留10s后点击session2,看看session2页面中的id是否和1页面中的一样。

  最后后四位的ID不一样,而且属性值 只有username  没有password。

 

原文地址:https://www.cnblogs.com/hxsyl/p/5571215.html