청년취업아카데미/HTML

[21.01.29 HTML] JDBC연결

RSpring41 2021. 1. 29. 18:15

 

DB생성은 java 카테고리 참조

로그인 예제.egg
1.98MB

 

 

 sql에 있는 connection을 사용해야함

oracle jdbc 라이브러리는 lib 폴더에 넣기만 하면 된다.

 

▶ JSP에서는 session 이 자동적으로 선언되기 때문에 사용 가능하지만 servlet 파일에서는 선언해줘야함

HttpSession session = request.getSession();

 

뷰 모델 컨트롤러

html - dao - servlet

 

 

 

src 자바 파일은 데이터 제어 

 

 

 

 

 

 

html은 사용자에게 보여지는 부분

jsp도 보여 주지만 유동적으로 변경되는 부분

 

 

 

 

 회원가입 예제

 

회원가입 창

<!-- 회원 가입 창 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<style type="text/css">
input {
	width: 120px;
}

td {
	text-align: right;
}

.loginTable{
	margin: auto;
}

.loginBox {
	text-align: center;
	width: 300px;
	margin: auto;
	margin-top: 100px;
}
</style>
</head>
<body>
	<fieldset class="loginBox">
		<legend>회원가입</legend>

		<form action="Ex01_join" method="post">
			<table class="loginTable">
				<tr>
					<td width="60px">ID :</td>
					<td width="128px"><input type="text" name="ID" placeholder="아이디" required="required"></td>
				</tr>
				<tr>
					<td>PW :</td>
					<td><input type="password" name="PW" placeholder="비밀번호"
						required="required"></td>
				</tr>
				<tr>
					<td>NAME :</td>
					<td><input type="text" name="NAME" placeholder="이름"
						required="required"></td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" value="가입" style="width: 128px;"></td>
				</tr>
			</table>
		</form>
	</fieldset>

</body>
</html>

DB에  저장

// DB에 저장
package controller;

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;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import model.DAO;


@WebServlet("/Ex01_join")
public class Ex01_join extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// 세션 객체 생성
		HttpSession session = request.getSession();
		
		// 값 받아오기
		String id = request.getParameter("ID");
		String pw = request.getParameter("PW");
		String name = request.getParameter("NAME");
		
		DAO dao = new DAO();
		
		if(dao.join(id, pw, name)>0){
			//로그인  성공		
			session.setAttribute("name", name);	
			response.sendRedirect("ex01_joinSuccess.jsp");	
						
		}else {
			//로그인 실패
			response.sendRedirect("ex01_joinFalse.jsp");
		}
		
		
	}

}

 

회원 가입 성공

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<style type="text/css">
input {
	width: 120px;
}

td {
	text-align: right;
}

.loginTable{
	margin: auto;
}

.loginBox {
	text-align: center;
	width: 300px;
	margin: auto;
	margin-top: 100px;
}
</style>
</head>
<body>

<%
	//세션 조회
	String name = (String)session.getAttribute("name");
%>

	<fieldset class="loginBox">
		<legend>회원가입 성공</legend>

		<form>
			<table class="loginTable">
				<tr>
					<td><%=name %> 님 회원가입을 환영합니다.</td>
				</tr>
				<tr>
					<td><input type="button" value="로그아웃" style="width: 100%;"
					onclick="location.href='ex01_loginOut.jsp'"></td>
				</tr>
			</table>
		</form>
	</fieldset>

</body>
</html>