<% Date createTime = new Date(session.getCreationTime()); Date lastAccessTime = new Date(session.getLastAccessedTime()); " />

亚洲一区精品自拍_2021年国内精品久久_男同十八禁gv在线观看_免费观看a级性爱黄片

當前位置:文章中心>技術教程
公告通知 新聞快遞 技術教程 產(chǎn)品展示

JSP的session處理

發(fā)布時間:2021-12-06 點擊數(shù):737

利用JSP內(nèi)置的session對象的isNew方法判斷當前session是否是第一次創(chuàng)建的。使用session.setAttribute來設置屬性。

<%@ page import="java.io.*,java.util.*" %> <%  Date createTime = new Date(session.getCreationTime()); Date lastAccessTime = new Date(session.getLastAccessedTime());  String title = "Welcome Back to my website";  Integer visitCount = new Integer(0); String visitCountKey = new String("visitCount"); String userIDKey = new String("userID"); String userID = new String("ABCD"); if (session.isNew()){  title = "Welcome to my website";  session.setAttribute(userIDKey, userID);  session.setAttribute(visitCountKey,  visitCount); }  visitCount = (Integer)session.getAttribute(visitCountKey); visitCount = visitCount + 1; userID = (String)session.getAttribute(userIDKey); session.setAttribute(visitCountKey,  visitCount); %>  <html> <head> <title>Session Tracking</title> </head> <body> <center> <h1>Session Tracking</h1> </center> <table border="1" align="center"> <tr bgcolor="#949494">  <th>Session info</th>  <th>Value</th> </tr> <tr>  <td>id</td>  <td><% out.print( session.getId()); %></td> </tr> <tr>  <td>Creation Time</td>  <td><% out.print(createTime); %></td> </tr> <tr>  <td>Time of Last Access</td>  <td><% out.print(lastAccessTime); %></td> </tr> <tr>  <td>User ID</td>  <td><% out.print(userID); %></td> </tr> <tr>  <td>Number of visits</td>  <td><% out.print(visitCount); %></td> </tr> </table> </body> </html> 

第一次打開該JSP, 看到如下頁面:image.png刷新頁面,觀察到Number of visits的計數(shù)器刷新,并且Time of Last Access的值為最后刷新時間:image.png