[JSP] 회원가입 / 회원인증 ⑤ - 로그인, 로그아웃


member/Login.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String uId = (String)session.getAttribute("idKey");
%>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>로그인 페이지</title>
    <link rel="stylesheet" href="">
</head>
<body>
	<% if (uId != null) { %> 
	<!-- 현재 로그인 상태라면 -->
	
	<h2><%=uId %>님 환영합니다.</h2>
	<a href="Logout.jsp">로그아웃</a>
	<a href="Memberinfo_Mod.jsp">회원정보 수정</a>
	<a href="MemberInfo_Del.jsp">회원 탈퇴</a>
	
	<% } else { %>
	
	<form action="LoginProc.jsp" method="post" name="loginFrm" id="loginFrm">
		<table>
			<caption>로그인</caption>
			<tbody>
				<tr>
					<td>아이디</td>
					<td>
						<input type="text" name="uId" id="uId">
					</td>
				</tr>
				<tr>
					<td>비밀번호</td>
					<td>
						<input type="password" name="uPw" id="uPw">
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<button type="button" id="mainBtn">메인으로</button>
						<button type="button" id="loginBtn">로그인</button>
						<button type="button" id="joinBtn">회원가입</button>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
	
	<% } %>
	
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
	<script>
	$(function(){
		$("#loginBtn").click(function(){

			let uId = $("#uId").val();
			let uPw = $("#uPw").val();
			
			if (uId == ""){
   				alert("아이디를 입력하세요.");
   				$("#uId").focus();
   			}
			else if (uPw == ""){
   				alert("비밀번호를 입력하세요.");
   				$("#uPw").focus();
   			}
			else {
				$("#loginFrm").submit()
			}
		});
		
		$("loginBtn").click(function(){
			fnLogin();
		});
		
		$("#mainBtn").click(function(){   			
			location.href="../Index.jsp";				
		});
		
		$("#joinBtn").click(function(){
			location.href="Member.jsp";
		})
		
		$("input.loginInput").keyup(function(event){
			let code = event.keyCode;
			if(code==13) fnLogin();
		});
		
		});
	</script>
</body>
</html>



member/LoginProc.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<jsp:useBean id="memberMgr" class="pack_Member.MemberMgr" scope="page" />
    
<%
	request.setCharacterEncoding("UTF-8");
	String uId = request.getParameter("uId");
	String uPw = request.getParameter("uPw");
	
	boolean res = memberMgr.loginMember(uId, uPw);
	
	if (res) {
		session.setAttribute("idKey", uId);
		response.sendRedirect("../Index.jsp");
	} 
%>    
<script>
	alert("아이디와 비밀번호를 확인해주세요.");
	history.back();
</script>



pack_Member/MemberMgr.java


package pack_Member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MemberMgr {

	private DBConnectionMgr pool;
	
	Connection 			conn 	= null;
	PreparedStatement 	pstmt 	= null;
	ResultSet 			rs 		= null;
	String 				sql 	= null;

	public MemberMgr() {
		try {
			pool = DBConnectionMgr.getInstance();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
    // 로그인
	public boolean loginMember(String uId, String uPw) {
		
		boolean flag = false;
		
		try {
			conn = pool.getConnection();
			sql = "select uId from tblMember where uId=? and uPw=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, uId);
			pstmt.setString(2, uPw);
			rs = pstmt.executeQuery();
			flag = rs.next();
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return flag;
	}
}



  • 아이디   비밀번호 잘못 입력한 경우


  • 아이디 && 비밀번호 맞게 입력한 경우 : LoginPro.jsp -> Index.jsp 로 이동



member/Logout.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% session.invalidate(); %>
<script>
	location.href="../Index.jsp";
</script>

Categories:

JSP/Servlet