목표 로그인 및 로그아웃 구현.
노란부분 ->추가된 파일
STEP 1 userLoginAction.jsp : 실제 사용자의 로그인을 처리
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="user.UserDTO"%>
<%@ page import="user.UserDAO"%>
<%@ page import="java.io.PrintWriter"%>
<%
request.setCharacterEncoding("UTF-8");
String userID = null;
String userPassword = null;
if(request.getParameter("userID") !=null) {
userID = request.getParameter("userID");
}
if(request.getParameter("userPassword") != null) {
userPassword = (String) request.getParameter("userPassword");
}
if(userID == null ||userPassword ==null) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('입력이 안 된 사항이 있습니다.');");
script.println("history.back();");
script.println("</script>");
script.close();
return;
}
UserDAO userDAO = new UserDAO();
int result = userDAO.login(userID, userPassword);
if (result == 1) {
session.setAttribute("userID", userID);
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("location.href='index.jsp'");
script.println("</script>");
script.close();
return;
} else if (result == 0) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('비밀번호가 틀립니다.');");
script.println("history.back();");
script.println("</script>");
script.close();
return;
} else if (result == -1) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('존재하지 않는 아이디입니다.');");
script.println("history.back();");
script.println("</script>");
script.close();
return;
} else if (result == -2) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('데이터베이스 오류가 발생했습니다.');");
script.println("history.back();");
script.println("</script>");
script.close();
return;
}
%>
STEP 2 userLogout.jsp : 사용자의 로그인 세션 파기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.invalidate();
%>
<script>
location.href = 'index.jsp';
</script>
STEP 3 index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="user.UserDAO" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; "charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>강의평가 웹 사이트</title>
<!-- 부트스트랩 CSS 추가 -->
<link rel="stylesheet" href="./css/bootstrap.min.css">
<!-- 커스텀 CSS 추가 -->
<link rel="stylesheet" href="./css/custom.css">
</head>
<body>
<%
String userID = null;
if(session.getAttribute("userID") != null) {
userID = (String) session.getAttribute("userID");
}
if(userID == null) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('로그인을 해주세요.');");
script.println("location.href = 'userLogin.jsp';");
script.println("</script>");
script.close();
return;
}
boolean emailChecked = new UserDAO().getUserEmailChecked(userID);
if(emailChecked == false) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("location.href = 'emailSendConfirm.jsp';");
script.println("</script>");
script.close();
return;
}
%>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="index.jsp">강의평가 웹 사이트</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbar" class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="index.jsp">메인</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="dropdown" data-toggle="dropdown">
회원관리
</a>
<div class="dropdown-menu" aria-labelledby="dropdown">
<%
if(userID == null) {
%>
<a class="dropdown-item" href="userLogin.jsp">로그인</a>
<a class="dropdown-item" href="userJoin.jsp">회원가입</a>
<%
} else {
%>
<a class="dropdown-item" href="userLogout.jsp">로그아웃</a>
<%
}
%>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="내용을 입력하세요" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">검색</button>
</form>
</div>
</nav>
<section class="container"> <!-- 검색창 -->
<form method="get" action="./index.jsp" class="form-inline mt-3"> <!-- get 방식으로 어떠한 내용을 서버로 전달 'mt-3'는 위쪽으로 3만큼 마진 -->
<select name ="lectureDivide" class="form-control mx=1 mt-2"> <!-- select를 이용해 강의 구분을 서버로 전달 -->
<option value="전체">전체</option> <!-- option으로 서버로 내용 전달 -->
<option value="전공">전공</option>
<option value="교양">교양</option>
<option value="기타">기타</option>
</select>
<input type="text" name="search" class="form-control mx-1 mt-2" placeholder="Search"> <!-- input을 이용해 사용자가 실질적으로 입력하도록 -->
<button type="submit" class="btn btn-primary mx-1 mt-2">검색</button>
<a class="btn btn-primary mx-1 mt-2" data-toggle="modal" href="#registerModal">등록하기</a> <!-- modal은 웹페이지 위에 나오는 것 -->
<a class="btn btn-danger mx-1 mt-2" data-toggle="modal" href="#reportModal">신고</a>
</form>
<div class="card bg-light mt-3">
<div class="card-header bg-light">
<div class="row">
<div class="col-8 text-left">컴퓨터개론 <small>한승표</small></div>
<div class="col-4 text-right">
종합 <span style="color: red;">A</span>
</div>
</div>
</div>
</div>
<div class="card-body">
<h5 class="card-title">
좋은 강의에요. <small>(2018년 가을학기)</small>
</h5>
<p class="card-text">강의가 몰입도가 있고, 교수님도 열정적이고 재치가 있습니다.</p>
<div class="row">
<div class="col-9 text-left">
성적<span style="color: red;">A</span>
널널<span style="color: red;">A</span>
강의<span style="color: red;">B</span>
<span style="color: green;">(추천: 15)</span>
</div>
<div class="col-3 text-right">
<a onclick="return confirm('추천하시겠습니까?')" href="./likeAction.jsp?evaluationID=">추천</a>
<a onclick="return confirm('삭제하시겠습니까?')" href="./deleteAction.jsp?evaluationID=">삭제</a>
</div>
</div>
</div>
<div class="card bg-light mt-3">
<div class="card-header bg-light">
<div class="row">
<div class="col-8 text-left">축구학 <small>손흥민</small></div>
<div class="col-4 text-right">
종합 <span style="color: red;">A</span>
</div>
</div>
</div>
</div>
<div class="card-body">
<h5 class="card-title">
유익하고 재밌습니다. <small>(2018년 가을학기)</small>
</h5>
<p class="card-text">다양한 실전 기술을 배울 수 있습니다</p>
<div class="row">
<div class="col-9 text-left">
성적<span style="color: red;">A</span>
널널<span style="color: red;">A</span>
강의<span style="color: red;">B</span>
<span style="color: green;">(추천: 151)</span>
</div>
<div class="col-3 text-right">
<a onclick="return confirm('추천하시겠습니까?')" href="./likeAction.jsp?evaluationID=">추천</a>
<a onclick="return confirm('삭제하시겠습니까?')" href="./deleteAction.jsp?evaluationID=">삭제</a>
</div>
</div>
</div>
<div class="card bg-light mt-3">
<div class="card-header bg-light">
<div class="row">
<div class="col-8 text-left">정치개론 <small>홍길동</small></div>
<div class="col-4 text-right">
종합 <span style="color: red;">c</span>
</div>
</div>
</div>
</div>
<div class="card-body">
<h5 class="card-title">
유익한 강의에요. <small>(2018년 가을학기)</small>
</h5>
<p class="card-text">정치에 관심이 생겼습니다.</p>
<div class="row">
<div class="col-9 text-left">
성적<span style="color: red;">A</span>
널널<span style="color: red;">A</span>
강의<span style="color: red;">B</span>
<span style="color: green;">(추천: 7)</span>
</div>
<div class="col-3 text-right">
<a onclick="return confirm('추천하시겠습니까?')" href="./likeAction.jsp?evaluationID=">추천</a>
<a onclick="return confirm('삭제하시겠습니까?')" href="./deleteAction.jsp?evaluationID=">삭제</a>
</div>
</div>
</div>
</section>
<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true"> <!-- modal 양식 modal fade를 사용하는 게 일반적 -->
<div class="modal-dialog"> <!-- modal-dialog를 이용해 모달창임을 알려주는 것 -->
<div class="modal-content">
<div class="modal-header"> <!-- 모달창 제목 -->
<h5 class="modal-title" id="modal">평가등록</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <!-- 닫기 버튼 -->
<span aria-hidden="true">×</span> <!-- 닫기 아이콘 -->
</button>
</div>
<div class="modal-body">
<form action="./evaluationRegisterAction.jsp" method="post"> <!--jsp 페이지로 post방식을 이용해 보낸다-->
<div class="form-row"> <!-- 사용자가 입력할 수 있는 내용을 한줄씩 들어가도록 만든다. row는 하나의 행을 여러개의 열로 나눌 때 사용 -->
<div class="form-group col-sm-6">
<label>강의명</label>
<input type="text" name="LectureName" class="form-control" maxlength="20"> <!-- 강의명 최대 20자 -->
</div>
<div class="form-group col-sm-6"> <!-- 일반적으로 한개의 행은 12열이 배당된다. 여기선 위 아래로 반반씩 나누었다 -->
<label>교수명</label>
<input type="text" name="professorName" class="form-control" maxlength="20"> <!-- 강의명 최대 20자 -->
</div>
</div>
<div class="form-row">
<div class="form-group col-sm-4">
<label>수강연도</label>
<select name="lectureYear" class="form-control">
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018" selected>2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
</div>
<div class="form-group col-sm-4">
<label>수강학기</label>
<select name="semesterDivide" class="form-control">
<option value="1학기" selected>1학기</option>
<option value="여름학기">여름학기</option>
<option value="2학기">2학기</option>
<option value="겨울학기">겨울학기</option>
</select>
</div>
<div class="form-group col-sm-4">
<label>강의구분</label>
<select name="lectureDivide" class="form-control">
<option value="전공"selected>전공</option>
<option value="교양">교양</option>
<option value="기타">기타</option>
</select>
</div>
</div>
<div class="form-group">
<label>제목</label>
<input type="text" name="evaluationTime" class="form-control" maxlength="30">
</div>
<div class="form-group">
<label>내용</label>
<textarea name="evaluationContent" class="form-control" maxlength="2048" style="height: 180px;"></textarea>
</div>
<div class="form-row"><!-- 하나의 행을 나눌 때 사용 -->
<div class="form-group col-sm-3">
<label>종합</label>
<select name="totalScore" class="form-control">
<option value="A" selected>A</option>
<option value="B" >B</option>
<option value="C" >C</option>
<option value="D" >D</option>
<option value="E" >E</option>
<option value="F" >F</option>
</select>
</div>
<div class="form-group col-sm-3">
<label>강의</label>
<select name="letureScore" class="form-control">
<option value="A" selected>A</option>
<option value="B" >B</option>
<option value="C" >C</option>
<option value="D" >D</option>
<option value="E" >E</option>
<option value="F" >F</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">취소</button>
<button type="submit" class="btn btn-primary">등록하기</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="modal fade" id="reportModal" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true"> <!-- modal 양식 modal fade를 사용하는 게 일반적 -->
<div class="modal-dialog"> <!-- modal-dialog를 이용해 모달창임을 알려주는 것 -->
<div class="modal-content">
<div class="modal-header"> <!-- 모달창 제목 -->
<h5 class="modal-title" id="modal">신고하기</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <!-- 닫기 버튼 -->
<span aria-hidden="true">×</span> <!-- 닫기 아이콘 -->
</button>
</div>
<div class="modal-body">
<form action="./reportAction.jsp" method="post"> <!--jsp 페이지로 post방식을 이용해 보낸다-->
<div class="form-group">
<label>신고 제목</label>
<input type="text" name="reportTitle" class="form-control" maxlength="30">
</div>
<div class="form-group">
<label>신고 내용</label>
<textarea name="reportContent" class="form-control" maxlength="2048" style="height: 180px;"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">취소</button>
<button type="submit" class="btn btn-danger">신고하기</button>
</div>
</form>
</div>
</div>
</div>
</div>
<footer class="bg-dark mt-4 p-5 text-center" style="color: #FFFFFF;">
Copyright © 2020 한승표 ALL RIGHT Reserved.
</footer>
<!-- 제이쿼리 자바스크립트 추가 -->
<script src="./js/jquery.min.js"></script>
<!-- 파퍼 자바스크립트 추가 -->
<script src="./js/popper.js"></script>
<!-- 부트스트랩 자바스크립트 추가 -->
<script src="./js/bootstrap.min.js"></script>
</body>
</html>
STEP 4 userLogin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintWriter" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; "charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>강의평가 웹 사이트</title>
<!-- 부트스트랩 CSS 추가 -->
<link rel="stylesheet" href="./css/bootstrap.min.css">
<!-- 커스텀 CSS 추가 -->
<link rel="stylesheet" href="./css/custom.css">
</head>
<body>
<%
String userID = null;
if(session.getAttribute("userID") != null) {
userID = (String) session.getAttribute("userID");
}
if(userID != null) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('로그인이 된 상태입니다.');");
script.println("location.href = 'index.jsp';");
script.println("</script>");
script.close();
return;
}
%>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="index.jsp">강의평가 웹 사이트</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbar" class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="index.jsp">메인</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="dropdown" data-toggle="dropdown">
회원관리
</a>
<div class="dropdown-menu" aria-labelledby="dropdown">
<%
if(userID == null) {
%>
<a class="dropdown-item" href="userLogin.jsp">로그인</a>
<a class="dropdown-item" href="userJoin.jsp">회원가입</a>
<%
} else {
%>
<a class="dropdown-item" href="userLogout.jsp">로그아웃</a>
<%
}
%>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="내용을 입력하세요" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">검색</button>
</form>
</div>
</nav>
<section class="container mt-3" style="max-width: 560px;">
<form method="post" action="userLoginAction.jsp">
<div class="form-group">
<label>아이디</label>
<input type="text" name="userID" class="form-control">
</div>
<div class="form-group">
<label>비밀번호</label>
<input type="password" name="userPassword" class="form-control">
</div>
<button type="submit" class="btn btn-primary">로그인</button> <!-- btn-primary 버튼은 파란색으로 구성된 버튼을 뜻한다. -->
</form>
</section>
<footer class="bg-dark mt-4 p-5 text-center" style="color: #FFFFFF;">
Copyright © 2020 한승표 ALL RIGHT Reserved.
</footer>
<!-- 제이쿼리 자바스크립트 추가 -->
<script src="./js/jquery.min.js"></script>
<!-- 파퍼 자바스크립트 추가 -->
<script src="./js/popper.js"></script>
<!-- 부트스트랩 자바스크립트 추가 -->
<script src="./js/bootstrap.min.js"></script>
</body>
</html>
STEP 5 index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="user.UserDTO"%>
<%@ page import="user.UserDAO"%>
<%@ page import="util.SHA256"%>
<%@ page import="java.io.PrintWriter"%>
<%
request.setCharacterEncoding("UTF-8");
String userID = null;
if (session.getAttribute("userID") != null) {
userID = (String) session.getAttribute("userID");
}
if (userID != null) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('로그인이 된 상태입니다.');");
script.println("location.href = 'index.jsp';");
script.println("</script>");
script.close();
return;
}
String userPassword = null;
String userEmail = null;
if (request.getParameter("userID") != null) {
userID = request.getParameter("userID");
}
if (request.getParameter("userPassword") != null) {
userPassword = request.getParameter("userPassword");
}
if (request.getParameter("userEmail") != null) {
userEmail = request.getParameter("userEmail");
}
if (userID == null || userPassword == null || userEmail == null) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('입력이 안 된 사항이 있습니다.');");
script.println("history.back();");
script.println("</script>");
script.close();
return;
}
UserDAO userDAO = new UserDAO();
int result = userDAO.join(new UserDTO(userID, userPassword, userEmail, SHA256.getSHA256(userEmail), false));
if (result == -1) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('이미 존재하는 아이디입니다.');");
script.println("history.back();");
script.println("</script>");
script.close();
return;
} else {
session.setAttribute("userID", userID);
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("location.href = 'emailSendAction.jsp'");
script.println("</script>");
script.close();
return;
}
%>
STEP 6 emailSendConfirm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintWriter" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; "charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>강의평가 웹 사이트</title>
<!-- 부트스트랩 CSS 추가 -->
<link rel="stylesheet" href="./css/bootstrap.min.css">
<!-- 커스텀 CSS 추가 -->
<link rel="stylesheet" href="./css/custom.css">
</head>
<body>
<%
String userID = null;
if(session.getAttribute("userID") != null) {
userID = (String) session.getAttribute("userID");
}
if(userID == null) {
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('로그인을 해주세요.');");
script.println("location.href = 'userLogin.jsp';");
script.println("</script>");
script.close();
return;
}
%>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="index.jsp">강의평가 웹 사이트</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbar" class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="index.jsp">메인</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="dropdown" data-toggle="dropdown">
회원관리
</a>
<div class="dropdown-menu" aria-labelledby="dropdown">
<%
if(userID == null) {
%>
<a class="dropdown-item" href="userLogin.jsp">로그인</a>
<a class="dropdown-item" href="userJoin.jsp">회원가입</a>
<%
} else {
%>
<a class="dropdown-item" href="userLogout.jsp">로그아웃</a>
<%
}
%>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="내용을 입력하세요" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">검색</button>
</form>
</div>
</nav>
<section class="container mt-3" style="max-width: 560px;">
<div class="alert alert-warning mt-4" role= "alert">
이메일 주소 인증을 하셔야 이용 가능 합니다. 인증 메일을 받지 못하셨나요?
</div>
<a href="emailSendAction.jsp" class="btn btn-primary">인증 메일 다시 받기</a>
</section>
<footer class="bg-dark mt-4 p-5 text-center" style="color: #FFFFFF;">
Copyright © 2020 한승표 ALL RIGHT Reserved.
</footer>
<!-- 제이쿼리 자바스크립트 추가 -->
<script src="./js/jquery.min.js"></script>
<!-- 파퍼 자바스크립트 추가 -->
<script src="./js/popper.js"></script>
<!-- 부트스트랩 자바스크립트 추가 -->
<script src="./js/bootstrap.min.js"></script>
</body>
</html>
'JAVA > JSP' 카테고리의 다른 글
JSP 강의평가 웹 사이트 - 평가 검색 및 출력 구현 (0) | 2020.05.10 |
---|---|
JSP로 강의평가 웹 사이트 - 평가 등록 및 신고 구현 (0) | 2020.05.09 |
JSP 강의평가 웹사이트 회원가입 및 이메일 인증 (0) | 2020.05.07 |
JSP 강의평가 웹사이트 - 회원 데이터 모델링 (0) | 2020.05.06 |
JSP 강의평가 웹사이트 - 데이터베이스 구축 (0) | 2020.05.03 |