IT/Jsp

jsp - 회원가입, 로그인, 로그아웃, 회원정보목록, 회원정보보기, 회원수정, 회원삭제

노마드오브 2018. 9. 18. 22:26

파일명 : joinForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>회원가입</h1>

<hr>

<form action="joinPro.jsp" method="post">

아이디 : <input type="text" name="id"><br> 

패스워드 : <input type="password" name="passwd"><br>

이름 : <input type="text" name="name"><br>

나이 : <input type="number" name="age" min="0" max="200"><br>

성별 : <input type="radio" name="gender" value="남">남성 

<input type="radio" name="gender" value="여">여성<br>

이메일 : <input type="email" name="email"><br>

<input type="submit" value="회원가입">

</form>

</body>

</html>



파일명 : joinPro.jsp

<%@page import="com.db.MyOracle"%>

<%@page import="java.sql.*"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>


<%

// 한글처리

request.setCharacterEncoding("utf-8");

// 파라미터 가져오기

String id = request.getParameter("id");

String passwd = request.getParameter("passwd");

String name = request.getParameter("name");

int age = Integer.parseInt(request.getParameter("age"));  

String gender = request.getParameter("gender");

String email = request.getParameter("email");

//날짜생성

Timestamp reg_date = new Timestamp(System.currentTimeMillis());

// JDBC 참조변수

Connection con = null;

PreparedStatement pstmt = null;


// 1. 드라이버 로딩

Class.forName(MyOracle.DRIVER_NAME);

// 2. DB 연결

con = DriverManager.getConnection(MyOracle.URL, MyOracle.USER, MyOracle.PWD);

// 3. insert

String sql = "insert into member (id, passwd, name, reg_date, age, gender, email) values (?,?,?,?,?,?,?)";

pstmt = con.prepareStatement(sql);

pstmt.setString(1, id);

pstmt.setString(2, passwd);

pstmt.setString(3, name);

pstmt.setTimestamp(4, reg_date);

pstmt.setInt(5, age);

pstmt.setString(6, gender);

pstmt.setString(7, email);

// 실행

pstmt.executeUpdate();

// JDBC 자원닫기

pstmt.close();

con.close();

%>

<script>

alert('회원가입 성공');

location.href = 'loginForm.jsp';

</script>



파일명 : loginForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>로그인</h1>

<hr>

<form action="loginPro.jsp" method="post">

아이디: <input type="text" name="id"><br>

패스워드: <input type="password" name="passwd"><br>

<input type="submit" value="로그인">

<input type="button" value="회원가입" onclick="location.href='joinForm.jsp'">

</form>

</body>

</html>



파일명 : loginPro.jsp

<%@page import="java.sql.DriverManager"%>

<%@page import="com.db.MyOracle"%>

<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.PreparedStatement"%>

<%@page import="java.sql.Connection"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>


<%

// 폼 id passwd 가져오기

String id = request.getParameter("id");

String passwd = request.getParameter("passwd");

// JDBC 참조변수

Connection con = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

// 1. 드라이버 로딩

Class.forName(MyOracle.DRIVER_NAME);

// 2. DB연결

con = DriverManager.getConnection(MyOracle.URL, MyOracle.USER, MyOracle.PWD);

// 3. id에 해당하는 passwd 가져오기

String sql = "SELECT passwd FROM member WHERE id=?";

pstmt = con.prepareStatement(sql);

pstmt.setString(1, id);

// 4. 실행  rs에 저장

rs = pstmt.executeQuery();

// 5.

//   rs에 데이터(행)가 있으면 아이디있음

//       패스워드비교 맞으면 로그인인증(세션값생성 "id")

//       패스워드비교 틀리면 "패스워드틀림" 로그인페이지로 이동

//   rs에 데이터(행)가 없으면 "아이디없음" 로그인페이지로 이동

if (rs.next()) {

// 아이디있음

if (passwd.equals(rs.getString("passwd"))) {

// 로그인인증 main.jsp로 이동

session.setAttribute("id", id);

response.sendRedirect("main.jsp");

} else {

%>

<script>

alert('패스워드 틀림');

history.back();

</script>

<%

}

} else {

// 아이디없음  뒤로이동

%>

<script>

alert('아이디 없음');

//location.href = 'loginForm.jsp';  // 서버가 요청을 받는다.

history.back();  // history.go(-1);  // 서버에 요청없이 브라우저에서 자체적으로 처리

</script>

<%

}

%>



파일명 : main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%@include file="loginCheck.jspf" %>


<h1>메인페이지</h1>

<hr>

<%=id %>님이 로그인 하셨습니다.<br>

<input type="button" value="로그아웃" onclick="location.href='logout.jsp'"><br>

<a href="info.jsp">회원정보조회</a><br>

