zoukankan      html  css  js  c++  java
  • 11月26日学习日志

    今天学习了jsp中Session的应用。

    这个例子描述了如何使用HttpSession对象来获取创建时间和最后一次访问时间。我们将会为request对象关联一个新的session对象,如果这个对象尚未存在的话。

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="java.io.*,java.util.*" %>
    <%
       // 获取session创建时间
       Date createTime = new Date(session.getCreationTime());
       // 获取最后访问页面的时间
       Date lastAccessTime = new Date(session.getLastAccessedTime());
    
       String title = "再次访问菜鸟教程实例";
       Integer visitCount = new Integer(0);
       String visitCountKey = new String("visitCount");
       String userIDKey = new String("userID");
       String userID = new String("ABCD");
    
       // 检测网页是否有新的访问用户
       if (session.isNew()){
          title = "访问菜鸟教程实例";
          session.setAttribute(userIDKey, userID);
          session.setAttribute(visitCountKey,  visitCount);
       } else {
           visitCount = (Integer)session.getAttribute(visitCountKey);
           visitCount += 1;
           userID = (String)session.getAttribute(userIDKey);
           session.setAttribute(visitCountKey,  visitCount);
       }
    %>
    <html>
    <head>
    <title>Session 跟踪</title>
    </head>
    <body>
    
    <h1>Session 跟踪</h1>
    
    <table border="1" align="center"> 
    <tr bgcolor="#949494">
       <th>Session 信息</th>
       <th></th>
    </tr> 
    <tr>
       <td>id</td>
       <td><% out.print( session.getId()); %></td>
    </tr> 
    <tr>
       <td>创建时间</td>
       <td><% out.print(createTime); %></td>
    </tr> 
    <tr>
       <td>最后访问时间</td>
       <td><% out.print(lastAccessTime); %></td>
    </tr> 
    <tr>
       <td>用户 ID</td>
       <td><% out.print(userID); %></td>
    </tr> 
    <tr>
       <td>访问次数</td>
       <td><% out.print(visitCount); %></td>
    </tr> 
    </table> 
    </body>
    </html>
  • 相关阅读:
    (转)NandFlash详述
    (转)Redhat Linux 硬盘挂载方法!!!
    为Linux虚拟机挂载SD卡!
    DECLARE_GLOBAL_DATA_PTR 作用
    NAND FLASH ECC校验原理与实现
    Ehcache学习笔记(三) 与Spring集成
    ExtJs ComponentQuery 组件选择器
    好记性不如烂博客之 Quartz HowTo: Update an existing job
    使用WeakReference 与 ReferenceQueue 简单实现弱引用缓存
    Ehcache学习笔记(四) Web Caching 页面级别缓存
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14160294.html
Copyright © 2011-2022 走看看