【Servlet】【JSP】Servlet + JSP でログアウト(ログオフ)機能を実装する

サンプル

View class

LogonView.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "">http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Login</title>
</head>
<body>
<table border="0">
<form action="<%=request.getContextPath() %>/LogonPresenter" method="post">
<tr><th>User Name:</th><td><input type="text" name="userName"></td></tr>
<tr><th>Password:</th><td><input type="password" name="password"></td></tr>
<tr><th></th><td><input type="submit" value="Logon"></td></tr>
</form>
</table>
</body>
</html>

/Views/Logon/WelcomeView.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.sample.session.LogonPresenter" %>
<%
   response.setHeader("Pragma", "no-cache");
   response.setHeader("Cache-Control", "no-cache");
   response.addHeader("Cache-Control", "no-store");
   response.setDateHeader("Expires", -1);

   String uname = null;
   try {
      uname = (String) session
            .getAttribute(LogonnPresenter.SESSION_KEY_FOR_LOGIN_USER);

   } catch (Exception e) {
   }
   if (uname == null) {
      response.sendRedirect("../LogonView.jsp");
   }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>Welcome!!</h1>
	<a href="<%=request.getContextPath()%>/Views/Logon/Site01.jsp">Go
		Site01</a>
	<a href="<%=request.getContextPath()%>/Views/Logon/Site02.jsp">Go
		Site02</a>
	<a href="<%=request.getContextPath()%>/Views/Logon/Site03.jsp">Go
		Site03</a>
	<a href="<%=request.getContextPath() %>/LogoffPresenter">Logoff</a>
</body>
</html>

/Views/Logon/Site01.jsp (Site02.jsg/Site03.jsg)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="com.sample.session.LogonPresenter" %>
<%
   response.setHeader("Pragma", "no-cache");
   response.setHeader("Cache-Control", "no-cache");
   response.addHeader("Cache-Control", "no-store");
   response.setDateHeader("Expires", -1);

   String uname = null;
   try {
      uname = (String) session
            .getAttribute(LogonPresenter.SESSION_KEY_FOR_LOGIN_USER);

   } catch (Exception e) {
   }
   if (uname == null) {
      response.sendRedirect("../LogonView.jsp");
   }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hi</h1>
<a href="<%= request.getContextPath() %>/Views/Logon/WelcomeView.jsp">Return Back</a>
</body>
</html>

Presenter class

LogonPresenter.java

package com.sample.session;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginPresenter
 */
public class LogonPresenter extends HttpServlet {
   public static final String SESSION_KEY_FOR_LOGIN_USER = "User";
   private static final long serialVersionUID = 1L;

   /**
    * @see HttpServlet#HttpServlet()
    */
   public LogonPresenter() {
      super();
      // TODO Auto-generated constructor stub
   }

   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      // TODO Auto-generated method stub
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      String userName = request.getParameter("userName");
      String password = request.getParameter("password");

      // セッション取得
      HttpSession session = request.getSession(true);
      // 既存セッション破棄
      session.invalidate();
      // 新規セッションを開始
      HttpSession newSession = request.getSession(true);

      if ("admin".equals(userName) && "admin".equals(password)) {
         newSession.setAttribute(LogonPresenter.SESSION_KEY_FOR_LOGIN_USER,
               userName);
         response.sendRedirect(request.getContextPath()
               + "/Views/Logon/WelcomeView.jsp");
      } else {
         newSession.setAttribute(LogonPresenter.SESSION_KEY_FOR_LOGIN_USER,
               null);
         newSession.removeAttribute(LogonPresenter.SESSION_KEY_FOR_LOGIN_USER);
         response.sendRedirect(request.getContextPath()
               + "/Views/LogonView.jsp");
      }
   }
}

LogoffPresenter.java

package com.sample.session;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LogoutPresenter
 */
public class LogoffPresenter extends HttpServlet {
   private static final long serialVersionUID = 1L;

   /**
    * @see HttpServlet#HttpServlet()
    */
   public LogoffPresenter() {
      super();
   }

   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      HttpSession session = request.getSession(false);
      session.setAttribute(LogonPresenter.SESSION_KEY_FOR_LOGIN_USER, null);
      session.removeAttribute(LogonPresenter.SESSION_KEY_FOR_LOGIN_USER);
      request.getSession().invalidate();

      response.sendRedirect(request.getContextPath() + "/Views/LogonView.jsp");
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      this.doGet(request, response);
   }
}

参考文献

http://www.orquesta.org/takegata/Article/ArticleView.jsp?article_id=630
http://www.coderanch.com/t/485192/JSP/java/JSP-login-logout-session

関連記事

ページ遷移およびパラメータの受け取り ~クエリ文字列編~

http://blogs.yahoo.co.jp/dk521123/32776759.html

ページ遷移およびパラメータの受け取り ~セッション編~

http://blogs.yahoo.co.jp/dk521123/33118263.html

Servlet/JSP/useBeanタグ を使って、MVCモデル

http://blogs.yahoo.co.jp/dk521123/33118025.html

Servlet + JSP でログイン機能を実装する

http://blogs.yahoo.co.jp/dk521123/33567439.html

Servlet + JSP でログアウト機能を実装する

http://blogs.yahoo.co.jp/dk521123/33704010.html