JSP
로그인하면 로그아웃으로 변경 JSP
뚜벅뚜벅뚜벅이
2022. 7. 12. 19:41
반응형
사람들이 이 키워드로 블로그에 많이 들어오는데
정작 코드는 없어서 당황했을 것 같다 ㅎㅎ;;
그래서 정리해서 올려놓으려고 한다.
조건문을 사용해서 쓰면 된다.
1. 이클립스에서 개발을 하는 경우
컨트롤러에서 세션을 형성하고
jsp 화면에서 세션의 값이 있는지 없는지 체크한다.
나는 index 값으로 체크를 했다.
세션이 없으면 로그인과 회원가입이 보이도록 하고,
로그인을 해서 세션이 있으면 로그아웃 버튼을 보이게 하면 된다.
<!--로그인 세션이 없을 경우-->
<% if(session.getAttribute("midx") == null){%>
<a href="<%=request.getContextPath()%>/member/memberLogin.do" id="login">Login</a>
<a href="<%=request.getContextPath()%>/member/memberJoin.do" id="join">Join</a>
<% } %>
<!--로그인 했을 경우-->
<%if(session.getAttribute("midx") != null){ %>
<a href="<%=request.getContextPath()%>/member/memberInfo.do"><%=session.getAttribute("memberNickname")%> <span>님</span><br></a>
<a href="<%=request.getContextPath() %>/member/memberLogout.do" onclick="logout()">로그아웃</a>
<% } %>
2. spring에서 jstl과 el 을 사용하는 경우
마찬가지로 세션에 있는 값을 체크한다.
세션에 있는 값 중에서 닉네임으로 조건문을 작성하였다.
<c:if test="${nickname == null}">
<form action = "login.do" method = "post">
아이디 : <input type = "text" name = "id">
비밀번호 : <input type = "password" name = "password">
<input type = "submit" value = "확인">
</form>
</c:if>
<c:if test="${nickname != null}">
<h3>${session.nickname}님 </h3>
<input type="button" value="로그아웃" onclick="location.href='<%=request.getContextPath()%>/common/logout'">
</c:if>
반응형