<a href="update.jsp">회원정보수정</a><br>

<a href="delete.jsp">회원정보삭제</a><br>


<%

if (id.equals("admin")) {

%>

<a href="list.jsp">전체회원목록</a><br>

<%

}

%>


</body>

</html>



파일명 : loginCheck.jspf

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%

// 세션값 가져오기

String id = (String) session.getAttribute("id");

// 세션값 없으면(null이면) loginForm.jsp로 이동

if (id == null) {

response.sendRedirect("loginForm.jsp");

return;

}

%>



파일명 : logout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>


<%

// 세션초기화

session.invalidate();

// "로그아웃됨"  loginForm.jsp로 이동

%>

<script>

alert('로그아웃');

location.href = 'loginForm.jsp';

</script>



파일명 : list.jsp

<%@page import="java.sql.DriverManager"%>

<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.PreparedStatement"%>

<%@page import="java.sql.Connection"%>

<%@page import="com.db.MyOracle"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>전체회원목록</h1>

<hr>

<%

// 세션값 가져오기

String id = (String) session.getAttribute("id");

// 세션값 없으면(null이면) loginForm.jsp로 이동

if (id == null || !id.equals("admin")) {

response.sendRedirect("main.jsp");

return;

}


// JDBC 변수

Connection con = null;

PreparedStatement pstmt = null;

ResultSet rs = null;


// 1. 드라이버 로딩

Class.forName(MyOracle.DRIVER_NAME);

// 2. DB연결

con = DriverManager.getConnection(MyOracle.URL, MyOracle.USER, MyOracle.PWD);

// 3. sql 전체회원 가져오기

String sql = "select * from member";

pstmt = con.prepareStatement(sql);

// 4. rs 저장

rs = pstmt.executeQuery();

%>


<table border="1">

<tr>

<th>아이디</th><th>패스워드</th><th>이름</th>

<th>성별</th><th>나이</th><th>이메일</th><th>가입일자</th>

</tr>

<%

while (rs.next()) {  // 한 행식 출력

%>

<tr>

<td><%=rs.getString("id") %></td>

<td><%=rs.getString("passwd") %></td>

<td><%=rs.getString("name") %></td>

<td><%=rs.getString("gender") %></td>

<td><%=rs.getString("age") %></td>

<td><%=rs.getString("email") %></td>

<td><%=rs.getString("reg_date") %></td>

</tr>

<%

}

%>

</table></body>


<h3><a href="main.jsp">메인화면</a></h3>


</html>



파일명 : info.jsp

<%@page import="java.sql.*"%>

<%@page import="com.db.MyOracle"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%@include file="loginCheck.jspf" %>

<%

// 변수선언

String passwd = "";

String name = "";

Timestamp reg_date = null;

int age = 0;

String gender = "";

String email = "";


Connection con = null;

PreparedStatement pstmt = null;

Statement stmt = null;

ResultSet rs = null;

// 1. 드라이버 로딩

Class.forName(MyOracle.DRIVER_NAME);

// 2. DB 연결

con = DriverManager.getConnection(MyOracle.URL, MyOracle.USER, MyOracle.PWD);

// 3. sql  id에 해당하는 모든정보 가져오기

//String sql = "select * from member where id = ?";

//pstmt = con.prepareStatement(sql);

//pstmt.setString(1, id);

stmt = con.createStatement();

// 4. rs 실행 저장

//rs = pstmt.executeQuery();

String sql2 = "select * from member where id = '" + id + "'";

rs = stmt.executeQuery(sql2);

// 5. rs => 변수저장, 출력

if (rs.next()) {

passwd = rs.getString("passwd");

name = rs.getString("name");

reg_date = rs.getTimestamp("reg_date");

age = rs.getInt("age");

gender = rs.getString("gender");

email = rs.getString("email");

}

%>

<h1>회원정보 조회</h1>

<hr>

아이디: <%=id %><br>

패스워드: <%=passwd %><br>

이름: <%=name %><br>

가입날짜: <%=reg_date %><br>

나이: <%=age %><br>

성별: <%=gender %><br>

이메일: <%=email %><br>

<br>

<a href="main.jsp">메인화면</a>

</body>

</html>



파일명 : update.jsp

<%@page import="com.db.MyOracle"%>

<%@page import="java.sql.*"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%@include file="loginCheck.jspf" %>

<%

// 변수선언

String passwd = "";

String name = "";

Timestamp reg_date = null;

int age = 0;

String gender = "";

String email = "";


Connection con = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

Class.forName(MyOracle.DRIVER_NAME);

con = DriverManager.getConnection(MyOracle.URL, MyOracle.USER, MyOracle.PWD);

String sql = "select * from member where id = ?";

pstmt = con.prepareStatement(sql);

pstmt.setString(1, id);

