セッション作成日時と最終アクセス日時

広告

ここではセッションが作成された日時を確認してみます。

セッションの作成日時を取得するには「HttpSession」インターフェースで定義されている"getCreationTime"メソッドを使います。

Returns the time when this session was created, measured in 
milliseconds since midnight January 1, 1970 GMT. 

Returns:
  a long specifying when this session was created, expressed in 
    milliseconds since 1/1/1970 GMT 
Throws: 
  java.lang.IllegalStateException - if this method is called on an 
    invalidated session

取得した値はlong型の値となっており、1/1/1970 GMT からの経過秒数で表されます。よって例えば下記のように使います。

public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws IOException, ServletException{

  response.setContentType("text/html; charset=Shift_JIS");
  PrintWriter out = response.getWriter();

  HttpSession session = request.getSession(true);

  long createTime = session.getCreationTime();
  Date createDate = new Date(createTime);
}

またクライアントがセッション開始以降に最後にアクセスしたきた日時も取得できます。「HttpSession」インターフェースで定義されている"getLastAccessedTime"メソッドを使います。

Returns the last time the client sent a request associated with this 
session, as the number of milliseconds since midnight January 1, 
1970 GMT, and marked by the time the container received the request. 

Actions that your application takes, such as getting or setting a 
value associated with the session, do not affect the access time. 

Returns:
  a long representing the last time the client sent a request 
    associated with this session, expressed in milliseconds since
    1/1/1970 GMT 
Throws: 
  java.lang.IllegalStateException - if this method is called on an 
    invalidated session

"getLastAccessedTime"メソッドも戻り値ははlong型の値となっており、1/1/1970 GMT からの経過秒数で表されます。

サンプルプログラム

では簡単に試してみます。

web.xmlファイルは下記のようにしました。

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

  <servlet>
    <servlet-name>sessiontest</servlet-name>
    <servlet-class>SessionTest4</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>sessiontest</servlet-name>
    <url-pattern>/sessiontest</url-pattern>
  </servlet-mapping>
</web-app>

プログラムは下記の通りです。

SessionTest4.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;

public class SessionTest4 extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException{

    response.setContentType("text/html; charset=Shift_JIS");
    PrintWriter out = response.getWriter();

    HttpSession session = request.getSession(false);

    out.println("<html>");
    out.println("<head>");
    out.println("<title>セッションテスト</title>");
    out.println("</head>");
    out.println("<body>");

    if (session == null){
      out.println("<p>セッションを開始します</p>");
      session = request.getSession(true);

      long createTime = session.getCreationTime();
      Date createDate = new Date(createTime);

      out.println("<p>");
      out.println("セッション開始時間:" + createDate + "<br>");
      out.println("</p>");
    }else{
      out.println("<p>セッションは開始されています</p>");

      long createTime = session.getCreationTime();
      Date createDate = new Date(createTime);

      long accessTime = session.getLastAccessedTime();
      Date accessDate = new Date(accessTime);

      out.println("<p>");
      out.println("セッション開始時間:" + createDate + "<br>");
      out.println("最終アクセス時間  :" + accessDate);
      out.println("</p>");
    }

    out.println("<a href=\"/session/sessiontest\">再表示</a>");

    out.println("</body>");
    out.println("</html>");
  }
}

上記をコンパイル後に「d:\servlet-sample\session\WEB-INF\classes\」ディレクトリにクラスファイルを移動した後で、ブラウザで「http://localhost:8080/session/sessiontest」へアクセスしてみます。

セッション作成日時

「再表示」をクリックして下さい。

セッション作成日時

最終アクセス日時は、最初にセッションを作成した時の日時ですので、セッション作成日時と同じ日時が表示されます。再度「再表示」をクリックして下さい。

セッション作成日時

1つ前にアクセスがあった時の日時が最終アクセス日時に表示されます。

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。