[JSP] 회원가입 (로그인/로그아웃/정보조회/정보수정/정보삭제/모든정보목록)
@욕심쟁이
·2020. 8. 25. 17:42
반응형
(디비관련(insert/update/delete/select)참고자료 https://sseozzzy.tistory.com/99)
로그인 로그아웃 정보조회 정보수정 정보삭제 모든정보목록
1단계
- 회원가입 전체 정보를 저장하는 공간 만들기 : 데이터베이스만들기
- member 테이블 생성
create table member (
id varchar(20) primary key,
pass varchar(20),
name varchar(20),
date datetime
);
2단계
- 쇼핑몰 관리할 사용자 만들기(admin) : 사용자 만들고 권한 부여 하기(grant)
- 관리자 root → 디비관리 할 사용자 아이디 : jspid 비밀번호 : jsppass
→ jspdb1을관리할권한부여 create, alter, drop, select, insert, update, delete
grant select,insert,update,delete,create,drop,alter
on jspdb1.* to 'jspid'@'localhost'
identified by 'jsppass';
3단계(회원가입)
- insertForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp4/insertForm.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp4/insertForm.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
%>
<form action="insertPro.jsp" method="post">
아이디 : <input type="text" name="id"><br>
비밀번호 : <input type="password" name="pass"> <br>
이름 : <input type="text" name="name"><br>
<input type="submit" value="회원가입">
</form>
</body>
</html>
- insertPro.jsp
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="java.sql.Time"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp4/insertPro.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp2/insertPro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String pass = request.getParameter("pass");
String name = request.getParameter("name");
Timestamp date = new Timestamp(System.currentTimeMillis()); // 현재 날짜 출력
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost:3306/jspdb1";
String dbUser = "jspid";
String dbpass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl, dbUser, dbpass);
String sql = "insert into member (id,pass,name,date) values(?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1,id);
pstmt.setString(2,pass);
pstmt.setString(3,name);
pstmt.setTimestamp(4, date);
pstmt.executeUpdate();
%>
회원가입성공<%=pstmt %><br>
<%-- 아이디 : <%=id %><br> --%>
<%-- 패스워드 : <%=pass %><br> --%>
<%-- 이름 : <%=name %><br> --%>
<%-- 가입날짜 : <%=date %><br> --%>
<script type="text/javascript">
alert("회원가입 성공");
location.href="loginForm.jsp";
</script>
</body>
</html>
4단계(회원정보조회)
- info.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp4/info.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp4/info.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl="jdbc:mysql://localhost:3306/jspdb1";
String dbId="jspid";
String dbPass="jsppass";
Connection con = DriverManager.getConnection(dbUrl, dbId, dbPass);
String id = (String)session.getAttribute("id");
String sql = "select * from member where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
ResultSet rs = pstmt.executeQuery();
%>
<h2>나의 정보 조회</h2>
<table border="1">
<tr>
<td>아이디</td>
<td>비밀번호</td>
<td>이름</td>
<td>가입날짜</td>
</tr>
<%while(rs.next()){%>
<tr>
<td><%=rs.getString("id") %></td>
<td><%=rs.getString("pass") %></td>
<td><%=rs.getString("name") %></td>
<td><%=rs.getTimestamp("date") %></td>
</tr>
<%}%>
</table>
<a href="main.jsp" title="메인이동">메인이동</a>
</body>
</html>
5단계(login)
- loginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp4/loginForm.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp4/loginForm.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
%>
<form action="loginPro.jsp" method="post">
아이디 : <input type="text" name="id"><br>
비밀번호 : <input type="password" name="pass"> <br>
<input type="submit" value="로그인">
</form>
</body>
</html>
- loginPro.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp4/loginPro.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp4/loginPro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String pass = request.getParameter("pass");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost:3306/jspdb1";
String dbUser = "jspid";
String dbpass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl, dbUser, dbpass);
String sql = "select * from member where id = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1,id);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
out.println("아이디 있음");
if(pass.equals(rs.getString("pass"))){
out.println("비밀번호 일치");
session.setAttribute("id", id); //세션저장
session.setAttribute("pass", pass); //세션저장
response.sendRedirect("main.jsp"); //세션저장
}else{
out.println("비밀번호 불일치");
%>
<script type="text/javascript">
alert("비밀번호 불일치");
history.back(); //이전페이지 이동
</script>
<%}
}else{
out.println("아이디 없음");%>
<script type="text/javascript">
alert("아이디 없음");
history.back(); //이전페이지 이동
</script>
<%}%>
</body>
</html>
6단계(login 확인)
- main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp4/main.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp4/main.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
if(id == null){
response.sendRedirect("loginForm.jsp");
}
%>
<%=session.getAttribute("id")%> 님이 로그인 하셨습니다. //세션저장값들고오기
</body>
</html>
7단계(logout)
- main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp4/main.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp4/main.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String id = (String)session.getAttribute("id"); //세션저장값들고오기
//session.getAttribute은 String이 아니라 Object기 때문에 형변환을 해줘야한다.
if(id == null){
response.sendRedirect("loginForm.jsp");
}
%>
<%=id%> 님이 로그인 하셨습니다.<br>
<input type="button" value="로그아웃" onclick="location.href='logout.jsp'">
</body>
</html>
- logout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp4/logout.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp4/logout.jsp</h1>
<%
//세션값 전체삭제
session.invalidate();
%>
<script type="text/javascript">
alert("로그아웃");
location.href="loginForm.jsp";
</script>
</body>
</html>
8단계(정보수정)
- updateForm.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>
<h1>WebContent/Study/jsp2/updateForm.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String id =(String)session.getAttribute("id");
String nm = (String)session.getAttribute("name");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
String sql = "select * from member where id= ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1,id);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){%>
<form action="updatePro.jsp" method="post">
아이디 : <input type="text" name="id"value=<%=rs.getString("id") %> readonly><br>
비밀번호 : <input type="password" name="pass"> <br>
이름 : <input type="text" name="name" value=<%=rs.getString("name") %>><br>
<input type="submit" value="회원정보수정">
</form>
<%} %>
</body>
</html>
- updatePro.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>
<h1>WebContent/Study/jsp2/updatePro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String rid =request.getParameter("id");
String rpass = request.getParameter("pass");
String rnm = request.getParameter("name");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
String sql1 = "select * from member where id=?";
PreparedStatement pstmt = con.prepareStatement(sql1);
pstmt.setString(1,rid);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
String rsnm = rs.getString("name");
out.println("아이디있음");
if(rs.getString("pass").equals(rpass)){
out.println("비밀번호일치");
String sql2 = "update member set name=? where id = ? and name =? and pass=?";
pstmt = con.prepareStatement(sql2);
pstmt.setString(1,rnm);
pstmt.setString(2,rid);
pstmt.setString(3,rsnm);
pstmt.setString(4,rs.getString("pass"));
pstmt.executeUpdate();
response.sendRedirect("main.jsp");
}else{%>
<script type="text/javascript">
alert("비밀번호 틀림");
history.back();
</script>
<%}
}else{%>
<script type="text/javascript">
alert("아이디 없음");
history.back();
</script>
<%}
%>
</body>
</html>
9단계(회원탈퇴)
- deletForm.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>
<h1>WebContent/Study/jsp2/deleteForm.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String id =(String)session.getAttribute("id");
String nm = (String)session.getAttribute("name");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
String sql = "select * from member where id= ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1,id);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){%>
<form action="deletePro.jsp" method="post">
아이디 : <input type="text" name="id"value=<%=rs.getString("id") %> readonly><br>
비밀번호 : <input type="password" name="pass"> <br>
이름 : <input type="text" name="name" value=<%=rs.getString("name") %>><br>
<input type="submit" value="회원정보수정">
</form>
<%} %>
</body>
</html>
- deletePro.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>
<h1>WebContent/Study/jsp2/deletePro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String rid =request.getParameter("id");
String rpass = request.getParameter("pass");
String rnm = request.getParameter("name");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
String sql1 = "select * from member where id=?";
PreparedStatement pstmt = con.prepareStatement(sql1);
pstmt.setString(1,rid);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
String rsnm = rs.getString("name");
out.println("아이디있음");
if(rs.getString("pass").equals(rpass)){
out.println("비밀번호일치");
if(rs.getString("name").equals(rnm)){
out.println("이름일치");
String sql2 = "delete from member where id=? and name=? and pass=?";
pstmt = con.prepareStatement(sql2);
pstmt.setString(1,rid);
pstmt.setString(2,rnm);
pstmt.setString(3,rs.getString("pass"));
pstmt.executeUpdate();
session.invalidate();
%>
<script type="text/javascript">
alert("회원탈퇴되었습니다.");
location.href="loginForm.jsp";
</script>
<%
// response.sendRedirect("loginForm.jsp");
}else{%>
<script type="text/javascript">
alert("이름 틀림");
history.back();
</script>
<%}
}else{%>
<script type="text/javascript">
alert("비밀번호 틀림");
history.back();
</script>
<%}
}else{%>
<script type="text/javascript">
alert("아이디 없음");
history.back();
</script>
<%}
%>
</body>
</html>
10단계(회원리스트확인)
-list.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>
<h1>WebContent/Study/jsp2/list.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
String sql = "select * from member";
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
%>
<table border="1" width="40%">
<tr>
<td>아이디</td>
<td>비밀번호</td>
<td>이름</td>
<td>가입일자</td>
</tr>
<%
while(rs.next()){%>
<tr>
<td><%=rs.getString("id") %></td>
<td><%=rs.getString("pass") %></td>
<td><%=rs.getString("name") %></td>
<td><%=rs.getDate("date") %></td>
</tr>
<%}
%>
</table>
</body>
</html>
반응형
'IT > JSP' 카테고리의 다른 글
JSTL(JSP Standard Tag Library) (0) | 2021.01.22 |
---|---|
[JSP]게시판 만들기(글목록/글쓰기/글수정/글삭제) (0) | 2020.09.10 |
[JSP] Cookie란? (0) | 2020.08.14 |
[JSP] Session이란? (0) | 2020.08.14 |
[JSP] Include태그 (0) | 2020.08.13 |