로그인 정보를 DB와 비교를 하고, 사용자의 email을 띄워줌
<ul class="links">
<li><h5>로그인</h5></li>
<form action="LoginServiceCon" method = "post">
<li><input type="text" name="loginemail" placeholder="Email을 입력하세요"></li>
<li><input type="password" name="pw" placeholder="PW를 입력하세요"></li>
<li><input type="submit" value="LogIn" class="button fit"></li>
</form>
</ul>
public MemberDTO login(String email, String pw) {
dbconn();
try {
//사용자가 입력한 정보가 있는지 없는지 확인 필요값 불러오기
String sql = "select * from web_member2 where email=? and pw = ?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, email);
psmt.setString(2, pw);
rs = psmt.executeQuery();
if(rs.next()) { //true일때만 실행이됨, 해당값이 있을때만 if문
email = rs.getString(1);
pw = rs.getString(2);
String tel = rs.getString(3);
String address = rs.getString(4);
//실행결과
dto = new MemberDTO(email, pw, tel, address);
}
}catch(Exception e){
e.printStackTrace();
}finally {
dbclose();
}
return dto;
}
package 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;
import Model.MemberDTO;
import Model.memberDAO;
@WebServlet("/LoginServiceCon")
public class LoginServiceCon extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("[LoginServiceCon]");
//post방식 인코딩
request.setCharacterEncoding("euc-kr");
String email = request.getParameter("loginemail");
String pw = request.getParameter("pw");
//login 메소드
MemberDTO dto = new memberDAO().login(email, pw);
//성공 실패 여부 따지기
if(dto != null) {
System.out.println("로그인성공");
System.out.println(dto.getEmail());
HttpSession session = request.getSession();
session.setAttribute("info", dto);
}else {
System.out.println("로그인실패");
}
response.sendRedirect("main.jsp");
}
}
<body>
<%
//세션의 회원정보 가져오기
//로그인 성공시 info라는 이름으로 담아준 세션의 정보가있고, 로그인 실패 or 하지않은상태라면 null
//가져오는 데이터타입이 MemberDTO
MemberDTO info = (MemberDTO)session.getAttribute("info");
%>
<!-- Header -->
<header id="header" class="alt"> <a href="index.html"
class="logo"><strong>Forty</strong> <span>by HTML5 UP</span></a>
<nav>
<% if(info != null){ %>
<a href="update.jsp">개인정보수정<a>
<a href="LogoutServiceCon">로그아웃<a>
<%} else {%>
<a href ="#menu">로그인<a>
<%} %>
<!-- 로그인 후 Logout.jsp로 이동할 수 있는'로그아웃'링크와 '개인정보수정'링크를 출력하시오. -->
</nav> </header>
<header class="major">
<% if(info!=null){ %>
<h1> <%= info.getEmail() %> 님 환영합니다!</h1>
<%} else{ %>
<h1> 로그인한 세션 아이디를 출력해주세요</h1>
<%} %>
<!-- 로그인 후 로그인 한 사용자의 세션아이디로 바꾸시오.
ex)smart님 환영합니다 --> </header>
<a href="update.jsp">개인정보수정<a>
MemberDTO info = (MemberDTO)session.getAttribute("info");
<%@page import="Model.MemberDTO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Forty by HTML5 UP</title>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]-->
<link rel="stylesheet" href="assets/css/main.css" />
<!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><![endif]-->
<!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]-->
</head>
<style>
</style>
<body style="text-align: center;">
<%
MemberDTO info = (MemberDTO)session.getAttribute("info");
%>
<!-- Wrapper -->
<div id="wrapper">
<!-- Menu -->
<nav id="Update">
<ul class="actions vertical">
<li><h5>회원정보수정</h5></li>
<form action="UpdateServiceCon" method="post">
<li>접속한 Email : <%= info.getEmail() %></li>
<li><input type="password" name="pw" placeholder="PW를 입력하세요" style="width: 500px; margin: 0 auto;"></li>
<li><input type="text" name="tel" placeholder="전화번호를 입력하세요" style="width: 500px; margin: 0 auto;"></li>
<li><input type="text" name="address" placeholder="집주소를 입력하세요" style="width: 500px; margin: 0 auto;"></li>
<li><input type="hidden" name="email" value="<%=info.getEmail() %>"></li>
<li><input type="submit" value="JoinUs" class="button fit" style="width: 500px; margin: 0 auto;"></li>
</form>
</ul>
</nav>
</div>
public int update(MemberDTO dto) {
dbconn();
try {
String sql ="update web_member2 set pw=?, tel=?, address=? where email=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, dto.getPw());
psmt.setString(2, dto.getTel());
psmt.setString(3, dto.getAddress());
psmt.setString(4, dto.getEmail());
cnt = psmt.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally{
dbclose();
}
return cnt;
}
package 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;
import Model.MemberDTO;
import Model.memberDAO;
@WebServlet("/UpdateServiceCon")
public class UpdateServiceCon extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("[UpdateServiceCon]");
request.setCharacterEncoding("euc-kr");
String email = request.getParameter("email");
String pw = request.getParameter("pw");
String tel = request.getParameter("tel");
String address = request.getParameter("address");
System.out.println("email : "+email);
System.out.println("pw : "+ pw);
System.out.println("tel : "+ tel );
System.out.println("address : "+ address );
MemberDTO dto = new MemberDTO(email, pw, tel, address);
int cnt = new memberDAO().update(dto);
// int cnt = new memberDAO().update(new MemberDTO(email, pw, tel, address));
if(cnt>0) {
System.out.println("회원정보 수정 성공");
//성공시 session에 새로운 정보로 update
HttpSession session = request.getSession();
session.setAttribute("info", dto);
}else {
System.out.println("회원정보 수정 실패");
}
response.sendRedirect("main.jsp");
}
}
<a href="LogoutServiceCon">로그아웃<a>
package 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;
@WebServlet("/LogoutServiceCon")
public class LogoutServiceCon extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("[LogoutServiceCon]");
//로그아웃 : 로그인한 정보 삭제
HttpSession session = request.getSession();
// session.invalidate(); 페이지에사용되는 모든 세션 삭제 => 세션삭제
session.removeAttribute("info");
System.out.println("로그아웃 성공");
response.sendRedirect("main.jsp");
}
}