2025.10.29

오늘 수업의 목적 : 웹 프로그램의 흐름을 이해하는 날

CRUD

주제만 다를 뿐 웹은 거의 CRUD로 진행 된다.

회사에서 뽑을 때 게시판 만들 수 있는지 물어봄

사원 관리 프로그램

개발 순서

  1. DB스키마 설계
create table employee(
    idx number(4) primary key,
    name varchar2(30),
    email varchar2(100),
    dept varchar2(50)
)
create sequence employee_idx

insert into employee
values(employee_idx.nextval, '홍길동','hong@naver.com','교육과')

select * from employee 
  1. 디자인 설계(컨텐츠의 구조)
<%@ 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>
<link rel="stylesheet" type="text/css" href="CSS/mainLayout.css">
<style>
	h2{
		text-align:center;
	}
	fieldset{
		width:400px;
		margin:0px auto;
	}
	table{
		margin:0px auto; /*상,하 0px 좌,우 auto 자동으로 가운데 조정*/
	}
</style>
</head>
<body>
<%@include file="header.jsp" %>
<main>
	<section>
		<article>
			<h2>사원 관리 프로그램(기본형)</h2>
			<form name="empAdd" action="empAdd_ok.jsp">
				<fieldset>
					<legend>사원 등록</legend>
					<table>
						<tr>
							<th>사원이름</th>
							<td><input type="text" name="name"></td>
						</tr>
						<tr>
							<th>E-mail</th>
							<td><input type="text" name="email"></td>
						</tr>
						<tr>
							<th>부서명</th>
							<td><input type="text" name="dept"></td>
						</tr>
						<tr>
							<td colspan="2" align="center">
								<input type="submit" value="사원등록">
								<input type="reset" value="다시작성">
							</td>
						</tr>
					</table>
				</fieldset>
			</form>
		</article>
	</section>
</main>
<%@include file="footer.jsp" %>
</body>
</html>
  1. 기능 구현 (처리 페이지)

    1. index[사원 관리 프로그램] → emp.js
    2. 처리 페이지에서는 화면이 필요 없음.
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
    <%@ page import="java.util.*"%>
    <%@ page import="java.io.*"%>
    
    <%
    	//1. 재료 수집
    	String name=request.getParameter("name");
    	String email=request.getParameter("email");
    	String dept=request.getParameter("dept");
    	
    	//2. DB 연동
    	Class.forName("oracle.jdbc.driver.OracleDriver");
    	String url="jdbc:oracle:thin:@localhost:1521:xe";
    	String user="scott";
    	String pwd="1234";
    	Connection conn=DriverManager.getConnection(url, user, pwd);
    	
    	//3. 디비 입력
    	String sql="insert into employee values(employee_idx.nextval, ?, ?, ?)";
    	PreparedStatement ps = conn.prepareStatement(sql);
    	ps.setString(1, name);
    	ps.setString(2, email);
    	ps.setString(3, dept);
    	int count=ps.executeUpdate();
    	ps.close();
    	conn.close();
    	
    	if(count>0){
    		%>
    		<script>
    			window.alert('사원등록 완료하였습니다.');
    			location.href='emp.jsp';
    		</script>
    		<%
    	}else{
    		%>
    		<script>
    			window.alert('사원등록에 실패하였습니다.');
    			location.href='emp.jsp';
    		</script>
    		<%
    	}
    	
    %>
    

서블릿 코드 보는 경로

C:\student_java\jspstudy\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\myweb\org\apache\jsp