package user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class UserDAO {
DataSource dataSource;
public UserDAO() {
try {
InitialContext initContext = new InitialContext();
// 실질적으로 소스에 접속할 수 있도록 함
Context envContext = (Context) initContext.lookup("java:/comp/env");
// 데이터 소스 초기화
dataSource = (DataSource) envContext.lookup("jdbc/UserChat");
}catch (Exception e) {
e.printStackTrace();
}
}
//userID와 userPassword를 받아서
public int login(String userID, String userPassword) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM USER WHERE userID = ?";
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, userID);
rs = pstmt.executeQuery();
if(rs.next()){
//앞 쪽 userPassword는 db에서 가져온 패스워드
if(rs.getString("userPassword").equals(userPassword)) {
return 1; // 로그인에 성공
}
return 2; //비밀번호가 틀림
}else {
return 0; // 해당 사용자가 존재하지 않음.
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
return -1; // 데이터베이스 오류
}
//userID를 받아와서
public int registerCheck(String userID) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM USER WHERE userID = ?";
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, userID);
rs = pstmt.executeQuery();
if(rs.next() || userID.equals("")){
return 0; //이미 존재하는 회원
}else {
return 1; //가입 가능한 회원 아이디
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
return -1; // 데이터베이스 오류
}
// 회원가입하는 함수
public int register(String userID, String userPassword, String userName, String userAge, String userGender, String userEmail, String userProfile) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String SQL = "INSERT INTO USER VALUES(?,?,?,?,?,?,?)";
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, userID);
pstmt.setString(2, userPassword);
pstmt.setString(3, userName);
pstmt.setInt(4, Integer.parseInt(userAge));
pstmt.setString(5, userGender);
pstmt.setString(6, userEmail);
pstmt.setString(7, userProfile);
rs = pstmt.executeQuery();
if(rs.next() || userID.equals("")){
return 0; //이미 존재하는 회원
}else {
return 1; //가입 가능한 회원 아이디
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
return -1; // 데이터베이스 오류
}
}
UserDAO
package user;
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;
@WebServlet("/UserRegisterCheck")
public class UserRegisterCheckServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String userID = request.getParameter("userID");
//문자열 형태를 출력할 수 있도록 공백을 추가해줌.
response.getWriter().write(new UserDAO().registerCheck(userID) + "");
}
}
UserRegisterCheckServlet
package user;
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;
@WebServlet("/userRegisterServlet")
public class UserRegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String userID = request.getParameter("userID");
String userPassword1 = request.getParameter("userPassword1");
String userPassword2 = request.getParameter("userPassword2");
String userName = request.getParameter("userName");
String userAge = request.getParameter("userAge");
String userGender = request.getParameter("userGender");
String userEmail = request.getParameter("userEmail");
String userProfile = request.getParameter("userProfile");
if(userID == null || userID.equals("") || userPassword1 == null || userPassword1.equals("") ||
userPassword2 == null || userPassword2.equals("") || userName == null || userName.equals("") || userAge == null ||
userAge.equals("") || userGender == null || userGender.equals("") ||
userEmail == null || userEmail.equals("")) {
request.getSession().setAttribute("messageType", "오류 메시지");
request.getSession().setAttribute("messageContent", "모든 내용을 입력해 주세요");
response.sendRedirect("join.jsp");
return;
}
if(!userPassword1.equals(userPassword2)) {
request.getSession().setAttribute("messageType", "오류 메시지");
request.getSession().setAttribute("messageContent", "암호가 일치하지 않습니다.");
response.sendRedirect("join.jsp");
return;
}
//여기까지 왔으면 userPassword에 1을 넣든 2를 넣든 상관 없음.
int result = new UserDAO().register(userID, userPassword1, userName, userAge, userGender, userEmail, userProfile);
if(result == 1) {
request.getSession().setAttribute("messageType", "성공 메시지");
request.getSession().setAttribute("messageContent", "회원 가입을 성공하였습니다.");
response.sendRedirect("index.jsp");
return;
} else {
request.getSession().setAttribute("messageType", "오류 메시지");
request.getSession().setAttribute("messageContent", "이미 존재하는 회원 입니다.");
response.sendRedirect("join.jsp");
}
}
}
UserRegisterServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>UserChat</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>UserRegisterServlet</servlet-name>
<servlet-class>user.UserRegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserRegisterServlet</servlet-name>
<url-pattern>/userRegister</url-pattern>
</servlet-mapping>
</web-app>
web.xml
'2020 > JSP 기본 WEB 만들기(미완성)' 카테고리의 다른 글
# 6 채팅 데이터베이스 구축 (0) | 2020.05.28 |
---|---|
#5 채팅창 디자인 구현 (0) | 2020.05.27 |
# 회원 가입 화면 만들기 (0) | 2020.05.20 |
# 2 MySQL 데이터베이스 커넥션 풀 구축 (0) | 2020.05.20 |
#1 메인화면의 메뉴bar 만들기 (0) | 2020.05.19 |