「Java」invalidateメソッドでセッション(session)を無効にするサンプル

環境
Java 1.8
Eclipse 4.14.0
構文

1.void invalidate()
このセッションを無効にし、それにバインドされているオブジェクトのバインドを解除します。
2.HttpSession getSession()
このリクエストに関連付けられている現在のセッションを返すか、リクエストにセッションがない場合は作成します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
 public void doGet( HttpServletRequest request, HttpServletResponse response )
           throws ServletException,IOException {
// セッションにユーザーを格納
HttpSession session = request.getSession();
User user =(User)session.getAttribute("USER_SESSION_KEY");
// セッションの破棄
session.invalidate();
//セッションの再作成
session = request.getSession(true);
処理コード
}
 public void doGet( HttpServletRequest request, HttpServletResponse response )            throws ServletException,IOException { // セッションにユーザーを格納 HttpSession session = request.getSession(); User user =(User)session.getAttribute("USER_SESSION_KEY"); // セッションの破棄 session.invalidate(); //セッションの再作成 session = request.getSession(true); 処理コード }
 public void doGet( HttpServletRequest request, HttpServletResponse response )
           throws ServletException,IOException {

   // セッションにユーザーを格納
    HttpSession session = request.getSession();
      User user =(User)session.getAttribute("USER_SESSION_KEY");
      
      // セッションの破棄
      session.invalidate();
      //セッションの再作成
      session = request.getSession(true);
      
      処理コード
      
}

説明
invalidateメソッドは、セッションを無効にし、結びつけられている全てのオブジェクトを解放します。

Java

Posted by arkgame