プログラミング
 Computer >> コンピューター >  >> プログラミング >> プログラミング

JSPページのセッションを使用してWebページのアクセス時間を追跡するにはどうすればよいですか?


この例では、HttpSessionオブジェクトを使用して、セッションの作成時刻と最後にアクセスされた時刻を確認する方法について説明します。新しいセッションがまだ存在しない場合は、リクエストに関連付けます。

<%@ page import = "java.io.*,java.util.*" %>
<%
   // Get session creation time.
   Date createTime = new Date(session.getCreationTime());

   // Get last access time of this Webpage.
   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");

   // Check if this is new comer on your Webpage.
   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>

次に、上記のコードを main.jspに配置します http:// localhost:8080 / main.jspにアクセスしてみてください 。 URLを実行すると、次の結果が表示されます-

私のウェブサイトへようこそ

セッション情報

セッション情報
id 0AE3EC93FF44E3C525B4351B77ABB2D5
作成時間 2010年6月8日火曜日17:26:40GMT + 04:00
最終アクセス時刻 2010年6月8日火曜日17:26:40GMT + 04:00
ユーザーID ABCD
訪問数 0

ここで、同じJSPをもう一度実行してみると、次の結果が得られます。

ようこそ私のウェブサイトに戻る

セッション情報

情報タイプ
id 0AE3EC93FF44E3C525B4351B77ABB2D5
作成時間 2010年6月8日火曜日17:26:40GMT + 04:00
最終アクセス時刻 2010年6月8日火曜日17:26:40GMT + 04:00
ユーザーID ABCD
訪問数 1

  1. JSPでタイムゾーンを使用するにはどうすればよいですか?

    タグは、本体内のすべてのタグが使用するタイムゾーンを指定するために使用されます。 属性 タグには次の属性があります- 属性 説明 必須 デフォルト 値 身体に適用するタイムゾーン はい なし 例 <%@ taglib uri = "https://java.sun.com/jsp/jstl/core" prefix = "c" %> <%@ taglib uri = "https://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %&g

  2. Pythonを使用してファイルの最終アクセス時間を確認するにはどうすればよいですか?

    ファイルの変更時刻を取得するには、os.path.getmtime(path)を使用できます。クロスプラットフォームでサポートされています。 例 >>> import os >>> print os.path.getmtime('my_file.txt') 1505928275.3081832