백엔드 개발/Servlet,JSP

[Servlet,JSP]데이터베이스 연동(로그인 환경 구축)

독학하는 정호빈 2022. 1. 19. 21:01

유튜브 강의 '자바-JSP-Spring'을 기반으로

공부하였습니다.

https://www.youtube.com/watch?v=OFnieB9KDTg&list=PLieE0qnqO2kTyzAlsvxzoulHVISvO8zA9&index=50&ab_channel=SEOULG-%EC%BA%A0%ED%94%84

 

 

1. 회원가입

 

(input.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="inputdata.jsp" method="post">
	아이디<br>
	<input type="text" name="id"><br>
	비밀번호<br>
	<input type="password" name="pw"><br>
	<p>
		<input type="submit" value="회원가입"><br>
	</p>
</form>
<p>
	<a href="login.html">홈으로</a>
</p>
</body>
</html>

 

 

(inputdata.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%! 
	String id, pw, qry;
	String DB_Driver = "org.mariadb.jdbc.Driver";
	String url = "jdbc:mariadb://?????/?????";
	Connection conn;
	Statement stmt;
	PreparedStatement pstmt;
%>

<%
	request.setCharacterEncoding("UTF-8");
	id = request.getParameter("id");
	pw = request.getParameter("pw");
	qry = "INSERT INTO member VALUES (?,?)";
	
	try{
		
		Class.forName(DB_Driver);
		conn = DriverManager.getConnection(url,"????","????");
		pstmt = conn.prepareStatement(qry);
		
		pstmt.setString(1, id);
		pstmt.setString(2, pw);
		
		int i = pstmt.executeUpdate();
		if(i==1){
			out.println("<script>alert('success')</script>");
			out.println("<script>location.replace('login.html')</script>");
		}
		else{
			out.println("<script>alert('fail')</script>");
			out.println("<script>location.replace('input.html')</script>");
		}
		
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		try{
			if(stmt != null)stmt.close();
			if(pstmt != null)pstmt.close();
			if(conn != null)conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
%>
</body>
</html>

 

 

회원가입화면

 

 

회원가입 성공

 

 

데이터베이스에 정상 추가

 

2. 로그인

 

(login.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="logindata.jsp" method="post">
	아이디<br>
	<input type="text" name="id"><br>
	비밀번호<br>
	<input type="password" name="pw"><br>
	<p>
		<input type="submit" value="로그인"><br>
	</p>
</form>
<p>
	<a href="input.html">회원가입</a>
</p>

</body>
</html>

 

 

(logindata.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%! 
	String id, pw, qry;
	String DB_Driver = "org.mariadb.jdbc.Driver";
	String url = "jdbc:mariadb://????/????";
	Connection conn;
	Statement stmt;
	PreparedStatement pstmt;
	ResultSet rs;
%>

<%
	request.setCharacterEncoding("UTF-8");
	id = request.getParameter("id");
	pw = request.getParameter("pw");
	qry = "SELECT pw FROM member WHERE id=?";
	
	try{
		
		Class.forName(DB_Driver);
		conn = DriverManager.getConnection(url,"????","????");
		pstmt = conn.prepareStatement(qry);
		
		pstmt.setString(1, id);
		
		rs = pstmt.executeQuery();
		if(rs.next()){
			String rs_pw = rs.getString("pw");
			
			if(rs_pw.equals(pw)){
				out.println("로그인 성공!");
			}
			else{
				out.println("비밀번호가 다릅니다.");
			}
			
		}
		else{
			out.println("해당하는 아이디가 없습니다.");
			
		}
		
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		try{
			if(rs != null)rs.close();
			if(stmt != null)stmt.close();
			if(pstmt != null)pstmt.close();
			if(conn != null)conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
%>
</body>
</html>

 

 

로그인 실행화면

 

 

로그인 성공

 

 

다른 비밀번호 입력

 

 

비밀번호 오류

 

없는 아이디 입력

 

 

아이디 오류

 

'백엔드 개발 > Servlet,JSP' 카테고리의 다른 글

[Servlet,JSP]EL(Expression Language)  (0) 2022.01.24
[Servlet,JSP]데이터베이스 연동(환경설정)  (0) 2022.01.18
[Servlet,JSP]자바 빈  (0) 2022.01.14
[Servlet,JSP]예외처리  (0) 2022.01.14
[Servlet,JSP]쿠키와 세션  (0) 2022.01.12