🎈 login_view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login_db_teacher.jsp" method="post" name="frm">
<p>
<label>아이디 <input type="text" name="userid"> </label>
</p>
<p>
<label>비밀번호 <input type="password" name="userpw"> </label>
</p>
<input type="button" value="로그인" onclick="sendit();">
</form>
</body>
<script src="login.js">
</script>
</html>
🎈 UserDAO_teacher.java
package com.koit.web.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.koit.web.beans.UserBean;
public class UserDAO_teacher {
Connection conn;
PreparedStatement pstm;
ResultSet rs;
public boolean join(UserBean user) {
boolean result = false;
String sql = "INSERT INTO TBL_USER values(?, ?, ?, ?, ?)";
int count = 0;
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(sql);
pstm.setString(1, user.getUserid());
pstm.setString(2, user.getUserpw());
pstm.setString(3, user.getUsername());
pstm.setString(4, user.getUsergender());
pstm.setString(5, user.getUserphone());
count = pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if(count != 0) {
System.out.println("회원가입 성공");
result = true;
}else {
System.out.println("회원가입 실패");
result = false;
}
return result;
}
public UserBean login(String userid, String userpw) {
UserBean user = null;
String sql = "SELECT * FROM TBL_USER tu WHERE USERID = ? AND USERPW =?";
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(sql);
pstm.setString(1, userid);
pstm.setString(2, userpw);
rs = pstm.executeQuery();
if(rs.next()) {
user = new UserBean();
user.setUserid(rs.getString(1));
user.setUserpw(rs.getString(2));
user.setUsername(rs.getString(3));
user.setUsergender(rs.getString(4));
user.setUserphone(rs.getString(5));
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
- 로그인을 하면 id, pw를 파라미터로 받아온다.
- DAO 클래스 login() 에 파라미터로 넘겨준다.
- UserBean 객체를 생성하고, 받아온 id,pw로 모든 컬럼을 select해와서 빈즈 객체에 담아주고, 이 빈즈 객체를 리턴한다.
🎈 login_db_teacher.jsp
<%@page import="com.koit.web.dao.UserDAO_teacher"%>
<%@page import="com.koit.web.beans.UserBean"%>
<%@page import="com.koit.web.dao.UserDAO"%>
<%@page import="com.koit.web.dao.UserDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String userid = request.getParameter("userid");
String userpw = request.getParameter("userpw");
UserDAO_teacher udao = new UserDAO_teacher();
UserBean user = udao.login(userid, userpw);
if(user != null){
session.setAttribute("session_id", user);
%>
<script type="text/javascript">
location.href = "main_view.jsp"
</script>
<%
}else{
%>
<script type="text/javascript">
alert("로그인 실패");
history.go(-1);
</script>
<%
}
%>
</body>
</html>
- DAO 객체를 생성하고 파라미터로 받은 id, pw를 login()에 넘겨서 받은 빈즈 객체를 user변수에 저장한다.
- 객체가 null이 아니라면, session에 빈즈 객체를 저장한다.
🎈 main_view.jsp
<%@page import="com.koit.web.beans.UserBean"%>
<%@page import="com.koit.web.dao.UserDAO_teacher"%>
<%@page import="com.koit.web.dao.UserDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
UserDAO_teacher udao = new UserDAO_teacher();
UserBean user = (UserBean)session.getAttribute("session_id");
String username = user.getUsername();
String userid = user.getUserid();
if(userid == null){
%>
<script>
alert("로그인 후 이용해주세요");
location.href='login_view.jsp';
</script>
<%
}else{
%>
<script type="text/javascript">
alert("<%=userid%>(<%=username%>)님 안녕하세요!");
</script>
<%
}
%>
<p>
<%=userid %>(<%=username%>)님 <input type="button" value="로그아웃">
</p>
</body>
</html>
- 로그인에 성공하면 메인페이지로 이동된다.
- DAO 객체를 생성하고 session에 저장된 빈즈 객체를 다시 가져온다.
- 이름과 아이디를 빈즈 객체에서 꺼내 사용한다.