<%%>
- 스크립트릿
- 온갖 자바 문법을 사용할 수 있음
<%=%>
- 표현식
- 변수의 내용을 출력
<%@%>
- 지시자
JSP와 JAVA
- jsp 또한 java 의 DB 와 연결하는 것과 비슷하다. 단, 이 때 webapp > WEB-INF > lib 폴더에 ojdbc8.jar 파일을 추가해주어야 한다.
<!-- start.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="http://localhost:8088/day0712_a/InsertBook">도서등록</a>
<a href="http://localhost:8088/day0712_a/InsertDept">부서등록</a>
<a href="http://localhost:8088/day0712_a/InsertCustomer">고객등록</a>
<a href= "http://localhost:8088/day0712_a/ListCustomer">고객목록</a>
</body>
</html>
<!-- InsertBook.jsp -->
<%@ 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>
<h2>도서등록</h2>
<hr>
<form action="InsertBookOK.jsp" method="post">
도서번호 : <input type="text" name = "bookid"><br>
도서이름 : <input type="text" name = "bookname"><br>
도서가격 : <input type="text" name = "price"><br>
출판사명 : <input type="text" name = "publisher"><br>
<input type="submit" value="등록">
<input type="reset" value="취소">
</form>
</body>
</html>
<!-- insertCustomOK -->
<%@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>Insert title here</title>
</head>
<body>
<%
//새로운 도서 등록을 위하여 사용자가 입력한 도서의 정보(도서번호, 도서명, 가격, 출판사)는
//jsp 내장 객체인 request 에 실려서 온다.
//사용자가 요청한 문자 은 한글임을 설정
request.setCharacterEncoding("UTF-8");
//사용자가 입력한 도서번호, 도서명, 가격, 출판사를 받아와서 변수에 저장
int bookid = Integer.parseInt(request.getParameter("bookid"));
String bookname = request.getParameter("bookname");
int price = Integer.parseInt(request.getParameter("price"));
String publisher = request.getParameter("publisher");
//DB연결하여 실행 시킬 DB 명령어 sql 을 만든다.
String sql = "insert into book values(?,?,?,?)";
//데이터베이스 연결에 필요한 변수들을 만든다.
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String username = "c##madang";
String password = "madang";
int re = 0;
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bookid);
pstmt.setString(2, bookname);
pstmt.setInt(3, price);
pstmt.setString(4, publisher);
re = pstmt.executeUpdate();
conn.close();
pstmt.close();
}catch(Exception e){
out.println(e); //out 내장객체 사용 가능
}
if(re >= 1){
out.print("<h2>도서등록 성공</h2>");
}else{
out.print("<h2>도서등록 실패</h2>");
}
%>
</body>
</html>
<!-- listCustomer.jsp -->
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@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>
<h2>고객목록</h2>
<hr>
<ul>
<%
String sql = "select custid, name from customer";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String username= "c##madang";
String password = "madang";
int re = -1;
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
%>
<li><a href="detailCustomer.jsp?custid=<%=rs.getInt(1)%>"><%= rs.getString(2) %></a></li>
<%
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println("예외발생:"+e.getMessage());
}
%>
</ul>
</body>
</html>
<!-- detailCustomer.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>
<script type="text/javascript">
window.onload = function(){
document.querySelector("#delete_btn").onclick = function(event){
let re = confirm("정말로 삭제할까요?");
if(re == false){
event.preventDefault();
return false;
}
}
}
</script>
</head>
<body>
<h2>고객 상세 정보</h2>
<hr>
<%
int custid = Integer.parseInt(request.getParameter("custid"));
String sql = "select * from customer where custid=?";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String username= "c##madang";
String password = "madang";
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, custid);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
%>
고객번호 : <%= custid %><br>
고객이름 : <%= rs.getString(2) %><br>
고객주소 : <%= rs.getString(3) %><br>
고객전화 : <%= rs.getString(4) %><br>
<%
}
%>
<hr>
<a href="updateCustomer.jsp?custid=<%=custid %>">수정</a>
<a href="deleteCustomer.jsp?custid=<%=custid %>" id="delete_btn">삭제</a>
<%
rs.close();
pstmt.close();
conn.close();
}catch(Exception e){
System.out.println("예외발생:"+e.getMessage());
}
%>
</body>
</html>
<!-- updateCustomer.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>
<%
int custid = Integer.parseInt(request.getParameter("custid"));
String sql = "select * from customer where custid=?";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String username= "c##madang";
String password = "madang";
String name = "";
String address= "";
String phone = "";
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, custid);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
name = rs.getString(2);
address = rs.getString(3);
phone = rs.getString(4);
}
rs.close();
pstmt.close();
conn.close();
}catch(Exception e){
System.out.println("예외발생:"+e.getMessage());
}
%>
<h2>고객수정</h2>
<hr>
<form action="updateCustomerOK.jsp" method="post">
고객번호 : <span><%=custid %></span><input type="hidden" name="custid" value="<%=custid%>"><br>
고객이름 : <input type="text" name="name" value="<%=name%>"><br>
고객주소 : <input type="text" name="address" value="<%=address%>"><br>
고객전화 : <input type="text" name="phone" value="<%=phone%>"><br>
<input type="submit" value="수정">
<input type="reset" value="다시입력">
</form>
</body>
</html>
<!-- updateCustomerOK.jsp -->
<%@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>
<%
request.setCharacterEncoding("UTF-8");
int custid = Integer.parseInt(request.getParameter("custid"));
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
String sql = "update customer set name=?,address=?,phone=? where custid=?";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String username= "c##madang";
String password = "madang";
int re = -1;
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, address);
pstmt.setString(3, phone);
pstmt.setInt(4, custid);
re = pstmt.executeUpdate();
pstmt.close();
conn.close();
}catch(Exception e){
System.out.println("예외발생:"+e.getMessage());
}
if(re > 0){
%>
<h3>고객의 정보를 수정하였습니다.</h3>
<%
}else{
%>
<h3>고객의 정보 수정에 실패하였습니다.</h3>
<%
}
%>
<hr>
<a href="listCustomer.jsp">고객목록</a>
<a href="insertCustomer.jsp">고객등록</a>
</body>
</html>
<!-- deleteCustomer.jsp -->
<%@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>
<%
int custid = Integer.parseInt(request.getParameter("custid"));
String sql = "delete customer where custid = ?";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String username= "c##madang";
String password = "madang";
int re = -1;
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, custid);
re = pstmt.executeUpdate();
pstmt.close();
conn.close();
}catch(Exception e){
System.out.println("예외발생:"+e.getMessage());
}
if(re > 0){
%>
<h3>고객의 정보를 삭제하였습니다.</h3>
<%
}else{
%>
<h3>고객의 정보 삭제에 실패하였습니다.</h3>
<%
}
%>
<a href="listCustomer.jsp">고객목록</a>
<a href="insertCustomer.jsp">고객등록</a>
</body>
</html>
- 그런데, 해당 페이지들의 View 화면과 DB 작업 화면 들이 섞여 코드 관리 및 유지보수 등이 힘들어지는 단점이 있다. 이러한 단점을 보완하기 위해 mvc 패턴이 만들어졌다.