본문 바로가기
CLASS/SERVLET

#7-1 / 회원 가입 후 로그인,로그아웃

by eungSe__ 2024. 6. 19.

👀 참고

out.print(); : 웹페이지(jsp)에 출력

<% if (id==null || name ==null) { %>
<!--로그인 전-->
<p><a href="./login.html">로그인</a>| 회원가입 | 고객센터</p>
<% } else { %>
<!--로그인 후-->
<p><%=name%>님 환영합니다. | 장바구니 | 고객센터</p>
<% } %>


<!-- 
    이름 출력시 
    <p><%out.print(name);%>님 환영합니다. | 장바구니 | 고객센터</p>
    요것도 가능!
-->

 

 

* session 가져올 시 null일 경우 String으로 변환  관련 https://dev-eunse.tistory.com/153

⚡ 회원가입 후 로그인기능 

이전 과정들은  https://dev-eunse.tistory.com/150 여기에 있음!
= 회원가입(sql data 저장) + 로그인(성공시 세션저장)

- top.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%	
	HttpSession hs = request.getSession(); //object 배열형태
	String id = (String)hs.getAttribute("id");
	String name = (String)hs.getAttribute("name");
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>웹사이트 상단 페이지</title>
<script src="./jquery3.js"></script>
</head>
<body>
<%if (id==null || name ==null) {%> <!-- 로그인 전 -->
<p><a href="./login.html">로그인</a>| 회원가입 | 고객센터</p>
<% }else { %> <!-- 로그인 후 -->
<p><%=name%>님 환영합니다. | 장바구니 | 고객센터 | <input type="button" value="로그아웃" id="btn"></p>
<% } %>
</body>

<script type="text/javascript">
	$(function(){
		$("#btn").click(function(){
			if(confirm("로그아웃 하시겠습니까?")){ //확인창 띄우기(확인/취소 - else작성필요없음)
				//location.href시 servlet post x => get으로 받아야함
				location.href = './logout.do';
			};
		})
	})
</script>

</html>



- logout.java (.do)

public class logout extends HttpServlet {
	private static final long serialVersionUID = 1L;
    PrintWriter pw = null;
    HttpSession hs = null;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");

		this.hs = request.getSession();
		this.hs.invalidate();
		this.pw = response.getWriter();
		this.pw.write("<script>"
				+ "alert('정상적으로 로그아웃되셨습니다.');"
				+ "location.href='./top.jsp';"
				+ "</script>");
		this.pw.close();
	}
}

 

로그인 전
로그인 후

 

 

 

 

 

✅ 회원가입 -> 로그인 -> 로그아웃시 순서  

  1. 기획서 맞게 DB설계
  2. 회원가입페이지 제작 : 회원가입 시 DB에 insert
  3. 로그인페이지 제작 : 로그인시 DB에 select / session 저장
  4. 로그아웃 : session 삭제

'CLASS > SERVLET' 카테고리의 다른 글

#7-3 / 첨부파일 기능 + database 저장  (0) 2024.06.19
#7-2 / 첨부파일 기능 - java + io  (0) 2024.06.19
#6-3 / storage  (0) 2024.06.18
#6-2 / session,cookie  (0) 2024.06.18
#6-1 / 간편 회원가입⭐  (0) 2024.06.18