세션(session)과 로그인

세션(session)과 로그인

/* 세션: 실행중인 프로그램
   = 저장 setAttribute(“key”,”key-value”)
   = 저장 가지고 오기 getAttribute(“key”)
   = 유지시간  setMaxactiveInterval(초): 30분이 디폴트
   = 해제  invalidate()
   = 일부해제  removeAttibute(“key”)
   **** 접속마다 session은 한개만 생성이 가능.
   접속자마다 sessionId 생성
*/

1. 로그인되었는지 아닌지 확인
String id=(String)session.getAttribute(“id”);
String log_jsp=””;
if(id==null){
 log_jsp=”login.jsp”;
}
else{
 log_jsp=”logout.jsp”;
}

2. 로그인 하기

<form method=post action=”login_ok.jsp” name=frm>
     <input type=text name=id value=”hong” size=15>

     <input type=password name=pwd value=”1234″ size=15>
     <input type=submit value=로그인>
</form>

3. 로그인 확인

String id=request.getParameter(“id”);
String pwd=request.getParameter(“pwd”);

     <%– 아이디와 비밀번호가 옳다면 아래를 실행–%>
session.setAttribute(“id”, id);
session.setAttribute(“name”, result);

4. 로그아웃

session.invalidate();//세션 해제: 아이디를 널값으로 바꿈
response.sendRedirect(“main.jsp”);