index.jsp
MemberLoginServlet.java
index.jsp
세션 유지를 위해 세션 가져오기
Member m = (Member)session.getAttribute("member");
m 안에 들어있는 값에 따라 화면 구성
1) session이 있다면 -> userName, 환영 인사, 로그아웃 링크, 회원 탈퇴 링크
2) null이라면(첫화면에는 session이 없으니) -> 로그인 폼
<%@page import="kr.or.iei.member.model.vo.Member"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
#loginForm{
border : 1px solid grey;
width : 300px;
height : 250px;
margin: 0 auto;
}
*{
box-sizing: border-box;
}
</style>
</head>
<body>
<H3>메인 페이지 (기본)</H3>
<%
Member m = (Member)session.getAttribute("member");
%>
<%if(m!=null){ %>
[<a href="/member/memberCheck.do"><%=m.getUserName()%></a>] 님 환영합니다!! <a href="/member/logout.do">로그아웃</a><br>
<a href="/member/memberCheck.do">마이 페이지</a><br>
<%if(m.getRoll().substring(0, 2).equals("AD")){ //관리자로 구분되어진 사용자라면 %>
<a href="/admin/memberAllList.do">회원관리 페이지</a><br>
<a href="/admin/boardAllList.do">게시판 관리 페이지</a><br><br><hr>
<%}else{ %>
<a href="/member/memberWithDraw.do" id="withDraw">회원 탈퇴</a><br>
<%} %>
<script>
document.getElementById('withDraw').onclick=function(){
// 1. 탈퇴를 진행하시겠습니까?
// 2. 탈퇴를 하시게 되면, 데이터는 절대 복구 불가능합니다.
if(window.confirm("탈퇴를 진행하시겠습니까?") && window.confirm("탈퇴를 하시게 되면, 데이터는 절대 복구 불가능합니다."))
{
return true;
}else
{
return false;
}
};
</script>
<%}else{ %>
<div id="loginForm">
<form action="/member/login.do" method="post">
<fieldset>
<legend style="text-align: center;">로그인</legend>
<input type="text" class="b-block w-100" name="userId" placeholder="ID를 입력하세요"/><br><br>
<input type="password" class="b-block w-100" name="userPwd" placeholder="PW를 입력하세요"/><br><br>
<input type="submit" class="btn btn-primary b-block w-100" value="로그인"/>
</fieldset>
<br>
<a href="/views/member/memberJoinus.jsp" style="display:block; text-align: right;">회원가입</a>
</form>
</div>
<%} %>
<a href="/board/boardAllSelectNP.do">게시판(페이징 처리 X)</a><br>
<a href="/board/boardAllSelect.do">게시판(페이징 처리 O)</a><br>
</body>
</html>
package kr.or.iei.member.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import kr.or.iei.member.model.service.MemberService;
import kr.or.iei.member.model.service.MemberServiceImpl;
import kr.or.iei.member.model.vo.Member;
/**
* Servlet implementation class MemberLoginServlet
*/
@WebServlet("/member/login.do")
public class MemberLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MemberLoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 로그인을 처리하기 위한 Servlet
// 1. 2개의 값을 가져와야한다 (userId, userPwd)
String userId = request.getParameter("userId");
String userPwd = request.getParameter("userPwd");
// check(데이터 잘 넘어 왔나)
/*
* System.out.println("보내온 ID : "+userId);
* System.out.println("보내온 PW : "+userPwd);
*/
// 2. 비즈니스 로직 처리
MemberService mService = new MemberServiceImpl();
Member m = mService.selectOneMember(userId, userPwd);
if (m != null) { // 로그인 성공
HttpSession session = request.getSession(true);
session.setAttribute("member", m);
//로그인 성공시 sendRedirect 메소드를 통하여 메인페이지로 이동
// sendRedirect 메소드는 사용자의 URL을 변경시켜주는 response 객체의 메소드
response.sendRedirect("/");
} else { // 로그인 실패
// RequestDispatcher를 이용한면 URL을 변경하지 않고, 이동할 수 있다.
// 이때 pageContext.forward 처럼 request와 response 객체를 가지고 이동할 수 있다.
RequestDispatcher view = request.getRequestDispatcher("/views/member/memberLoginFail.jsp");
view.forward(request, response);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<script>
alert('로그인을 실패하였습니다. ID/PW를 재 확인해 주세요');
location.replace('/');
</script>
</body>
</html>
package kr.or.iei.member.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class MemberLogoutServlet
*/
@WebServlet("/member/logout.do")
public class MemberLogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MemberLogoutServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//로그아웃을 처리하기 위한 Servlet doGet 메소드
// 1. 세션 가져오기
HttpSession session= request.getSession();
// 2. 세션 파기
session.invalidate();
// 일반적인 로그아웃은 session을 찾아서 파기만 하면 된다.
// 그런데 로그아웃에 대한 시점을 저장하고 싶다면 -> session에서 데이터(ID값)를 찾아서 DB 에 기록을 해야한다.
// 3. 메인페이지로 이동
response.sendRedirect("/");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package kr.or.iei.member.model.service;
import kr.or.iei.member.model.vo.Member;
public interface MemberService {
public Member selectOneMember(String userId, String userPwd);
public int insertOneMember(Member m);
public int updateOneMember(Member m);
public int deleteOneMemeber(int userNo);
public Member refreshOneMember(int userNo);
public boolean selectIdCheck(String userId);
public int updatePwdMember(String userId, String pwd, String newPwd);
}
package kr.or.iei.member.model.service;
import java.sql.Connection;
import kr.or.iei.common.JDBCTemplate;
import kr.or.iei.member.model.dao.MemberDAO;
import kr.or.iei.member.model.vo.Member;
import sun.management.jdp.JdpBroadcaster;
public class MemberServiceImpl implements MemberService {
private MemberDAO mDAO = new MemberDAO();
@Override
public Member selectOneMember(String userId, String userPwd) {
Connection conn = JDBCTemplate.getConnection();
Member m = mDAO.selectOneMember(userId, userPwd, conn);
JDBCTemplate.close(conn);
return m;
}
@Override
public int insertOneMember(Member m) {
Connection conn = JDBCTemplate.getConnection();
int result = mDAO.insertOneMember(m, conn);
if (result > 0) {
JDBCTemplate.commit(conn);
} else {
JDBCTemplate.rollback(conn);
}
JDBCTemplate.close(conn);
return result;
}
@Override
public int updateOneMember(Member m) {
Connection conn = JDBCTemplate.getConnection();
int result = mDAO.updateOneMember(m, conn);
if(result>0) {
JDBCTemplate.commit(conn);
}else {
JDBCTemplate.rollback(conn);
}
JDBCTemplate.close(conn);
return result;
}
@Override
public int deleteOneMemeber(int userNo) {
Connection conn = JDBCTemplate.getConnection();
int result = mDAO.deleteOneMemeber(userNo, conn);
if (result > 0) {
JDBCTemplate.commit(conn);
} else {
JDBCTemplate.rollback(conn);
}
JDBCTemplate.close(conn);
return result;
}
@Override
public Member refreshOneMember(int userNo) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean selectIdCheck(String userId) {
Connection conn = JDBCTemplate.getConnection();
boolean result = mDAO.selectIdCheck(userId, conn);
JDBCTemplate.close(conn);
return result;
}
@Override
public int updatePwdMember(String userId, String pwd, String newPwd) {
Connection conn= JDBCTemplate.getConnection();
int result = mDAO.updatePwdMember( userId, pwd, newPwd, conn);
if(result>0)JDBCTemplate.commit(conn);
else JDBCTemplate.rollback(conn);
JDBCTemplate.close(conn);
return result;
}
}
package kr.or.iei.member.model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import kr.or.iei.common.JDBCTemplate;
import kr.or.iei.member.model.vo.Member;
public class MemberDAO {
public Member selectOneMember(String userId, String userPwd, Connection conn) {
PreparedStatement pstmt= null;
ResultSet rset = null;
Member m = null;
String sql="SELECT * FROM MEMBER WHERE userId=? AND userPwd=? AND end_YN='N'";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, userId);
pstmt.setString(2, userPwd);
rset=pstmt.executeQuery();
if(rset.next()) {
m=new Member();
m.setUserNo(rset.getInt("userNo"));
m.setUserId(rset.getString("userId"));
m.setUserPwd(rset.getString("userPwd"));
m.setUserName(rset.getString("userName"));
m.setAge(rset.getInt("age"));
m.setEmail(rset.getString("email"));
m.setPhone(rset.getString("phone"));
m.setAddress(rset.getString("address"));
m.setGender(rset.getString("gender").charAt(0));
m.setHobby(rset.getString("hobby"));
m.setEnrollDate(rset.getDate("enrollDate"));
m.setEndYN(rset.getString("end_YN").charAt(0));
m.setRoll(rset.getString("roll"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(rset);
JDBCTemplate.close(pstmt);
}
return m;
}
public int insertOneMember(Member m, Connection conn) {
PreparedStatement pstmt= null;
int result=0;
String sql="INSERT INTO MEMBER VALUES(MEMBER_SEQ.NEXTVAL, ?,?,?,?,?,?,?,?,?,SYSDATE,'N',DEFAULT)";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, m.getUserId());
pstmt.setString(2, m.getUserPwd());
pstmt.setString(3, m.getUserName());
pstmt.setInt(4, m.getAge());
pstmt.setString(5, m.getEmail());
pstmt.setString(6, m.getPhone());
pstmt.setString(7, m.getAddress());
pstmt.setString(8, String.valueOf(m.getGender()));
pstmt.setString(9, m.getHobby());
result=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(pstmt);
}
return result;
}
public boolean selectIdCheck(String userId, Connection conn) {
PreparedStatement pstmt= null;
ResultSet rset=null;
boolean result=false;
String sql= "SELECT userId FROM MEMBER WHERE userId=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, userId);
rset=pstmt.executeQuery();
if(rset.next()) {
result=true;
}else {
result=false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(rset);
JDBCTemplate.close(pstmt);
}
return result;
}
public int deleteOneMemeber(int userNo, Connection conn) {
PreparedStatement pstmt= null;
int result = 0;
String sql= "UPDATE MEMBER SET END_YN='Y' WHERE USERNO=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, userNo);
result=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(pstmt);
}
return result;
}
public int updateOneMember(Member m, Connection conn) {
PreparedStatement pstmt= null;
int result = 0;
String sql="UPDATE MEMBER SET age=?,email=?, phone=?,address=?, gender=?, hobby=? WHERE userid=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, m.getAge());
pstmt.setString(2, m.getEmail());
pstmt.setString(3, m.getPhone());
pstmt.setString(4, m.getAddress());
pstmt.setString(5, String.valueOf(m.getGender()));
pstmt.setString(6, m.getHobby());
pstmt.setString(7, m.getUserId());
result=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(pstmt);
}
return result;
}
public int updatePwdMember(String userId, String pwd, String newPwd, Connection conn) {
PreparedStatement pstmt = null;
int result = 0;
String sql="UPDATE MEMBER SET userPwd=? WHERE userId=? AND userPwd=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, newPwd);
pstmt.setString(2, userId);
pstmt.setString(3, pwd);
result = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCTemplate.close(pstmt);
}
return result;
}
}