FINAL PROJECT

로그인하면 -> 로그아웃 버튼으로 만들기

asso 2022. 8. 4. 03:49

먼저 내 코드

 

login_ok.jsp >>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
	int ucode = (Integer)request.getAttribute("ucode");
	String id= (String)request.getAttribute("id");
	out.println( "<script type='text/javascript'>" );
	if( ucode != -1 ) {
		//세션 등록

		System.out.println( "id: " + id );
		
		session.setAttribute( "idKey", id );
		
		session.setMaxInactiveInterval(5);
		
		out.println( "location.href='home.do';" );
	} else {
		out.println( "alert( '로그인 실패: 아이디나 비밀번호를 확인해주세요.' );" );
		out.println( "history.back();" );
	}
	out.println( "</script>" );
%>

 

home.jsp >>

id를 가져와서 session 값에 넣기!

 

<!-- 로그인/로그아웃 버튼 -->
         	<form class="d-flex">
         		<%
         			if( id != null ) {
         				%>
         				<strong><%=id %></strong>&nbsp;&nbsp;<button class="btn btn-outline-primary btn-sm" type="button"><a href="./logout_ok.do">LOGOUT</a></button>
         				<%
         			} else if( id == null ){
         				%>
         				<button class="btn btn-outline-primary btn-sm" type="button"><a href="./login.do">LOGIN</a></button>
         				<%
         			}
         		%>
             </form>

로그인을 해서 id 값을 가져오면 '아이디'+님 이 표시되고, 로그인 버튼이 로그아웃 버튼으로 바뀐다!!! 

 

 

logout_ok.jsp >>

저기서 logout 버튼 클릭 하면 여기로 넘어와서

세션을 삭제시킨 후 로그아웃 성공 완료!!! 흑흑

 

아직 못한 건

세션이 만료되면, 그 알림창이 자동으로 뜨는 것..을 못했음.. (회의해보니 안해도된다고함)

그냥 세션만료되면 자동으로 logout 되고, login 으로 버튼이 바뀐다 흑흑

 

 


결과 >>

 

 

 

 

 

 

 

* 참고한 블로그 >>

 

JSP | Session :: Pathas' Path as Web Developer (tistory.com)

 

JSP | Session

Session 클라이언트에 세션 ID를 부여해서 클라이언트를 구분하는 방법 Session 개요 클라이언트와 서버 간 상태를 유지하는 방법 중 하나 클라이언트가 처음 접속했을 때 세션 ID를 부여하고 서버에

pathas.tistory.com

 

[JSP] 세션(Session) 로그인/로그아웃 처리 (velog.io)

 

[JSP] 세션(Session) 로그인/로그아웃 처리

세션(Session) 이란? 사용자로부터 넘어온 정보를 서버측에 저장하는 개념 웹 브라우저당 한 개의 세션이 웹 컨테이너에 저장된다. 웹 서버의 서비스를 받는 사용자를 구분할 수 있는 단위. 세션을

velog.io