참 고맙지.
HttpSession 객체를 이용한 세션관리
 
Login.html
 
<html>
<body>
<form action="session.jsp" method="post">
ID <input type="text" name="id"><br>
Pass <input type="password" name="passwd"><p>
<input type="submit" value="전송">
</form>
</body>
</html>
 
 
session.jsp
 
<%@ page contentType="text/html;charset=euc-kr" session="false"%>
<%         request.setCharacterEncoding("euc-kr"); %>
<%
             HttpSession session = request.getSession(true);
             //세션을 생성
             String name = request.getParameter("name");
             String id = request.getParameter("id");
             String passwd = request.getParameter("passwd");
            
             //세션저장 (플래그, 값)
             session.setAttribute("login.name", name);
             session.setAttribute("login.id", id);;
             session.setAttribute("login.passwd", passwd);
             session.setAttribute("login.time", new Long(System.currentTimeMillis()));
%>
 
<html>
<body>
<center>
<a href="showinfo.jsp">로그인 정보보기</a>
</body>
</html>
 
showinfo.jsp
 
<%@ page contentType="text/html;charset=euc-kr" session="false"%>
<%
             HttpSession session = request.getSession(false);
             if(session == null){ //세션이 null을 지니면 페이지전화
                           response.sendRedirect("login.html");
                           return;
             }
// 세션 플래그에 해당하는 값 반환
             String name = (String)session.getAttribute("login.name");
             String passwd = (String)session.getAttribute("login.passwd");
             String id = (String)session.getAttribute("login.id");
             Long start = (Long)session.getAttribute("login.time");
             // currentTimeMillis() 현재시간 반환
             long current = System.currentTimeMillis();
             long durantion = current - start.longValue();
             long durantion_min = (durantion/1000)/60;
             long durantion_sec = (durantion/1000)%60;
             String session_durantion =
                                                  ""+durantion_min+"분 "+durantion_sec+"초";
%>
 
로그인정보<hr>
이름: <%= name%><br>
아이디: <%= id%><br>
패스워드: <%= passwd%><br>
세션유지시간: <%= session_durantion%>
<form action="logout.jsp">
<input type="submit" value="로그아웃">
</form>
 
Logout.html
 
<%@ page contentType="text/html;charset=euc-kr" session="false"%>
<%
             HttpSession session = request.getSession(false);
             String name ="";
             if(session != null){
                           name = (String)session.getAttribute("login.name");
                           session.invalidate();  //세션삭제
             }
%>
<%= name%> 정상 로그아웃 되었습니다.<br>
<a href="showinfo.jsp">로긴 정보보기</a>
 
<!--로그아웃 된상태에서 showinfo 를 보려한다면
      showinfo 가 아닌 login.html 이 나올것이다.
      그 이유는 logout 페이지에서 세션을 삭제하기 때문인데
      showinfo 페이지는 널값을 지닌 세션을 받으면 sendRedirecet 를
      이용하여 페이지를 전환시킨다. -->


출처 : http://blog.empas.com/zeroscience/17230751
ref: http://darky.egloos.com/1182608

+ Recent posts