jsp, java부분을 연습하기 위해
html 및 css파일은 gpt를 통해 예제를 받아왔으니 참고하여 읽어주길 바랍니다.
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/DB_login/css/login.css">
<title>로그인</title>
</head>
<body>
<div class="login-container">
<h2>로그인</h2>
<form action="/DB_login/login.jsp" method="POST">
<input type="text" name="id" placeholder="사용자 아이디" required>
<input type="password" name="password" placeholder="비밀번호" required>
<button type="submit">로그인</button>
</form>
<div class="signup-link">
<a href="/DB_login/signup.html">회원가입</a>
</div>
</div>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean class="loginDB.MemberDAO" id="login"></jsp:useBean>
<jsp:useBean class="loginDB.MemberDTO" id="data"></jsp:useBean>
<jsp:setProperty property="*" name="data" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String id = request.getParameter("id");
String password = request.getParameter("password");
String redirectURL = "http://localhost:8088/DB_login/login.html"; //무언가 잘못되었을 때 넘길 페이지
boolean method = request.getMethod().equals("POST");
if(method){
data.setId(id);
data.setPassword(password);
data = login.selectOne(data);
if(data != null){
System.out.println("로그인 성공");
response.sendRedirect(redirectURL);
}else{
System.out.println("로그인 실패");
response.sendRedirect(redirectURL);
}
}
else{
System.out.println("잘못된 접근");
response.sendRedirect(redirectURL);
}
%>
</body>
</html>
singup.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/DB_login/css/signup.css">
<title>회원가입</title>
</head>
<body>
<div class="signup-container">
<h2>회원가입</h2>
<form action="/DB_login/signup.jsp" method="POST">
<input type="text" name="id" placeholder="사용자 아이디" required>
<input type="email" name="email" placeholder="이메일" required>
<input type="password" name="password" placeholder="비밀번호" required>
<input type="password" name="confirm_password" placeholder="비밀번호 확인" required>
<button type="submit">회원가입</button>
</form>
<div class="login-link">
<a href="/DB_login/login.html">로그인</a>
</div>
</div>
</body>
</html>
signup.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean class="loginDB.MemberDAO" id="singup"></jsp:useBean>
<jsp:useBean class="loginDB.MemberDTO" id="data"></jsp:useBean>
<jsp:setProperty property="*" name="data" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String id = request.getParameter("id");
String password = request.getParameter("password");
String confirm_password = request.getParameter("confirm_password");
String email = request.getParameter("email");
String redirectURL = "http://localhost:8088/DB_login/"; //무언가 잘못되었을 때 넘길 페이지
boolean method = request.getMethod().equals("POST");
if(method){
if(!password.equals(confirm_password)){
response.sendRedirect(redirectURL);
}
data.setId(id);
data.setPassword(password);
data.setEmail(email);
boolean flag = singup.insert(data);
if(flag){
System.out.println("회원가입 성공");
response.sendRedirect(redirectURL+"login.html");
}
else{
System.out.println("회원가입 실패");
response.sendRedirect(redirectURL+"signup.html");
}
}
else{
// 리다이렉트 수행
response.sendRedirect(redirectURL);
%>
<h1>잘못된 요청입니다.<a href="/DB_login/signup.html">로그인페이지로 돌아가기</a></h1>
<%
}
%>
</body>
</html>
login.css
@charset "UTF-8";
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
.login-container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
.login-container h2 {
margin-bottom: 20px;
}
.login-container input[type="text"], .login-container input[type="password"]
{
width: 100%;
padding: 10px;
margin: 5px 0;
border: 1px solid #ccc;
border-radius: 3px;
}
.login-container button {
width: 100%;
padding: 10px;
background-color: #007BFF;
border: none;
color: white;
border-radius: 3px;
cursor: pointer;
}
.login-container button:hover {
background-color: #0056b3;
}
.login-container .signup-link {
margin-top: 10px;
text-align: center;
}
signup.css
@charset "UTF-8";
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
.signup-container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
.signup-container h2 {
margin-bottom: 20px;
}
.signup-container input[type="text"], .signup-container input[type="password"],
.signup-container input[type="email"] {
width: 100%;
padding: 10px;
margin: 5px 0;
border: 1px solid #ccc;
border-radius: 3px;
}
.signup-container button {
width: 100%;
padding: 10px;
background-color: #28a745;
border: none;
color: white;
border-radius: 3px;
cursor: pointer;
}
.signup-container button:hover {
background-color: #218838;
}
.signup-container .login-link {
margin-top: 10px;
text-align: center;
}
MemberDTO.java
package loginDB;
public class MemberDTO {
private String id;
private String password;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MemberDAO.java
package loginDB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class MemberDAO {
private final static String SQL_delete = "";
private final static String SQL_update = "";
private final static String SQL_insert = "INSERT INTO MEMBER (MID,PASSWORD,EMAIL) VALUES (?,?,?)";
private final static String SQL_selectOne = "SELECT MID, PASSWORD, EMAIL FROM MEMBER WHERE MID = ?";
private final static String SQL_selectAll = "";
private boolean delete(MemberDTO data) {
return false;
}
private boolean update(MemberDTO data) {
return false;
}
public boolean insert(MemberDTO data) {
Connection conn = JDBCutil.connect();
boolean flag = false;
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(SQL_insert);
pstmt.setString(1,data.getId());
pstmt.setString(2,data.getPassword());
pstmt.setString(3,data.getEmail());
int size = pstmt.executeUpdate();
if(size <= 0) {
System.err.println("insert로그 등록할 수 없습니다.");
return flag;
}
} catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
return flag;
}finally{
System.err.println("insert DB 실행 실패");
}
flag = JDBCutil.disconnect(conn, pstmt);
return flag;
}
public MemberDTO selectOne(MemberDTO data) {
MemberDTO result = null;
Connection conn = JDBCutil.connect();
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(SQL_selectOne);
pstmt.setString(1, data.getId());
ResultSet rs = pstmt.executeQuery();
rs.next();
if(rs.getString("MID").equals(data.getId()) && rs.getString("PASSWORD").equals(data.getPassword())) {
result = new MemberDTO();
result.setId(rs.getString("MID"));
result.setPassword(rs.getString("PASSWORD"));
result.setEmail(rs.getString("EMAIL"));
}
return result;
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.err.println("SQL selectOne Login 실패");
}finally {
System.err.println("SQL selectOne Login 성공");
}
JDBCutil.disconnect(conn, pstmt);
return null;
}
private ArrayList<MemberDTO> selectAll(ArrayList<MemberDTO> datas) {
return datas;
}
}
728x90
'국비 내용 정리 > 과제' 카테고리의 다른 글
jSP 쇼핑몰 제작(page : controller) (0) | 2024.08.11 |
---|---|
jSP 쇼핑몰 제작(page : cart, join, product) (0) | 2024.08.11 |
jSP 쇼핑몰 제작(page : index, main, mypage, admin) (0) | 2024.08.11 |
jsp를 통해 회원가입 작성하기 (0) | 2024.08.06 |
[과제] 미디어쿼리 사용 반응형 웹 (0) | 2024.08.05 |