rs = pstmt.executeQuery();

if (rs.next()) {

passwd = rs.getString("passwd");

name = rs.getString("name");

reg_date = rs.getTimestamp("reg_date");

age = rs.getInt("age");

gender = rs.getString("gender");

email = rs.getString("email");

}

%>

<h1>회원수정</h1>

<hr>

<form action="updatePro.jsp" method="post">

아이디 : <input type="text" name="id" value="<%=id%>" readonly><br> 

패스워드 : <input type="password" name="passwd"><br>

이름 : <input type="text" name="name" value="<%=name%>"><br>

나이 : <input type="number" name="age" min="0" max="200" value="<%=age%>"><br>

성별 : <input type="radio" name="gender" value="남"" 

<% if (gender != null && gender.equals("남")) { %>checked<% } %>>남성 

<input type="radio" name="gender" value="여">

<% if (gender != null && gender.equals("여")) { %>checked<% } %>여성<br>

이메일 : <input type="email" name="email" value="<%=email%>"><br>

<input type="submit" value="회원가입">

</form>

</body>

</html>



파일명 : updatePro.jsp

<%@page import="com.db.MyOracle"%>

<%@page import="java.sql.*"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ include file="loginCheck.jspf" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%

// post 한글처리

request.setCharacterEncoding("utf-8");

// 폼 파라미터 값

String passwd = request.getParameter("passwd");

String name = request.getParameter("name");

int age = Integer.parseInt(request.getParameter("age"));

String gender = request.getParameter("gender");

String email = request.getParameter("email");

Connection con = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

// 1. 드라이버 로딩

Class.forName(MyOracle.DRIVER_NAME);

// 2. DB연결

con = DriverManager.getConnection(MyOracle.URL, MyOracle.USER, MyOracle.PWD);

// 3. id에 해당하는 passwd 가져오기

String sql = "select passwd from member where id = ?";

pstmt = con.prepareStatement(sql);

pstmt.setString(1, id);

// 4. 실행 rs 저장

rs = pstmt.executeQuery();

// 5. rs 데이터 있으면 아이디 있음

//        패스워드 비교 맞으면 update  "main.jsp"이동

//                  틀리면  "패스워드틀림"  뒤로이동

if (rs.next()) {

if (passwd.equals(rs.getString("passwd")) ) {

pstmt.close();

pstmt = null;

sql = "update member set name=?,age=?,gender=?,email=? WHERE id=?";

pstmt = con.prepareStatement(sql);

pstmt.setString(1, name);

pstmt.setInt(2, age);

pstmt.setString(3, gender);

pstmt.setString(4, email);

pstmt.setString(5, id);

// 실행

pstmt.executeQuery();

// main.jsp 이동

//response.sendRedirect("main.jsp");

%>

<script>

alert("수정성공");

location.href="main.jsp";

</script>

<%

} else {

%>

<script>

alert("패스워드 틀림");

history.back();

</script>

<%

}

}

%>

</body>

</html>



파일명 : delete.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ include file="loginCheck.jspf" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>회원삭제</h1>

<form action="deletePro.jsp" method="post">

아이디 : <input type="text" name="id" value="<%=id%>" readonly="readonly"><br>

패스워드 : <input type="password" name="passwd"><br>

<input type="submit" value="회원삭제">

</form>

</body>

</html>



파일명 : deletePro.jsp

<%@page import="com.db.MyOracle"%>

<%@page import="java.sql.*"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ include file="loginCheck.jspf" %>

<%

// passwd 가져오기

String passwd = request.getParameter("passwd");

Connection con = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

// 1. 드라이버 로딩

Class.forName(MyOracle.DRIVER_NAME);

// 2. DB연결

con = DriverManager.getConnection(MyOracle.URL, MyOracle.USER, MyOracle.PWD);

// 3. id에 해당하는 passwd 가져오기

String sql = "select passwd from member where id = ?";

pstmt = con.prepareStatement(sql);

pstmt.setString(1, id);

// 4. 실행   rs 저장

rs = pstmt.executeQuery();

// 5. 데이터가 있으면 패스워드비교 맞으면 delete "loginForm.jsp"이동

//                         틀리면 "패스워드틀림" 뒤로이동

if (rs.next()) {

if (passwd.equals(rs.getString("passwd"))) {

pstmt.close();

pstmt = null;

sql = "delete from member where id = ?";

pstmt = con.prepareStatement(sql);

pstmt.setString(1, id);

// 실행

pstmt.executeUpdate();

// 세션값 초기화

session.invalidate();

// response.sendRedirect("loginForm.jsp");

%>

<script>

alert("삭제되었습니다.");

location.href = "loginForm.jsp";

</script>

<%

} else {

%>

<script>

alert("패스워드 틀림");

history.back();

</script>

<%

}

%>